evanw / float-toy

Use this to build intuition for the IEEE floating-point format
155 stars 38 forks source link

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

Open cnordenb opened 6 months ago

cnordenb commented 6 months ago

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

evanw commented 6 months ago

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:

cnordenb commented 6 months ago

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!