1

Hi I have following float point value in base 10: 0.625. I need to convert this value in base 10 to binary format which is: 0.101. Algorithm I found is below. It works, but I am not understanding why this works. Could someone please explain to be why the code below works? I am aware that numbers after the decimal point is computed in the manner such that 1/2^n where n is count from the decimal point. Thanks.

To clarify, I need to know the reasoning behind the mathematical formula. Not stepping through the code.

private static String floatToBinaryString( double n ) {
    String val = "0.";
    while ( n > 0 ) {
        double r = n * 2;
        if( r >= 1 ) {
            val += "1";
            n = r - 1;
        }else{
            val += "0";
            n = r;
        }
    }
    return val;
}
4
  • It doesn't really look like you have it in base 10 Commented Jan 27, 2014 at 18:02
  • I suggest you step through the code in your debugger to see what each line of code does. Commented Jan 27, 2014 at 19:06
  • I know what each line of code does, I already said it works. I want to know why it works. Whats the reasoning behind the mathematical formula. Commented Jan 27, 2014 at 20:13
  • You could probably have left all the code out of the question and worded it something like this... "Whats the reasoning behind the mathematical formula for converting base 10 fractions to base 2?". Commented Jan 27, 2014 at 21:53

1 Answer 1

3

You multiply the fraction by 2 and use the ones place digit as the binary values until the fraction is equal to zero. Example below.

This is the standard formula for conversion using the 0.625 you have:

1) Multiply fraction by 2 =>  0.625 * 2 = 1.25
    The digit to the left of the decimal point is the first binary value, 0.1 so far
2) Ignore the ones-place digit and you have 0.25 which is still larger than zero.
    Multiply the fraction by 2 => 0.25 * 2 = 0.50
    The digit to the left of the decimal point is the next binary value, 0.10 so far
3) Ignore the ones-place digit and you have 0.50 which is less than zero.
    Multiply the fraction by 2 => 0.5 * 2 = 1.00
    The digit to the left of the decimal point is the next binary value, 0.101 so far
4) Ignore the ones-place digit and you have 0.00 which is equal to zero.
    Conversion complete!

private static String floatToBinaryString( double n ) {
    String val = "0.";    // Setting up string for result
    while ( n > 0 ) {     // While the fraction is greater than zero (not equal or less than zero)
        double r = n * 2;   // Multiply current fraction (n) by 2
        if( r >= 1 ) {      // If the ones-place digit >= 1
            val += "1";       // Concat a "1" to the end of the result string (val)
            n = r - 1;        // Remove the 1 from the current fraction (n)
        }else{              // If the ones-place digit == 0
            val += "0";       // Concat a "0" to the end of the result string (val)
            n = r;            // Set the current fraction (n) to the new fraction
        }
    }
    return val;          // return the string result with all appended binary values
}
Sign up to request clarification or add additional context in comments.

2 Comments

My question is why "Multiply current fraction (n) by 2 ". I know all the steps, it is intuitive. My question is the reasoning behind the mathematical formula.
Apologies. The phrase in your question "Algorithm I found is below. It works, but I am not understanding why this works. Could someone please explain to be why the code below works?" led me to believe you didn't understand how the code was working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.