Issue With Program

You enter decimal number into the program and what base you want. The integer part of the decimal is being handled fine, but the decimal is not.

For example, I enter 15.6847 and base 10, which means I'm going from base 10 to base 10. It spits out 68469999999999 for the decimal part. (Do not worry about the first block of numbers. The second block seperated from the first by a space is where the decimal will appear in order.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <string>
#include <math.h>

using namespace std;

int baseConverter(int, int, int *, int *);
int decConverter(double, int, int*, int*);

int main()
{
    int base;
    int i;
    double num;
    double dec;
    int *counter;
    int *conversion;
    int *decConversion;
    
    counter = new int[2];
    conversion = new int[15];
    decConversion = new int[15];
    
    *counter = 0;
    *(counter + 1) = 0;
    
    
    cout << "Enter a number: ";
    cin >> num;
    cout << endl;
    
    dec = num - static_cast<int>(num);
    num = num - dec;
    
    cout << "Enter base: ";
    cin >> base;
    cout << endl;
    
    *conversion = baseConverter(static_cast<int>(num), base, counter, conversion + 1);
    *decConversion = decConverter(dec, base, counter + 1, decConversion + 1);
    
    for (i = 0; i < 15; i++)
         {
         cout << *(conversion + i) << endl;
         }
    
    cout << endl << endl;
    for (i = 0; i < 15; i++)
         {
         cout << *(decConversion + i) << endl;
         }
    cout << *(counter + 1);
    system("pause");    
    
    return 0;
}

int baseConverter(int num, int base, int *counter, int *conversion)
{
    if ((num / base) >= 1)
         {
         *conversion = baseConverter(static_cast<int>(num / base), base, counter, conversion + 1);
         *counter = *counter + 1;
         }
    return num%base;
}

int decConverter(double dec, int base, int *counter, int *decConversion)
{
    if((*counter < 15) && (dec != 0))
         {
         *counter = *counter + 1;
         *decConversion = decConverter((dec * base) - static_cast<int>(dec*base), base, counter, decConversion + 1);
         }
    return static_cast<int>(dec*base);
}
Hi!

You should be aware that double and float variables are not always represented exactly. Strictly speaking, only those values could be represented exactly, which have finite (and short) representation in binary system, i.e. 0.5, 0.25, 1.375 etc.

In your case it is obvious that num is slight different from what you entered at the beginning. It is normal - just read something about floating point numbers.
For some reason, this statement:

(dec * base) - static_cast<int>(dec*base)

is being evaluated as "1" when it should be zero when the decimal is only to the tens place.
Topic archived. No new replies allowed.