468,510 Members | 1,755 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,510 developers. It's quick & easy.

floating point calculation inaccurate?

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
Nov 15 '05 #1
9 2043

"Klaus Bonadt" <Bo****@hotmail.com> a écrit dans le message de news:
e2**************@TK2MSFTNGP12.phx.gbl...
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.
Nov 15 '05 #2
Klaus Bonadt wrote:
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
Nov 15 '05 #3
Use the Convert class to round. If you cast you just get the integer part
ofcourse.

Convert class does the rounding for you.
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:O%****************@tk2msftngp13.phx.gbl...
Klaus Bonadt wrote:
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

Nov 15 '05 #4
Klaus Bonadt <Bo****@hotmail.com> wrote:

<snip>
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
Take a look at:
http://www.google.com/url?sa=U&start...ath.pdf&e=7418

Ronald

"Klaus Bonadt" <Bo****@hotmail.com> wrote in message
news:e2**************@TK2MSFTNGP12.phx.gbl...
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

Nov 15 '05 #6
Yes because we all love clicking on PDF links that locks up IE
"Ronald Laeremans [MSFT]" <ro*****@online.microsoft.com> wrote in message
news:Oe**************@tk2msftngp13.phx.gbl...
Take a look at:
http://www.google.com/url?sa=U&start...io-state.edu/~
dws/grouplinks/floating_point_math.pdf&e=7418
Ronald

"Klaus Bonadt" <Bo****@hotmail.com> wrote in message
news:e2**************@TK2MSFTNGP12.phx.gbl...
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


Nov 15 '05 #7
Ronald Laeremans [MSFT] wrote:
Take a look at:
http://www.google.com/url?sa=U&start...ath.pdf&e=7418

There's also an HTML version here:
http://docs.sun.com/source/806-3568/ncg_goldberg.html

Andy
Nov 15 '05 #8
> 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
Nov 15 '05 #9
Klaus Bonadt <Bo****@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Siemel Naran | last post: by
7 posts views Thread by Vinoth | last post: by
9 posts views Thread by Klaus Bonadt | last post: by
10 posts views Thread by Bryan Parkoff | last post: by
39 posts views Thread by rembremading | last post: by
15 posts views Thread by arnuld | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.