473,227 Members | 1,637 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,227 software developers and data experts.

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 3048
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
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
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
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
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
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
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
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
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
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
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.