floating point calculation inaccurate?

K

Klaus Bonadt

I have found strange behaviour in casting floating point values in C++ 6.0
to int:

If I enter in the watch window while debugging in version 6.0 the following
term:
(1.4 - 1.0) * 10.0
the result will be 4.0 - as expected.

But if I cast the same to int:
(int)((1.4 - 1.0) * 10.0)
the result will be 3!

I tried the same with C# in .NET:
(1.4 - 1.0) * 10.0
will be evaluated differently, sometimes 4.019999999991, sometimes
3.9999999999999991.
In according to the different values for the floating point calculation, the
cast to int:
(int)((1.4 - 1.0) * 10.0)
will yield into results 3 or 4, respectively.

Using floor and ceil or Math.Floor and Math.Ceiling does not change
anything, because the faulty evaluation will be done at first.

OK, I understand that floating point calculation must have limited
preciseness. However, I am surprised that this simple term is evaluated
wrongly.

How to overcome this in daily practice?
Never go back from double to int?
Using other rounding algorithm?

What do you do?
Regards,
Klaus
 
L

Le Géant Vert

Klaus Bonadt said:
I have found strange behaviour in casting floating point values in C++ 6.0
to int:

If I enter in the watch window while debugging in version 6.0 the following
term:
(1.4 - 1.0) * 10.0
the result will be 4.0 - as expected.

But if I cast the same to int:
(int)((1.4 - 1.0) * 10.0)
the result will be 3!

I tried the same with C# in .NET:
(1.4 - 1.0) * 10.0
will be evaluated differently, sometimes 4.019999999991, sometimes
3.9999999999999991.
In according to the different values for the floating point calculation, the
cast to int:
(int)((1.4 - 1.0) * 10.0)
will yield into results 3 or 4, respectively.

Using floor and ceil or Math.Floor and Math.Ceiling does not change
anything, because the faulty evaluation will be done at first.

OK, I understand that floating point calculation must have limited
preciseness. However, I am surprised that this simple term is evaluated
wrongly.

How to overcome this in daily practice?
Never go back from double to int?
Using other rounding algorithm?

What do you do?
Regards,
Klaus

floating point numbers don't allow to represent all numbers, that's why you
may have some inaccuracy when casted into integer values. Thus, all floating
point computations are consistent, just avoir casting to int as much as
possible, and use an "epsilon" to test double values.
 
C

Carl Daniel [VC++ MVP]

Klaus said:
I have found strange behaviour in casting floating point values in
C++ 6.0 to int:

If I enter in the watch window while debugging in version 6.0 the
following term:
(1.4 - 1.0) * 10.0
the result will be 4.0 - as expected.

But if I cast the same to int:
(int)((1.4 - 1.0) * 10.0)
the result will be 3!

I tried the same with C# in .NET:
(1.4 - 1.0) * 10.0
will be evaluated differently, sometimes 4.019999999991, sometimes
3.9999999999999991.
In according to the different values for the floating point
calculation, the cast to int:
(int)((1.4 - 1.0) * 10.0)
will yield into results 3 or 4, respectively.

Using floor and ceil or Math.Floor and Math.Ceiling does not change
anything, because the faulty evaluation will be done at first.

OK, I understand that floating point calculation must have limited
preciseness. However, I am surprised that this simple term is
evaluated wrongly.

It's not wrong, it's just the nature of floating point calculations.
How to overcome this in daily practice?
Never go back from double to int?
Using other rounding algorithm?

Learn how to round floating point calculations properly. For example, when
converting to int, add 0.5 and then truncate (assuming you want round
towards positive infinty - adjust as needed for other rounding models).

-cd
 
G

Guest

Use the Convert class to round. If you cast you just get the integer part
ofcourse.

Convert class does the rounding for you.
 
J

Jon Skeet [C# MVP]

OK, I understand that floating point calculation must have limited
preciseness. However, I am surprised that this simple term is evaluated
wrongly.

In that case I don't think you *really* understand how floating point
calculations work.

See http://www.pobox.com/~skeet/csharp/floatingpoint.html
How to overcome this in daily practice?

Well, you could use the decimal type if that's really important to you.
Most of the time if you're using a double you shouldn't care too much
about the *exact* value anyway.
Never go back from double to int?

Never assume that your double value is the same as you'd get
algebraically, and if you want to compare values, use some sort of
"nearness" constraint.
 
K

Klaus Bonadt

I tried the same with C# in .NET:
(1.4 - 1.0) * 10.0
will be evaluated differently, sometimes 4.019999999991, sometimes
3.9999999999999991.
In according to the different values for the floating point calculation, the
cast to int:
(int)((1.4 - 1.0) * 10.0)
will yield into results 3 or 4, respectively.

Thanks for your answers.
What I'm still not understand, is the fact that the floating point
arithmetic is indeterministic (at least using .NET DevStudio debugger.
I understand, I can't expect precise values even for very simple terms.
However, I would like to see same results for same operations.

Is floating point arithmetic in C++ 6.0 deterministic?

Regards,
Klaus
 
J

Jon Skeet [C# MVP]

Klaus Bonadt said:
What I'm still not understand, is the fact that the floating point
arithmetic is indeterministic (at least using .NET DevStudio debugger.
I understand, I can't expect precise values even for very simple terms.
However, I would like to see same results for same operations.

It *is* deterministic, but I believe the debugger probably uses
different rules in various ways. Things you need to consider:

o The output format used can make a difference - if the debugger
chooses to display only 3 decimal places but normal output displays
more, you'll see differences.

o The default precisions used - the debugger may interpret 1.0 as a
float rather than a double, for instance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top