473,700 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.9999999999999 991.
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 2230

"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.9999999999999 991.
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.9999999999999 991.
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.nos pam>
wrote in message news:O%******** ********@tk2msf tngp13.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.9999999999999 991.
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.co m>
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******** ******@TK2MSFTN GP12.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.9999999999999 991.
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******** ******@tk2msftn gp13.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******** ******@TK2MSFTN GP12.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.9999999999999 991.
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.9999999999999 991.
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.co m>
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
3669
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. However, they generate slightly different results for floating point numbers. Are they really supposed to generate exactly the same results? I guess so because both platforms are supposed to be IEEE floating point standard (754?) compliant. ...
2
4479
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 differ by 0.0000000001 or some small number) on some implementations.
7
3391
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 discuss about ways to emulate floating point calculations with just long int or long long int. For eg., if i've a formula X=(1-b)*Y + b*Z in floating point domain, i can calculate X with just long ints (but, some data may be lost in final division;...
9
2381
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 result will be 4.0 - as expected. But if I cast the same to int: (int)((1.4 - 1.0) * 10.0)
10
18761
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
2436
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 number of threads in this newsgroup about the floating point
39
3561
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 out there understands what happens. Essentially, when I subtract the (double) function value GRID_POINT(2) from a variable which has been assigned the same value before this gives a non-zero result and I really do not understand why.
15
7532
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 on Numerical-Computations. I was reading 29.16 ans 29.17 sections of FAQs: http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.16 as a test, I just tried this program on Linux, GCC 4.2.2 and it gave me 2 different results:
0
8725
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9214
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8970
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8924
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7807
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5902
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4656
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3088
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2392
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.