473,508 Members | 2,212 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Floating point subtraction rounding error (NOT display error)

Hey, I have a bit of code that died on a domain error when doing an
arcsin, and apparently it's because floating point subtraction is
having problems. I know about the impossibility of storing floating
point numbers precisely, but I was under the impression that the
standard used for that last digit would prevent subtraction errors
from compounding.

Is there a simple solution to this problem, or do I need to run some
sort of check at every subtraction to make sure that my float does not
deviate? I'm not sure I know even how to do that.

A sample of the failure:
ipdb>1.0000000000000001 == 1
True
ipdb>R
0.69999999999999996
ipdb>R==.7
True
ipdb>y2
3.2999999999999998
ipdb>y2 == 3.3
True
ipdb>cirY-y2
0.70000000000000018
ipdb>cirY-y2 == .7
False

I was unable to find solutions when searching the web because all of
the hits I got were discussing display issues, which I'm not concerned
with.

Thanks,
Adam
Dec 13 '07 #1
5 3059
In article <93**********************************@s12g2000prg. googlegroups.com>,
Keflavich <ke*******@gmail.comwrote:
>
Hey, I have a bit of code that died on a domain error when doing an
arcsin, and apparently it's because floating point subtraction is
having problems. I know about the impossibility of storing floating
point numbers precisely, but I was under the impression that the
standard used for that last digit would prevent subtraction errors
from compounding.

Is there a simple solution to this problem, or do I need to run some
sort of check at every subtraction to make sure that my float does not
deviate? I'm not sure I know even how to do that.
Switch to Decimal module? Available in 2.4 and later.
--
Aahz (aa**@pythoncraft.com) <* http://www.pythoncraft.com/

"Typing is cheap. Thinking is expensive." --Roy Smith
Dec 13 '07 #2
On Thu, 13 Dec 2007 14:30:18 -0800, Keflavich wrote:
Hey, I have a bit of code that died on a domain error when doing an
arcsin, and apparently it's because floating point subtraction is having
problems.
I'm not convinced that your diagnosis is correct. Unless you're using
some weird, uncommon hardware, it's unlikely that a bug in the floating
point subtraction routines has escaped detection. (Unlikely, but not
impossible.) Can you tell us what values give you incorrect results?
I know about the impossibility of storing floating point
numbers precisely, but I was under the impression that the standard used
for that last digit would prevent subtraction errors from compounding.
What gave you that impression? Are you referring to guard digits?

Is there a simple solution to this problem, or do I need to run some
sort of check at every subtraction to make sure that my float does not
deviate? I'm not sure I know even how to do that.
I still don't quite understand your problem. If you think your float is
deviating from the correct value, that implies you know what the correct
value should be. How do you know what the correct value should be?

I should also mention that of course your answer will deviate, due to the
finite precision of floats.

A sample of the failure:
ipdb>1.0000000000000001 == 1
True
ipdb>R
0.69999999999999996
ipdb>R==.7
True
ipdb>y2
3.2999999999999998
ipdb>y2 == 3.3
True
ipdb>cirY-y2
0.70000000000000018
What's cirY? How do you know this is the incorrect value?
ipdb>cirY-y2 == .7
False
Obviously not. As you've already shown, the correct value is
0.70000000000000018, not 0.69999999999999996 (the closest floating point
value to 0.7).

I was unable to find solutions when searching the web because all of the
hits I got were discussing display issues, which I'm not concerned with.
Have you read this?
http://docs.sun.com/source/806-3568/ncg_goldberg.html


--
Steven.

Dec 14 '07 #3
On Dec 14, 3:22 am, Keflavich <keflav...@gmail.comwrote:
On Dec 13, 5:52 pm, Steven D'Aprano <st...@REMOVE-THIS-

cybersource.com.auwrote:
On Thu, 13 Dec 2007 14:30:18 -0800, Keflavich wrote:
Hey, I have a bit of code that died on a domain error when doing an
arcsin, and apparently it's because floating point subtraction is having
problems.
I've come to understand that
1. Floats are fuzzy around the edges.
2. Never do: "The difference between two floats is equal to..."
Do: "The difference between two floats is in the range ..."
But even 2 needs some serious thinking if the range of float values
being
compared is large.

Its unscientific, but floats need more respect.

- Paddy.
Dec 14 '07 #4
Solved: used round(number,12) in this case for all of the operands of
my arcsines. Not pretty, but at least VIM made it easy...
You might have the same problem though:
>>round(1.000340100000325235000000235,13)
1.0003401000003
>>round(1.000340100000325235000000235,12)
1.0003401000000001
Dec 14 '07 #5
On Dec 14, 2:57 am, "Nikos Vergas" <ni...@vergas.grwrote:
Solved: used round(number,12) in this case for all of the operands of
my arcsines. Not pretty, but at least VIM made it easy...

You might have the same problem though:
>round(1.000340100000325235000000235,13)
1.0003401000003
>round(1.000340100000325235000000235,12)

1.0003401000000001
True, but I think that's actually the behavior I'm looking for: the
truncation in this case gives me a smaller number (I hope it's safe to
assume that 1.0003401000000001 < 1.0003401000003), which is exactly
what I want when I'm worried about subtractions giving me numbers very
slightly larger than 1.

Gabriel, Paddy, thanks for the overviews. Yes, floats deserve more
respect, or at least more caution.

I feel fairly certain, however, that floats are exactly what I want
for my purposes: I need moderately high precision and I'm not
concerned about the least-significant-bit errors except when they
violate function domains. I guess the overriding lesson is that every
float subtraction needs to be carefully thought through, which is a
disappointing loss of simplicity for me, but I'll deal with it.

Adam
Dec 14 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3289
by: Dave | last post by:
Hi folks, I am trying to develop a routine that will handle sphere-sphere and sphere-triangle collisions and interactions. My aim is to develop a quake style collision engine where a player can...
12
3783
by: Dave Rahardja | last post by:
Does the C++ standard specify the behavior of floating point numbers during "exceptional" (exceptional with respect to floating point numbers, not exceptions) conditions? For example: double...
687
22819
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
2
15824
by: Benjamin Rutt | last post by:
Does anyone have C code laying around to do this? I have to read in some binary data files that contains some 4-byte IBM/370 floating point values. I would like a function to convert 4-byte...
9
2219
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...
4
2823
by: jacob navia | last post by:
Hi people I continue to work in the tutorial for lcc-win32, and started to try to explain the floating point flags. Here is the relevant part of the tutorial. Since it is a difficult part, I...
7
2191
by: thisismyidentity | last post by:
Hi all, I am trying to predict the behaviour of floating point load and store operations on integer locations. I ve written a small piece of code having some inline assembly code which I am...
15
8035
by: Mukesh_Singh_Nick | last post by:
Why does floating point have a rounding error? How to work around it? For example, the following: flaot f = 1234.12345678F; printf("%2f\n", f) //prints 1234.123413 and
31
1589
by: Mark Dickinson | last post by:
On SuSE 10.2/Xeon there seems to be a rounding bug for floating-point addition: dickinsm@weyl:~python Python 2.5 (r25:51908, May 25 2007, 16:14:04) on linux2 Type "help", "copyright",...
0
7331
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
7391
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
5633
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,...
1
5056
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...
0
4713
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...
0
3204
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...
0
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1564
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 ...
1
768
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.