Pointer to convert fraction to binary
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Pointer to convert fraction to binary

1. Junior Member
Join Date
Jan 2010
Posts
1

## Pointer to convert fraction to binary

Hi,
I am using a function which takes a floating point fraction and converts it to binary upto 24 sig fig.I am passing a pointer pointing to an array (where the binary will be stored)to the function and then increasing the address by 1 (pointer arithmetic allows to point the variable to the next entry of the array).But when I run the code it seems the only one item gets copied into the array.
Here is my code:
/*this program converts a fractional decimal to binary e,g 0.625 should give .1010000000000000 upto 22 sig fig*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char myMantissa[25]; /* where the binary number is stored*/
char *fractiontoBinary(double a,char *b){
for(int i=0;i<22;i++)
{
if(a*2>=1)
{
*b='1';
b=b+1;
a=(a*2)-1;
}
else
{
*b='0';
b=b+1;
a=a*2;}
}

return b;
}

int main (void)
{double input;
scanf("%d",&input);
char *s= fractiontoBinary(input,myMantissa);

myMantissa[23]='\0';
printf("%d",myMantissa[1]);
return 0;
}

2. cg1
Junior Member
Join Date
Jan 2010
Posts
4

## Re: Pointer to convert fraction to binary

It looks like you are missing a closing curly brace. So...how did this even compile? BTW...this is a C-Sharp Programming forum. It looks like this is C code.

3. ## Re: Pointer to convert fraction to binary

You are only displaying Mantissa[1], that is one character. You need to pass a pointer to the start of the array in the printf statement as follows...

printf("&#37;s", myMantissa);

I recoded this in C# for the benfit of others in this forum, but unless my palgarisum is bad, and I'm an expert at it so ... it seems the algorithm does not work?

Not the best C# code, but just tried to match it to the exact C code given for the benefit of the author.
Code:
```public Foo()
{
String b;
b = FractionToBinary(0.625, out b);  // returns all '1's
}

private String FractionToBinary(Double a, out String b)
{
b = String.Empty;

for (Int32 i = 0; i < 22; i++)
{
if (a * 2 >= 1)    // always true...  and 'a' only increases in size
{
b += '1';
a = (a * 2) + 1;  // should this be divide not multiply?
}
else
{
b += '0';
a *= 2;  // does not work even with divide in both cases?
}
}

return b;
}```
Last edited by rliq; January 18th, 2010 at 11:59 PM.

4. cg1
Junior Member
Join Date
Jan 2010
Posts
4

## Re: Pointer to convert fraction to binary

Originally Posted by rliq
You are only displaying Mantissa[1], that is one character. You need to pass a pointer to the start of the array in the printf statement as follows...

printf("%s", myMantissa);

I recoded this in C# for the benfit of others in this forum, but unless my palgarisum is bad, and I'm an expert at it so ... it seems the algorithm does not work?

Not the best C# code, but just tried to match it to the exact C code given for the benefit of the author.
Notice that rliq used "%s" as a format specifier in his printf statement which is very different from "%d". The "%s" tells the printf function to read bytes from an array until a null byte is encountered. The %d on the other hand tells the printf function to read a value as an integer. So the binary representation of your character at myMantissa[1] (e.g. '1') would be interpreted as the ASCII value not the character you intended. I also noticed that I was wrong about the missing curly brace. Sorry about that.

5. ## Re: Pointer to convert fraction to binary

For those interested, here is a C# function that will take a base 10 Double N (0.0 < N < 1.0) and return the Binary equivelent to a maximum number of decimal places.
Code:
```using System;

namespace BinFract
{
class Program
{
static void Main(string[] args)
{
String binary1 = DoubleToBinary(0.625, 22);

String binary2 = DoubleToBinary(Math.PI - 3.0, 100);

// binary1 = 0.101  (as expected)

// binary2 = 0.001001000011111101101010100010001000010110100011

// so PI = 11.001001000011111101101010100010001000010110100011... ;)
}

//  Decimal     Binary
//      4       100
//      2        10
//      1         1
//      0.5       0.1       // dividing the decimal by 2, moves the binary '1', one place to the right,
//      0.25      0.01      // same as dividing it by 10 in base10 (1000, 100, 10, 1, 0.1, 0.01, 0.001...)
//      0.125     0.001
// etc
//
// So in decimal 0.625 = 0.5 + 0.125.  Therefore in Binary = 0.1 + 0.001 = 0.101

/// <summary>
/// Return the binary of a Base 10 number to a maximum number of decimal places
/// </summary>
/// <param name="base10">The number to be converted (must be less that 1.0)</param>
/// <param name="decimalPlaces">The maximum number of decimal places</param>
/// <returns>A string containing the binary representation</returns>
private static String DoubleToBinary(Double base10, Int32 decimalPlaces)
{
// divided by 2 (because binary)
Double divisor = 1.0 / 2.0;

// this will hold the output
String binary = "0.";

for (Int32 i = 0; i < decimalPlaces; i++)
{
if (base10 >= divisor)
{
binary += '1';
base10 -= divisor;
}
else
binary += '0';

// divide by 2 (because binary)
divisor /= 2.0;
}

// trim the insignificant zero's
return binary.TrimEnd('0');
}
}
}```
Last edited by rliq; January 19th, 2010 at 06:10 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•