jsonlite icon indicating copy to clipboard operation
jsonlite copied to clipboard

toJSON: default value digits=4 is dangerous (can truncate data)

Open simon-anders opened this issue 5 months ago • 0 comments

The default parameter digits=4 can cause quite unexpected behaviour.

In the following example, we lose 2e-5, which is made zero, while both the larger and smaller numbers stay intact.

> jsonlite::toJSON( c( 2e-7, 2e-6, 2e-5, 2e-4, 2e-3 ) )
[2e-07,2e-06,0,0.0002,0.002] 

This here is also maybe not what one would expect:

> jsonlite::toJSON( (1:9)*1e-5 )
[1e-05,0,0,0,0,0.0001,0.0001,0.0001,0.0001] 

The issue arises because the smallest number representable in plain notation with 4 digits is 1e-4, but scientific notation (i.e., the one with e) is used only below 2e-5.

I would suggest setting the default value for digits to at least 5, or enforce that scientific notation is used as soon as the number is below 10^-digits

Curiously, by the way, the problem does not arise here:

> jsonlite::toJSON(10^(-6:0))
[1e-06,1e-05,0.0001,0.001,0.01,0.1,1] 

simon-anders avatar Sep 07 '24 00:09 simon-anders