Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't convert 0.3 to IEEE754

I am trying to convert 0.3 to IEEE754 format. What I am getting is: 0011111010011001100110011001 1001, but what I should get is: 0011111010011001100110011001 1010. As you can see, the last 4 digits differ. I multiplied 0.3 with 2 until I found the period and then, after filling the mantisa with 001, I filled the rest of the mantisa with the period (1001). What am I doing wrong?

like image 903
Toma Radu-Petrescu Avatar asked Nov 24 '25 08:11

Toma Radu-Petrescu


1 Answers

You have to round, not truncate. At bit 25 the pattern continues: 10011001... Since bit 25 and beyond is > 1/2 ULP, you have to round up. That will give you the answer you are expecting.

like image 107
Rick Regan Avatar answered Nov 27 '25 01:11

Rick Regan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!