473,399 Members | 3,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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 2215

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: JS | last post by:
We have the same floating point intensive C++ program that runs on Windows on Intel chip and on Sun Solaris on SPARC chips. The program reads the exactly the same input files on the two platforms....
2
by: Siemel Naran | last post by:
About inaccurate floating point and DBL_MAX. double x = 2; double y = 1 + 1; assert(x == y); Because of inaccurate floating point representation, x and y may not be equal (ie. they may...
7
by: Vinoth | last post by:
I'm working in an ARM (ARM9) system which does not have Floating point co-processor or Floating point libraries. But it does support long long int (64 bits). Can you provide some link that would...
9
by: Klaus Bonadt | last post by:
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...
10
by: Bryan Parkoff | last post by:
The guideline says to use %f in printf() function using the keyword float and double. For example float a = 1.2345; double b = 5.166666667; printf("%.2f\n %f\n", a, b);
6
by: yong321 | last post by:
With this script <script> alert(8/(3-8/3)) </script> I hope to get 24, but I get 23.99999999999999 in IE 6.0.2800 and Firefox 1.5.0.4. alert(6/(1-3/4)) returns 24 as expected. I see a...
39
by: rembremading | last post by:
Hi all! The following piece of code has (for me) completely unexpected behaviour. (I compile it with gcc-Version 4.0.3) Something goes wrong with the integer to float conversion. Maybe somebody...
15
by: arnuld | last post by:
Next month I will start to work on a C++ based Software named CAT++ which is going to provide FORTRAN like arrays in C++ and will be used within Scientific Community and hence will heavily depend...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.