Saturday, November 14, 2026
HomeC ProgrammingFractions of the Binary Form

Fractions of the Binary Form


I’ve written about binary fractions earlier than, although to check how they’re saved in an actual quantity requires extra depth (see final week’s Lesson). Primarily, binary fractions are simply unfavorable powers of two.

Determine 1 is from my authentic binary fraction put up about ten years in the past. It’s additionally one of many first hits you see on Google when trying to find binary fractions, which amuses me.

Determine 1. Adverse powers of two used to signify fractions in a binary quantity.

First, some ugly math: 2-1 is similar as ½ or 0.5. Should you take 2-2 it’s the identical as 1/2×2 or ¼ or 0.25. The identical sample holds true for values cascading alongside the fractional binary path, as illustrated in Determine 1.

Second, to create a binary values for decimals apart from powers of two, you have to mix fractional binary values. This technique mirrors how integers are created with binary, although for fractions it’s harder to be exact.

For instance, the integer worth 102 is written as 01100110 in binary. That’s 0×128 + 1×64 + 1×32 + 0×16 + 0×8 + 1×4 + 1×2 + 0×1, or 64 + 32 + 4 + 2, which equals 102.

Now take the worth 0.375. In binary this worth is written as 0.011. That’s 0×1.0 (the primary digit), decimal, 0×0.5 + 1×0.25 + 1×0.125, or 0.25 + 0.125, which equals 0.375.

To translate a decimal fraction right into a binary fraction you have to peel off every unfavorable energy of two (2-n) till you get the right worth — or no less than shut sufficient to it. This “shut sufficient” is the precision, which is part of each actual quantity saved in a pc.

The next code prompts for a fraction, any worth between zero and optimistic one. The worth enter, an actual quantity, is manipulated to acquire the binary digits for unfavorable powers of two, with the outcome output as a string.

2026_11_14-Lesson.c


#embody <stdio.h>
#embody <stdlib.h>

int principal()
{
    int x;
    float f;

    
    printf("Enter a optimistic decimal: ");
    scanf("%f",&f);
    if( f>=1.0)
    {
        places("Enter a worth 0.0 to 1.0");
        return(-1);
    }

    
    printf("0.");
    for( x=0; x<16; x++ )
    {
        f *= 2.0;
        if( f==0 )
            break;
        if( f>=1.0 )
        {
            putchar('1');
            f -= 1.0;
        }
        else
            putchar('0');
    }
    putchar('n');

    
    return 0;
}

After confirming enter, the fraction is processed for 16 unfavorable powers of two:

for( x=0; x<16; x++ )

For every iteration, the worth of variable f (the fraction enter) is doubled:

f *= 2.0;

The impact is to undo the unfavorable energy of two by elevating the worth by an influence by two. So 2-1 turns into 20, or one. When the worth of f at this level is zero, no extra fractional portion of the worth is accessible for processing, so the loop ends:

if( f==0 )
    break;

In any other case, when the worth f is larger than zero, a binary digit has been discovered. The a ‘1’ is output and the worth of f is decreased by 1.0:

if( f>=1.0 )
{
    putchar('1');
    f -= 1.0;
}

When the worth of f is just not better than 1.0, and it’s not but zero, no binary digit was discovered. A ‘0’ is output:

else
    putchar('0');

The method continues till all 16 powers of unfavorable two have been processed, or the worth of variable f is the same as zero.

Here’s a pattern run for the worth 0.375:

Enter a optimistic decimal: 0.375
0.011

The outcome output is described earlier on this put up: 0.25 + 0.125.

Listed below are a couple of extra runs:

Enter a optimistic decimal: 0.5
0.1

The worth 0.5 is 0.1 binary, 2-1.

Enter a optimistic decimal: 0.1
0.0001100110011001

The worth 0.1 is a type of decimals that may’t be represented exactly, although the result’s shut. On this case, it might be fascinating to see how 0.1 is introduced within the binary32 format initially discovered within the floating level worth f. I discover how this comparability can occur in subsequent week’s Lesson and the way my method right here will not be good.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments