Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've been able to create a code that converts decimal numbers (whole numbers) to base 2 to 16 numbers, but i'm having trouble with the fractional part for quite a while.
C++
#include <iostream>
#include <stdio.h>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;
void toBase (int n, int base)
{
    string x = "0123456789ABCDEF";
    if (n > 0)
    {
      toBase(n / base, base);
        cout << x[n % base];
    }
}

int main()
{

    float num;
    int a;
    float fNum = 12.34;
    int iPart = (int)fNum;
    float fPart = fNum - (float)iPart;
    cout << "";
    cin >> num;
    cout << "";
    cin >> a;
    toBase(num, a);
    cout << ".";
    toBase(fNum, a);
    cout << endl;
 

    }


And these are the results (10.5 and 2 are the decimal number and the base, the second number is what i should get, the third number is what i get):

10.5 - 1010.1 - 1010.1100
2

3.827 - 10.2110222122 - 10.110
3

82.7593 - 101.6744681322 - 101.13
9

7218.8192 - 5472.90139A8535 - 5472.11
11

8273.8 - 26B8.C - 26B8.C
15

23517.75 - 5BDD.C - 5BDD.C
16

92.33271 - 1011100.0101010100 - 1011100.1100
2

4095.839 - 7777.6554426416 - 7777.14
8

29672.9311 - 13033220.3232113021 - 13033220.30
4

9999.987 - 270F.FCAC083126 - 270F.C
16

Only two of the ten test results are correct. I have a deadline in a few hours for the assignment, so how should i proceed, what should i add or change?
Posted
Comments
Philippe Mori 30-Apr-13 20:15pm    
You have already posted a similar question. In the future, update existing question when it is directly related to that question instead of posting a new one.

Homework I see... what you want to do is get the fractional portion (to the right of the ".") and keep multiplying by 2 and looking at the integer result.
 
Share this answer
 
You should uses a debugger and also a pen and a paper.

Do each step on the paper and trace the corresponding code. As soon as the result differ, you will have an idea of what was used and what you get.

The first problem with your code is that you always uses 12.34 for fNum... thus the fractional part is always 12 in base a.
For 10.5 and 2, you get: 12 = 1x8 + 1x4 + 0x2 + 0x1 = 1100

For 3.828 and 3, you get: 12 = 1x9 + 1x3 + 1x0 = 110

For 82.7593 and 9, you get: 12 = 1x9 + 3%9 = 13

For 7218.8192 and 11, you get: 11 = 1x11 + 1%11 = 11

For 8273.8 and 15, you get: 12 = 12%15 = C


As mentionned in your other duplicate question, one way to handle that would be to multiply the fractional part...

For 10.5 and 2, you get for the fractional part:
0.5 * 2 = 1 ; 1 % 2 => 1 ; no more fractional part


For 3.827 and 3, you get for the fractional part:
0.827 * 3 = 2.481 ; 2 % 3 => 2
0.481 * 3 = 1.443 ; 1 % 3 => 1
0.443 * 3 = 1.329 ; 1 % 3 => 1
0.329 * 3 = 0.987 ; 0 % 3 => 0
0.987 * 3 = 2.961 ; 2 % 3 => 2
0.961 * 3 = 2.883 ; 2 % 3 => 2
0.883 * 3 = 2.649 ; 2 % 3 => 2 
...
In that case, you would stop when you have desired precision...
 
Share this answer
 
I only have two suggestions for you. Look at logarithm rules, and use continued fractions.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900