Skip to content

Commit

Permalink
Fix floating point precision
Browse files Browse the repository at this point in the history
  • Loading branch information
ijl committed Apr 19, 2019
1 parent 228a159 commit bbf518e
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 18 deletions.
66 changes: 54 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ classifier = [
[dependencies]
encoding_rs = { version = "0.8", default_features = false, features = ["simd-accel"] }
itoa = { version = "0.4", default_features = false }
pyo3 = { git = "https://github.com/PyO3/pyo3.git", rev = "dc2fc80f4aeb89dd645a20620b292bfea4756228", features = ["extension-module"]}
pyo3 = { git = "https://github.com/PyO3/pyo3.git", rev = "77f0974dafb8819b95437e74da581d7d9c41a1ad", features = ["extension-module"]}
serde = { version = "1", default_features = false }
serde_json = { version = "1", default_features = false }
serde_json = { git = "https://github.com/ijl/json.git", rev = "2c0c55b628c9177543283a1535549e1dcb6a870a", default_features = false, features = ["num_lexical"] }
smallvec = { version = "0.6", default_features = false, features = ["union"] }

[profile.release]
Expand Down
2 changes: 1 addition & 1 deletion test/test_jsonchecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
_path = os.path.join(os.path.dirname(__file__), '..', 'data', 'jsonchecker')


PATTERN_1 = '["JSON Test Pattern pass1",{"object with 1 member":["array with 1 element"]},{},[],-42,true,false,null,{"integer":1234567890,"real":-9876.54321,"e":1.23456789e-13,"E":1.2345678900000002e34,"":2.3456789011999997e76,"zero":0,"one":1,"space":" ","quote":"\\"","backslash":"\\\\","controls":"\\b\\f\\n\\r\\t","slash":"/ & /","alpha":"abcdefghijklmnopqrstuvwyz","ALPHA":"ABCDEFGHIJKLMNOPQRSTUVWYZ","digit":"0123456789","0123456789":"digit","special":"`1~!@#$%^&*()_+-={\':[,]}|;.</>?","hex":"ģ䕧覫췯ꯍ\uef4a","true":true,"false":false,"null":null,"array":[],"object":{},"address":"50 St. James Street","url":"http://www.JSON.org/","comment":"// /* <!-- --","# -- --> */":" "," s p a c e d ":[1,2,3,4,5,6,7],"compact":[1,2,3,4,5,6,7],"jsontext":"{\\"object with 1 member\\":[\\"array with 1 element\\"]}","quotes":"&#34; \\" %22 0x22 034 &#x22;","/\\\\\\"쫾몾ꮘﳞ볚\uef4a\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?":"A key can be any string"},0.5,98.6,99.44,1066,10.0,1.0,0.1,1.0,2.0,2.0,"rosebud"]'.encode('utf-8')
PATTERN_1 = '["JSON Test Pattern pass1",{"object with 1 member":["array with 1 element"]},{},[],-42,true,false,null,{"integer":1234567890,"real":-9876.54321,"e":1.23456789e-13,"E":1.23456789e34,"":2.3456789012e76,"zero":0,"one":1,"space":" ","quote":"\\"","backslash":"\\\\","controls":"\\b\\f\\n\\r\\t","slash":"/ & /","alpha":"abcdefghijklmnopqrstuvwyz","ALPHA":"ABCDEFGHIJKLMNOPQRSTUVWYZ","digit":"0123456789","0123456789":"digit","special":"`1~!@#$%^&*()_+-={\':[,]}|;.</>?","hex":"ģ䕧覫췯ꯍ\uef4a","true":true,"false":false,"null":null,"array":[],"object":{},"address":"50 St. James Street","url":"http://www.JSON.org/","comment":"// /* <!-- --","# -- --> */":" "," s p a c e d ":[1,2,3,4,5,6,7],"compact":[1,2,3,4,5,6,7],"jsontext":"{\\"object with 1 member\\":[\\"array with 1 element\\"]}","quotes":"&#34; \\" %22 0x22 034 &#x22;","/\\\\\\"쫾몾ꮘﳞ볚\uef4a\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?":"A key can be any string"},0.5,98.6,99.44,1066,10.0,1.0,0.1,1.0,2.0,2.0,"rosebud"]'.encode('utf-8')


def _read_jsonchecker_file(filename):
Expand Down
27 changes: 24 additions & 3 deletions test/test_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,35 @@ def test_float(self):
self.assertEqual(1.893, orjson.loads("1.893"))
self.assertEqual(1.3, orjson.loads("1.3"))

@pytest.mark.xfail
def test_float_precision(self):
def test_float_precision_loads(self):
"""
float precision
float precision loads()
"""
self.assertEqual(orjson.loads("31.245270191439438"), 31.245270191439438)
self.assertEqual(orjson.loads("121.48791951161945"), 121.48791951161945)

def test_float_precision_dumps(self):
"""
float precision dumps()
"""
self.assertEqual(orjson.dumps(31.245270191439438), b'31.245270191439438')
self.assertEqual(orjson.dumps(121.48791951161945), b'121.48791951161945')

def test_float_edge(self):
"""
float edge cases
"""
self.assertEqual(orjson.dumps(0.8701), b'0.8701')

self.assertEqual(orjson.loads("0.8701"), 0.8701)
self.assertEqual(orjson.loads("0.0000000000000000000000000000000000000000000000000123e50"), 1.23)
self.assertEqual(orjson.loads("0.4e5"), 40000.0)
self.assertEqual(orjson.loads("0.00e-00"), 0.0)
self.assertEqual(orjson.loads("0.4e-001"), 0.04)
self.assertEqual(orjson.loads("0.123456789e-12"), 1.23456789e-13)
self.assertEqual(orjson.loads("1.234567890E+34"), 1.23456789e+34)
self.assertEqual(orjson.loads("23456789012E66"), 2.3456789012e+76)

def test_float_notation(self):
"""
float notation
Expand Down

0 comments on commit bbf518e

Please sign in to comment.