yitzchak / shasht

Common Lisp JSON reading and writing for the Kzinti.
MIT License
46 stars 3 forks source link

issue with reading JSON floats. #14

Open jsulmont opened 1 year ago

jsulmont commented 1 year ago

Thanks for that neat lib!!

read-json-number seems to behave incorrectly:

(let ((cl:*read-default-float-format* 'double-float))
  (shasht:read-json "-3.14159"))

returns:

-3.1415900000000003d0

when

(let ((cl:*read-default-float-format* 'long-float))
  (read-from-string "-3.14159"))

returns:

-3.14159d0, 8

Am I missing anything here?

Thanks

hxzrx commented 5 months ago

Also: (shasht:write-json 14.0d0 nil) returns: "1.3999999999999999e1"

yitzchak commented 3 months ago

Reading and printing floats correctly, let alone quickly, is highly non-trivial. Currently, shasht delegates printing to CL:FORMAT with an attempt to cleanup the output a bit. For parsing it uses the CL implementation's coerce. To avoid some of the issues with both aspects we are working on a new Lisp system, Quaviver, which handles the difficult part of reading and printing, i.e base 10 conversion with some more modern algorithms then Burger-Dybvig, etc.

I've added a preliminary testing branch "quaviver" that uses these algorithms and it appears to pass the test cases given so far in this issue.

hxzrx commented 2 months ago

That works, thanks!

sabracrolleton commented 2 months ago

@yitzchak Do you have any suggested reading beyond the classic What Every Computer Scientist Should Know About Floating-Point Arithmetic?

yitzchak commented 2 months ago

@sabracrolleton I don't have any suggested reading for generic floating point operations/concerns yet. We do have some references for some of the stuff we are working on for Quaviver https://github.com/s-expressionists/Quaviver/wiki/References

Its not organized at all yet. If you have anything you think is worth adding please feel free to.