Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would directly assemble the floating point number using its binary representation.</p> <p>Read in the number one character after another and first find all digits. Do that in integer arithmetic. Also keep track of the decimal point and the exponent. This one will be important later.</p> <p>Now you can assemble your floating point number. The first thing to do is to scan the integer representation of the digits for the first set one-bit (highest to lowest).</p> <p>The bits immediately following the first one-bit are your mantissa. </p> <p>Getting the exponent isn't hard either. You know the first one-bit position, the position of the decimal point and the optional exponent from the scientific notation. Combine them and add the floating point exponent bias (I think it's 127, but check some reference please).</p> <p>This exponent should be somewhere in the range of 0 to 255. If it's larger or smaller you have a positive or negative infinite number (special case).</p> <p>Store the exponent as it into the bits 24 to 30 of your float.</p> <p>The most significant bit is simply the sign. One means negative, zero means positive.</p> <p>It's harder to describe than it really is, try to decompose a floating point number and take a look at the exponent and mantissa and you'll see how easy it really is.</p> <p>Btw - doing the arithmetic in floating point itself is a bad idea because you will always force your mantissa to be truncated to 23 significant bits. You won't get a exact representation that way.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload