pre-commit-hooks icon indicating copy to clipboard operation
pre-commit-hooks copied to clipboard

`pretty-format-json` modifies floating point numbers that have too many digits of precision

Open dougthor42 opened this issue 2 years ago • 5 comments

pretty-format-json modifies floating point numbers that have too many digits of precision.

Background

I have to work with JSON files that may be generated by non-python programs. These files, for whatever reason, have numbers that have up to 16 digits after the decimal place.

(It's absurd, really. The people taking these measurements are somehow able to measure 0.1μHz on a 10GHz scale?? Yeah, they're saving values like 5.9257052820783001 GHz. Someone needs to teach them about significant figures... but that's beside the point. The point is I have to deal with this data :unamused:)

Steps to Reproduce

  1. Create the following json file:
    {"foo": 4.4257052820783003}
    
  2. Run pretty-format-json on it.

Expected Output:

{
  "foo": 4.4257052820783003
}

Actual Output:

{
  "foo": 4.4257052820783
}

The diff from expected is:

 {
-  "foo": 4.4257052820783003
+  "foo": 4.4257052820783
 }

Version Info

  • pre-commit: 2.19.0
  • pre-commit-hooks: v4.2.0
  • Python: 3.8.8
  • OS: Debian 11

Discussion

This might be something that has to be fixed within the python builtin json package. A custom JSON encoder/decoder that wraps things using decimal.Decimal might work too.

I've created a test case for this. See my high-precision-numbers branch or the diff.

I'll see if I have time to actually fix this, but I don't expect to :frowning_face:.

dougthor42 avatar Jun 07 '22 18:06 dougthor42

yeah I suspect this isn't really fixable due to json -- but feel free to take a stab at it

asottile avatar Jun 07 '22 19:06 asottile

As @asottile said the problem is in json standard library. image Therefore, this issue isn't fixable in the pre-commit hook and I advice to close this issue.

liortct avatar Aug 17 '22 20:08 liortct

Hi, I have a PR #818 to solve this issue. What do you think about it, can you check it please? @dougthor42 @asottile I've already included the tests for this from @dougthor42's diff

SilverTux avatar Oct 06 '22 16:10 SilverTux

@asottile How would you feel about a custom json encoder/decoder that handles floats using decimal.Decimal?

json.loads also has a parse_float arg that might be usable instead or in addition.

dougthor42 avatar Oct 07 '22 19:10 dougthor42

Python has no built-in arbitrary-precision floats. Here is an example:

>>> float(4.4257052820783003)
4.4257052820783

So it doesn't matter what you use, you can't have a float object with arbitrary precision.

riccardobucco avatar Oct 10 '22 14:10 riccardobucco