GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2019-01-25 16:37:29

lostdarkwolf
Member
Registered: 2015-11-19
Posts: 22

convert a real number to a binary float string, and back

The following scripts can convert a real number into a binary string that represents a floating-point number. Multiple bit depths are supported, as long as you know the format. 32-bit is default. Useful for file data.

///float_to_dec(binary_string, mantissa_bits*, hidden_mant_bit*, exp_bias*)
// * not required
//
// Converts a binary string representing a floating-point number, into a real number.
//
// binary_string     a binary string representing a floating-point number, string (binary sequence)
// fraction_bits     number of bits in the fraction portion of the float, real (integer)
// hidden_mant_bit   true if the first bit of the mantissa is hidden, real (binary)
// exp_bias          the bias of the exponent, real (integer)
//
// 32-bit float format is default.
//
/// GMLscripts.com/license

//result is a binary string
var var_arg_bin_str=argument[0];
var exp_bits=8;
if argument_count>1 exp_bits=argument[1];
var fraction_bits=23;
if argument_count>2 fraction_bits=argument[2];
var hidden_mant_bit=1;
if argument_count>3 hidden_mant_bit=argument[3];
var var_exp_bias=power(2,exp_bits-1)-1;
if argument_count>4 var_exp_bias=argument[4];

// get sign
var var_sign=power(-1,real(string_char_at(var_arg_bin_str,1)))

// get exponent
var var_exp=real(base_convert(string_copy(var_arg_bin_str,2,exp_bits),2,exp_bits+2))-var_exp_bias;

// get mantissa
var ii=0;
var var_mantissa=hidden_mant_bit;
repeat fraction_bits {
 if string_char_at(var_arg_bin_str,exp_bits+2+ii)='1' var_mantissa+=power(2,-(ii+1))
 ii+=1;
}

// return decimal
return (var_sign*var_mantissa*power(2,var_exp));
///dec_to_float(dec, mantissa_bits*, hidden_mant_bit*, exp_bias*)
// * not required
//
// Converts a real number into a binary string representing a floating-point number.
//
// dec               the number to convert into float, real
// fraction_bits     number of bits in the fraction portion of the float, real (integer)
// hidden_mant_bit   true if the first bit of the mantissa is hidden, real (binary)
// exp_bias          the bias of the exponent, real (integer)
//
// 32-bit float format is default.
//
/// GMLscripts.com/license

var var_arg_decimal=argument[0];
var exp_bits=8;
if argument_count>1 exp_bits=argument[1];
var fraction_bits=23;
if argument_count>2 fraction_bits=argument[2];
var hidden_mant_bit=1; // one is hidden
if argument_count>3 hidden_mant_bit=argument[3];
var var_exp_bias=power(2,exp_bits-1)-1;
if argument_count>4 var_exp_bias=argument[4];

// set sign
var return_string=string(!(min(sign(var_arg_decimal),0)+1));

var var_decimal_whole=floor(var_arg_decimal);

// convert integer of "dec" to binary.
var var_decimal_whole_bin=base_convert(string(var_decimal_whole),exp_bits+2,2);
var_decimal_whole_bin=string_copy(var_decimal_whole_bin,2,string_length(var_decimal_whole_bin)-1);
// set exponent
return_string+=base_convert(string(var_exp_bias+string_length(var_decimal_whole_bin)),exp_bits+2,2,exp_bits)
// set the mantissa
return_string+=var_decimal_whole_bin;

// convert decimal of "dec" to binary and finish the fraction bits
var var_decimal_fraction=var_arg_decimal-var_decimal_whole;
for (var ii=string_length(var_decimal_whole_bin)+hidden_mant_bit; ii<=fraction_bits; ii+=1;) {
 if var_decimal_fraction*2>1 return_string+="1"
 else return_string+="0"
 var_decimal_fraction=(var_decimal_fraction*2 mod 1);
}

return return_string;

Offline

Board footer

Powered by FluxBB