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

Claims that 1*2^16*1 is equal to infinity. #11

Open
cnordenb opened this issue Apr 21, 2024 · 2 comments
Open

Claims that 1*2^16*1 is equal to infinity. #11

cnordenb opened this issue Apr 21, 2024 · 2 comments

Comments

@cnordenb
Copy link

0 11111 0000000000 in 16 bit float should read 65536. This web app claims it is infinity.

@evanw
Copy link
Owner

evanw commented Apr 21, 2024

I'm using the Float16Array polyfill from here: https://github.com/petamoriken/float16. So if there's a bug with that polyfill, you should report it there instead.

However, the behavior you are describing with 16-bit seems consistent with 32-bit and 64-bit, so it doesn't appear to be a bug:

  • 0 11111 0000000000 in 16-bit is infinity

    > console.log(new Float16Array(new Int16Array([0b0_11111_0000000000]).buffer)[0])
    Infinity
  • 0 11111111 00000000000000000000000 in 32-bit is infinity

    > console.log(new Float32Array(new Int32Array([0b0_11111111_00000000000000000000000]).buffer)[0])
    Infinity
  • 0 11111111111 0000000000000000000000000000000000000000000000000000 in 64-bit is infinity

    > console.log(new Float64Array(new BigInt64Array([0b0_11111111111_0000000000000000000000000000000000000000000000000000n]).buffer)[0])
    Infinity

@cnordenb
Copy link
Author

cnordenb commented Apr 21, 2024

Ah, I'm just learning computer arithmetic as a student and realised after looking more into the IEEE 754 that that is how infinity is represented as a special case but your tool didn't mention that and just reported 1* 2^16 * 1 = infinity so I got bamboozled.

If you ever feel like adding more to your tool I think it would be very helpful for new learners if there was some sort of asterisk for the special cases explaining that the IEEE 754 format has decided to represent eg. infinity with those particular bits.

Thank you for making this, it's very helpful getting familiarised with how the float works in an interactive way!

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