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

Emulator: Math.ceil(Math.sqrt(9)) isn't 3 #2262

Open
rigrig opened this issue Aug 29, 2022 · 11 comments
Open

Emulator: Math.ceil(Math.sqrt(9)) isn't 3 #2262

rigrig opened this issue Aug 29, 2022 · 11 comments

Comments

@rigrig
Copy link
Contributor

rigrig commented Aug 29, 2022

This only happens in the emulator

>Math.ceil(Math.sqrt(9))
=4

Expected: 3 (it is 3 in node/Firefox console)

(Maybe related to #2258?)

Emulator versions affected: 2v14.104, 2v18.1 (both Bangle.js 1 and Bangle.js 2)

@fanoush
Copy link
Contributor

fanoush commented Aug 30, 2022

interesting, just tried in webide bangle2 emulator

>Math.sqrt(9)
=3
>Math.ceil(3)
=3
>Math.ceil(Math.sqrt(9))
=4

@nitzanfarhi
Copy link
Contributor

Doesn't happen on ESP32
image

@mariusGundersen
Copy link
Contributor

I suspect this is related to #2245. Which version are you getting this strange result in?

@MaBecker
Copy link
Contributor

Doesn't happen on ESP32

Yep, because it uses a different math lib than the nRF devices.

@rigrig
Copy link
Contributor Author

rigrig commented Aug 31, 2022

Hmm, I noticed this some time ago, but forgot to report it, and only re-tested in it the emulator when I did remember. Seems to be fixed already though 🎉:

  • It happens in the emulator, which is running 2v14.104.
  • Doesn't happen on my actual Bangle.js 2 running 2v15.5.

@rigrig rigrig closed this as completed Aug 31, 2022
@philipandresen
Copy link

philipandresen commented Jun 7, 2023

I realize this issue is closed, but I just experienced this issue in the emulator. @rigrig did you close this under the assumption that once the emulator was updated to at least 2v15.5 it would be fixed there? It seems like the emulator is running 2v18 now and the issue persists.

Or is there a different repo where the issue is tracked for the emulator? It's not a blocker for me or anything, but wanted to point it out in case there was an assumption that it wasn't happening anymore. Right now the device and emulator behave differently in this regard.

@fanoush
Copy link
Contributor

fanoush commented Jun 7, 2023

BTW, someone mentioned on Discord similar issue "Math.abs(Math.floor(14/8)-(14/8)) == 0.75 is false while it should be true" I tried also in emulator and it fails there in the same way. When printed it is rounded which makes it more confusing

>Math.abs(Math.floor(14/8)-(14/8))
=0.75
>Math.abs(Math.floor(14/8)-(14/8)) - 0.75
=-1.11022302462e-16
>Math.abs(Math.floor(14/8)-(14/8)) == 0.75
=false
>14/8-1.75
=-2.22044604925e-16

In node.js it gives true.

It was a bit surprising that Emulator has same floating point 'bugs' as the device but I guess it is a good thing after all?

EDIT: when re-reading this issue it is not about generic floating point math precision issues but the Math.ceil giving wrong integer (even if the underlying issue is the same, sqrt(9) s not 3 but 3.00000something => ceil of that is indeed 4) so maybe this one can have different solution/workaround than the generic issue I mentioned. In that case sorry about spam.

@rigrig
Copy link
Contributor Author

rigrig commented Jun 7, 2023

Yups, I just assumed it would be fixed when the emulator was updated, but I was wrong :-(

@rigrig rigrig reopened this Jun 7, 2023
@rigrig rigrig changed the title Math.ceil(Math.sqrt(9)) isn't 3 Emulator: Math.ceil(Math.sqrt(9)) isn't 3 Jun 7, 2023
@gfwilliams
Copy link
Member

It's a tricky one - floating point maths is not exact, pretty much by definition. We're using standard IEEE-754 double precision floating point. If you did the same sum in C, I imagine you'd get the same answer.

So I guess we can modify ceil to subtract 0.0000001 from the number before doing ceil, and modify floor to add the same, but it seems like a hack that may cause more problems than it solves

@rigrig
Copy link
Contributor Author

rigrig commented Jun 8, 2023

I reopened it because it only affects the emulator: code working on the watch but breaking in the emulator seems a bit worrying.

For example: I briefly added a workaround in Javascript for this code, and got rid of it when updating the firmware fixed this bug.

I have no idea how the emulator works though: if it's just a completely different Javascript engine, that seems like a good enough explanation not to bother.

@gfwilliams
Copy link
Member

Well, it does use the same engine, just compiled to JS - you can in fact step through it using Chrome debug tools. I just breakpointed on ceil and I see 3.0000000000000004.

And breakpointing jswrap_math_sqrt shows it's receiving 9 exactly, and is returning 3.0000000000000004 - the issue seems to be due to our sqrt implementation which was done to save some flash space. I'm kind of surprised it's not an issue on Bangle.js though as they should be doing basically the same thing

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

7 participants