I want to read and write data in binary files using the IEEE 754 standards for floating point numbers. The scripts for reading half, single and double precision floats are done and I am now working on the scripts for writing. This is where i am stuck. I started with the easiest one, a half precision float where the first bit should be the sign bit, 0 for positive and 1 for negative. The next five bits are the exponent and the last 10 bits are the fraction. I just don't know what mathematical steps i have to do to extract the exponent and convert the decimal value to a binary value. What I have so far:
{
//Write half precision float
//argument0: file
//argument1: position
//argument2: decimal
var s, i, f, e;
if sign(argument2) = -1
{
s = 1;
}
else
{
s = 0;
}
i = floor(abs(argument2));
f = frac(argument2);
file_bin_seek(argument0,argument1);
file_bin_write_byte(argument0,(s << 7) | (e << 2) | (f >>8));
file_bin_write_byte(argument0,f & 255);
}
Edited by Marten Troost, 15 February 2011 - 05:20 PM.











