473,588 Members | 2,565 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 2225

"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
3645
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
4463
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
3384
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
18750
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
2428
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
3538
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
7523
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
7929
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
7860
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
7984
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
8223
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
6634
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...
1
5726
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3847
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1458
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1195
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.