Python print and json.dumps changes float value

Viewed 128

Hitting below issue on a linux server with python 3.6.8. Json.dumps() call with simple float variable gives different results in each run.

$ python3
Python 3.6.8 (default, Oct  8 2020, 16:04:18) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44.0.200)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> print(json.dumps(float(2341)))
2373.0
>>> print(json.dumps(float(2341)))
2342.28
>>> print(json.dumps(float(2341)))
2342.28
>>> print(json.dumps(float(2341)))
2342.3121632
>>> print(json.dumps(float(2341)))
2342.28
>>> print(json.dumps(float(2341)))
2341.0
>>> print(json.dumps(float(2341)))
2341.0
>>> print(json.dumps(float(2341)))
2373.0
>>> print(json.dumps(float(2341)))
2342.28
>>> print(json.dumps(float(2341)))
2342.28
>>> print(json.__file__)
/usr/lib64/python3.6/json/__init__.py
>>> exit()
$ which python3
/usr/bin/python3
$ 

No issues with 'Int' variable. Have verified files in python package folder, all seem intact. No issues seen related to server's memory/cpu. Rebooting server / reinstalling python package are not feasible options currently. Need some pointers in debugging/fixing this further.

---- EDIT ---- Issue seen with print(float(x)) also

$ python3
Python 3.6.8 (default, Oct  8 2020, 16:04:18) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44.0.200)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print(float(2341))
2341.0
>>> print(float(2341))
2341.0
>>> print(float(2341))
2342.28
>>> print(float(2341))
2342.312
>>> print(float(2341))
2342.3121632
>>> print(float(2341))
2342.28
>>> print(float(2341))
2342.312
>>> print(float(2341))
2342.28
>>> print(float(2341))
2342.28
>>> print(float(2341))
2342.312
>>> print(float(2341))
2342.28
>>> print(float(2341))
2341.0
>>> print(float(2341))
2373.0
>>> exit()

----- EDIT ---- Different flavours of print + float. Same issue.

>>> x = float(2341)
>>> for _ in range(20): print(x)
... 
2342.312
2342.28
2342.28
2342.28
2342.312
2342.312
2373.0
2342.28
2373.0
2373.0
2373.0
2373.0
2342.3121632
2342.3121632
2373.0
2342.28
2373.0
2342.28
2342.28
2373.0
>>>
>>> for _ in range(20): print(2341.0)
... 
2373.0
2342.28
2342.312
2342.28
2342.28
2342.28
2342.28
2373.0
2342.28
2342.28
2373.0
2373.0
2342.28
2373.0
2342.312
2373.0
2373.0
2342.28
2342.3121632
2342.3121632
>>> 
>>> import builtins
>>> print(builtins.float(2341))
2342.28
>>> print(builtins.float(2341))
2373.0
>>> print(builtins.float(2341))
2342.3121632
>>> print(builtins.float(2341))
2373.0
>>> 
1 Answers

This doesn't seem to be the normal behavior of json.dumps(). For that reason, the best thing to do here is to ensure that everything is working correctly and reinstall any packages or modules that are causing the problem. Specifically, try reinstalling the base Python interpreter and see what happens with print(json.dumps(2341.0)) and other similar expressions. Additionally, if all else fails, you can always use a third-party library, like orjson.

Related