Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

writing a float only uses two digets after the decimal #33

Closed
tmccombs opened this issue Jan 19, 2016 · 2 comments
Closed

writing a float only uses two digets after the decimal #33

tmccombs opened this issue Jan 19, 2016 · 2 comments

Comments

@tmccombs
Copy link

If you encode a float with to-json, only two digits after the decimal are kept, and scientifc notation is never used for very large numbers. Ex:

(jonathan:to-lisp 0.33333334) => "0.33"
(jonathan:to-lisp 1e30) => "1000000000000000000000000000000.00"

I'm sure there was a good reason for doing this, since there was commit that just added a special %to-json implementation for floats that used (format nil "~$" number). But it is difficult to work around if you want more than 1/100th in accuracy in your json. And it is inconsistant with ratios:

(jonathan:to-lisp 1/3) => "0.33333334"

I don't know what the reason behind using "~$" to output the number was, but at the very least the number of digits should be configurable.

@fukamachi
Copy link
Contributor

fukamachi commented Oct 16, 2016

I found this issue too while encoding Latitude and Longitude, which are should be decimal.

(jojo:to-json 35.65910807942215d0)
;=> "35.66"

According to the document, ECMAScript's Number can represent double-precision floating-point, so rounding the Common Lisp values doesn't make sense.
http://www.ecma-international.org/ecma-262/5.1/#sec-8.5

FYI, CL-JSON uses ~F format directive for encoding real values.

(cl-json:encode-json 35.65910807942215d0 nil)
;=> "35.65910807942215"

https://github.com/hankhero/cl-json/blob/6dfebb9540bfc3cc33582d0c03c9ec27cb913e79/src/encoder.lisp#L410-L422

@rudolph-miller
Copy link
Owner

Fixed via fba9d5e

Thanks! :octocat:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants