Dividing and Multiplying a Double yields different results

I have this little tutorial program I am playing with in C++ with QT. It converts 2 currencies at once based on user input. And it keeps all three of them synced up, by monitoring the change in any one value and changing the other 2. It works now, but only if I give it conversion rates that can be successfully divided and then multiplied back to equal the exact same result. Because a change in one value will trigger the program to change the others. So the math has to work out on all of the equations.

The program was supposed to convert Integer to Hex to Binary which will always work out perfectly. I wanted to try to switch it to do currencies.

You can see in the code below. I set Dollar Euro at 1:2 so everything works perfect. If I change the dollar to Euro Conversion to 1.1 or to 3.0 the program will start behaving strange then crash, I am guessing this is because
((1 / 3)*3) is .999999 and = ((1/2)*2) is 1.0

Can anyone point me in the right direction to correct this?

A copy of my class implementation is below
Sorry there is a lot of wonky stuff in QT that is not really pure C++. I tried posting this on QT forums but the spam filter kept kicking it out probably because I mentioned currencies a bunch of times.

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
#include "ByteConverter.h"

ByteConverter::ByteConverter(QObject* parent) :
    QObject(parent)
{
}
void ByteConverter::setDol(const QString& newValue)
{
    bool ok;
    double num = newValue.toDouble(&ok);
    double eur = num * 2.0;
    double rub = num * 68.0;
    if (ok) {
        emit eurChanged(QString::number(eur));
        emit rubChanged(QString::number(rub));
    } else {
        emit eurChanged("");
        emit rubChanged("");
    }
}

void ByteConverter::setEur(const QString& newValue)
{
    bool ok;
    double num = newValue.toDouble(&ok);
    double dol = num / 2.0;
    double rub = ((num * 68.0) / 2.0);
    if (ok) {
        emit dolChanged(QString::number(dol));
        emit rubChanged(QString::number(rub));
    } else {
        emit dolChanged("");
        emit rubChanged("");
    }
}

void ByteConverter::setRub(const QString& newValue)
{
    bool ok;
    double num = newValue.toDouble(&ok);
    double dol = num / 68.0;
    double eur = ((num / 68.0) * 2.0) ;
    if (ok) {
        emit dolChanged(QString::number(dol));
        emit eurChanged(QString::number(eur));
    } else {
        emit dolChanged("");
        emit eurChanged("");
    }
}

Last edited on
The short answer is don't use floating point values to work with currency calculations. Use integers.
thanks but what if I want to count the pennies
Decide how accurate you want your conversions to be, and make the basic unit of your currency accordingly.

For example, you might decide that your basic unit is 1 millionth of a penny. You work entirely in int values, where "1" means "one millionth". You convert it back to whole pennies only for the purpose of displaying to the user.
Thanks. That sounds like a good solution
Topic archived. No new replies allowed.