Click here to Skip to main content
14,575,110 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello

Im relatively new to C++ but i want to become quite good in it
and not just do it to pass some subject.

So now everyday im improving a piece of my program to try and learn more and not unlearn what
I have already learned.

So now im making a Decimal to Binary conversion but im not using any pre made "Libraries" or "Classes" otherwise I feel like I havent really done anything.

I already have a piece of code that works out the Binary for anything INfront of a dot " 'xxx'.xxx " but
now im working on the part after the dot.

So I took out my Digital book and checked up how to do it with the multiplication by 2 move and coded it so it works on some fractions quite well.
HowEver using that ends up in an infinite loop allot of the times because every time I get something like 0.60 and work it out I somewhere along the line end up with 0.60 again thus ending in an infinite loop.

I have no idea how to let the program sense a repetition in the string that im creating.

Improved Question here.
My Question is...What would be a neat method of making the program realize hes running into and Endless Loop?
Is there some way I can make it sense that its working out something that it already worked out and that it should stop?

Here is the Code so far.


#include <cstdlib>
#include <iostream>
#include <string>
#include <math.h>

using namespace std;
double a;

double fracvalue(double x) 
{
 int z;            
       z = (x/1);
       a = x-z;
        return (a);
       }




int main(int argc, char** argv) {
    double x;
    int z, c;    
    cin >> x;
    double a =fracvalue(x)*2;
    cout << fracvalue(x) << "      " << a  << endl;//Checking if A and fracvalue are giving correct readings 
    string Bin = ".";
    
    for (int = i=;i < 20;i++)  //<<-- THIS was while(a != 0.0) but it never seemed to 
                      //stop even when it gets to 0.0 also it doesnt write out the complete binary this way
                      //sometimes only leaving dots or partial BIN numbers.
    {
          if (a > 1)
          {
                Bin = Bin +'1';
                a = a-1;
                cout << a << endl;
                a = a*2;
                cout << a << endl;}
                
             if (a < 1)
                {
                    Bin = Bin + '0';
                    a = a*2;
                    
                    }
                    cout << Bin << endl;
          }
          
          
          
    system("PAUSE");
    return 0;
}



For some reason it keeps on moaning about the line
z = (x/1); in the function fracvalue,the funny thing is I didnt touch this code :s it just didnt want to run anymore.

Anyway...It seems to give the right Binary if i insert large fractions but simple ones would come out only 0s or 1s and not both.
Larger ones however "Under while (a != 0.0)" will never end because it math itself loops infinitely.

Thanks
Posted
Updated 19-Nov-11 15:50pm
v4
Comments
Sergey Alexandrovich Kryukov 19-Nov-11 17:28pm
   
Not a question, not clear. Could you post some code sample of what you tried, explain what exactly is your problem? What exactly binary type do you need (say, double)? How a string is represented? The problem of parsing a decimal string into a numeric object is simple enough (yes, without any libraries, of course).

Please use "Improve question".
--SA
Yvan Rodrigues 19-Nov-11 19:11pm
   
Can't you just check if your string is more than n characters, and if so exit the loop?
Chuck O'Toole 19-Nov-11 22:33pm
   
z = (x/1);

is simply

z = (int)x;
Rate this:
Please Sign up or sign in to vote.

Solution 4

Hi,

you can convert a decimal to a binary as follows ,
40.25

for 40 the equivalent binary is 101000.

for the fraction part just use the following logic

0.25 x 2 = 0.5
0.5 x 2 = 1.0

from the answer take the integer part..

so the final answer will be (101000.01)

Regards,
Satheesh
   
v3
Rate this:
Please Sign up or sign in to vote.

Solution 1

Well the endless loops all come in different sizes depending on what value insert.
BTW

I made an ELSE statement between the 2 iFs in the MAIN function.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Since my 1st computer science class in 1966, there has always been a red flag in dealing with floating point numbers.

Never test for exact equality of two floating point numbers, especially if one of them is computed

Your original loop only ended when the computed value of "a" was precisely equal to "0.0". That's a red flag. Floating operations always leave some cumulative fractional part laying around.

There may be other things wrong with this code, I didn't try to debug it, only comment on the "loop ending condition"

--------------------------
OK, I ran your program and got this strange output. I have no idea what this program is supposed to do and have no idea what's wrong with it or what output you expect. It makes no sense to me at all.

123.456
0.456      0.912
.0
0.824
1.648
.01
0.648
1.296
.011
0.296
0.592
.01110
0.184
0.368
.0111010
.01110100
0.472
0.944
.0111010010
0.888
1.776
.01110100101
0.776
1.552
.011101001011
0.552
1.104
.0111010010111
0.104
0.208
.011101001011110
.0111010010111100
.01110100101111000
0.664
1.328
.011101001011110001
0.328
0.656
.01110100101111000110
0.312
0.624
.0111010010111100011010
0.248
0.496
.011101001011110001101010
.0111010010111100011010100
0.984
1.968
.01110100101111000110101001
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 3

Hm.. it seems to me that his code is supposed to convert decimal number to binary in 2 stages :
1 . Convert non-decimal part
2. convert decimal part (quote : "
Quote:
works out the Binary for anything INfront of a dot " 'xxx'.xxx " but
now im working on the part after the dot.


You implemented "some" algorithm that uses multiplication by 2 which , as far as I know, is used for converting from binary to decimal ...

My suggestion is to try :

1. Divide number by 2 and get int value like : int div = (x/2)
2. Find the remainder : int rmd = x-(div*2). Write down the remainder (0 or 1)
3. Follow these steps until you reach div = 1 ...

Your binary is all remainders read backwards ... simple example
Convert 17 to binary :
17 / 2 = 8 (remainder 1)
8 / 2 = 4 (remainder 0)
4 / 2 = 2 (remainder 0)
2 / 2 = 1 (remainder 0)
1 / 2 = 0 (remainder 1)
So , dec 17 = bin 10001

Hope this helps ...
   
v4
Comments
Chuck O'Toole 21-Nov-11 9:23am
   
ha, not a good example for showing printing out the numbers in reverse, 17 decimal = 1001 binary = a palendrome. Did you print it out forwards or backwards :)
RKnGl 21-Nov-11 11:45am
   
good one :) hate to edit it now and add another example to make it more useful,thus making Your comment obsolete . Thx for paying attention on us newbs , Sir :D ..
11 / 2 = 5 (1)
5 / 2 = 2 (1)
2 / 2 = 1 (0)
1 / 2 = 0 (1) .. so 11 in decimal equals 1011 in binary ..
anyway I left out one division there as 17 in decimal is 10001 yet it's still palindrome :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100