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

small numbers a convert to zero if smaller than 1e-9, big numbers are strip to 1e68 when convert to string #1489

Closed
jbgagnon opened this issue Aug 10, 2018 · 7 comments

Comments

@jbgagnon
Copy link
Contributor

so we can't do any physics ...

to test:
console.log(1e-9) write 0
'x:" + 1e-9 write "x:0"
JSON.stringify({x:1e-9}) return ="{"a":0}"
Math.pow(2,-32) says 0
('' + 1e72).length says 69
JSON.stringify({x:1e80}) is also wrong so can't be communicate to an external service
console.log(1e100/3) write something not enough long ( or two long 3.333333333333333e+99 is the good answer)

the bug is in
file: jsutils.c
function: ftoa_bounded_extra

it must test if exponent must be added for example if
val < 1e-9 or val > 1e64 (I don't really know the actual limits)
but double value have 17 chars of precision so probably test val > 1e17 (it also depends of buffer length, normaly a buffer of 32 chars is good (sign + dot + 17 chars + e + sign + exponent max 3 char = 24 chars)

bug probably or some platform (esp32, esp8266, ...) we can use native library and smaller this code ... no ?

@gfwilliams
Copy link
Member

I think parsing floating point numbers works just fine. 0==1e-20 is false. It's just converting to Strings.

Custom double-to-string code was originally added because platform-specific versions pulled in large amounts of extra code - on some platforms it caused 1kB of RAM to be statically allocated. I believe JS may have some standards on how double to string code should behave as well (not that we currently adhere to those).

So I'd like to keep the implementation inside Espruino if only because it aids testing to always use the same implementation - but you're welcome to contribute an updated ftoa_bounded_extra function that'll use e for larger/smaller values if you want to.

@jbgagnon
Copy link
Contributor Author

jbgagnon commented Aug 10, 2018 via email

@gfwilliams
Copy link
Member

Any chance of doing this?

@jbgagnon
Copy link
Contributor Author

jbgagnon commented Oct 25, 2018 via email

@gfwilliams
Copy link
Member

Yes - best to ask on the forum. You'll have much better luck with Windows 10 Linux Subsystem or with a virtual machine running Linux though

@jbgagnon
Copy link
Contributor Author

jbgagnon commented Oct 25, 2018 via email

jbgagnon added a commit to jbgagnon/Espruino that referenced this issue Nov 30, 2018
@gfwilliams
Copy link
Member

Fixed with 1e6586a

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

2 participants