Invert GMLscripts.com

bin_to_fraction

Converts a string of binary digits to a fractional value.

val = bin_to_fraction("00100");     //  val == 0.12500
val = bin_to_fraction("10101");     //  val == 0.65625
val = bin_to_fraction("11111");     //  val == 0.96875
bin_to_fraction(bin)
Returns a decimal fraction converted from a binary string.
COPY/// @func   bin_to_fraction(bin)
///
/// @desc   Returns a decimal fraction converted from a binary string.
///
/// @param  {string}    bin         binary digits
///
/// @return {real}      decimal fraction
///
/// GMLscripts.com/license

function bin_to_fraction(bin)
{
    var fraction = 0;

    var i = 0;
    repeat (string_length(bin)) {
        if (string_char_at(bin, i+1) == "1") fraction += (1 / (2 << i));
        i++;
    } 

    return fraction;
}

Contributors: Bryan

GitHub: View · Commits · Blame · Raw