Here is my code. I have a problem with the decimals when it comes to "temp /= 100.0".

montant = 61;
for (int i=0 ; i<7 ; i++)
{
    if (i != posCreancier)
    {
        temp = montant/7.0;                  (temp == 8.7142857142857135)
        temp *= 100;                         (temp == 871.42857142857133)
        temp += 0.5;                         (temp == 871.92857142857133)
        temp = floor(temp);                  (temp == 871.00000000000000)
        temp /= 100.0;                       (temp == 8.7100000000000009)  Wtf with that "9" appearing ?!?
        tabDettes[posCreancier][i] = temp;
    }
}

Recommended Answers

All 4 Replies

One of the "problems" with floating point is that many numbers can not be represented exactly as you have found out. It's due to the way floats/doubles are represented in memory. A full explanation is given here.

Is there a way to avoid this problem or another way to limit the number of decimals?

Not really. There are many threads on the net about that, here's one about rounding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.