LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++ : converting to binary numbers... (https://www.linuxquestions.org/questions/programming-9/c-converting-to-binary-numbers-501045/)

jubaitca 11-12-2006 11:21 PM

C++ : converting to binary numbers...
 
This is my code to convert double numbers to binary:

Code:

#include <cmath>
#include <iostream>
 
using namespace std ;
int intbin(int n);
int fractbin(double n);
int main()
{
        double number;
        cout << "Please enter the number: ";
        cin >> number;
        double decimal;
        decimal = number - floor(number);
        intbin(static_cast<int>(number));
        cout << ".";
        fractbin(decimal);
        return 0;
}
 
 
int intbin(int n)
{
        int h;
        if (n/2 == 0)
        {
        cout << n%2;
        }
        else
        {
                h = n/2;       
                intbin(h);
                cout << n%2;
        }       
 
}
int fractbin(double n)
{
        if (n == 0)
        {
        cout << 0;
        }
        else if (2*n == 1.0)
        {
                cout << 1;
        }       
        else if (2*n < 1.0)
        {
                cout << 0;
                fractbin(2*n);
        }
        else
        {       
                cout << 1;       
                fractbin((2*n)-1);       
        }
}

My next task is to to convert numbers (double) that are greater than 2^31 -1 and less than 1
my teacher suggested i use the mantissa somehow
by using the frexp function
and the ldexp function

how would i update this code to do this?

Quigi 11-13-2006 10:59 AM

I'd use frexp to get mantissa and exponent. :study: man frexp (Not sure where ldexp comes in, but hey, [s]he's your teacher and it's your homework.)

You can probably build on your fractbin function, passing it the exponent so it can put the decimal point in the right place. You may not need intbin at all. Anyway, you'll have to distinguish three cases:
(A) Start with "0.00...0" for small numbers -- less than 1 or 0.5, depending on your design.
(B) Put the period ("decimal point", now rather binary point) somewhere within the mantissa for middle numbers.
(C) Finish with "000...0" for large numbers -- greater or equal 2**54, I think. (Why 54? According to my /usr/include/ieee754.h, my doubles have a mantissa stored in 32+20 bits, and there's a leading 1. Yours might differ, but I doubt it. But you'll have to work out the details.)

Extra Hints:
- Normally I'd write this using a for or while loop instead of recursion. It uses less computer resources (probably a minor concern of yours), and might be easier to understand. A C++ type trait should tell you ahead of time how many bits your mantissa has.
- See if your program works for negative numbers.
- Do you handle subnormal numbers correctly? Probably frexp is taking care of the details.

acid_kewpie 11-16-2006 07:14 AM

please do not ask for help with homework, it's for you to do, not us.


All times are GMT -5. The time now is 06:27 AM.