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

Floating point error?

The following C code:

float f = 5.15002;
double d = 5.15002;
if (d + FLT_EPSILON < f)
puts("huh?");

Where 'FLT_EPSILON' is defined as the minimum number whereby 1.0 +
FLT_EPSILON != 1.0, causes the string "huh!" to be printed out on my
system (Pentium III)
I have included the relevant assembly code below.
Now is this a bug in the intel chip, or allowable because FLT_EPSILON
makes no promises when added to numbers greater than 1.0?

It doesn't happen when 'd' is another float, so presumably it has
something to do with double-precision/single-precision conversions
too.

Curious...

Dylan
8: float f = 5.15002;
00401028 C7 45 FC F7 CC A4 40 mov dword ptr [ebp-4],40A4CCF7h
9: double d = 5.15002;
0040102F C7 45 F4 D2 FB C6 D7 mov dword ptr
[ebp-0Ch],0D7C6FBD2h
00401036 C7 45 F8 9E 99 14 40 mov dword ptr [ebp-8],4014999Eh
10: if (d + FLT_EPSILON < f)
0040103D DD 45 F4 fld qword ptr [ebp-0Ch]
00401040 DC 05 28 60 42 00 fadd qword ptr
[__real@8@3fe88000000000000000 (00426028)]
00401046 D9 45 FC fld dword ptr [ebp-4]
00401049 DE D9 fcompp
0040104B DF E0 fnstsw ax
0040104D F6 C4 41 test ah,41h
00401050 75 0D jne main+4Fh (0040105f)
11: puts("huh?");
00401052 68 1C 60 42 00 push offset string "huh?"
(0042601c)
00401057 E8 34 00 00 00 call puts (00401090)
0040105C 83 C4 04 add esp,4
12: return 0;
0040105F 33 C0 xor eax,eax

Nov 13 '05 #1
3 3928
> float f = 5.15002;
double d = 5.15002;
if (d + FLT_EPSILON < f)
puts("huh?");

Where 'FLT_EPSILON' is defined as the minimum number whereby 1.0 +
FLT_EPSILON != 1.0, causes the string "huh!" to be printed out on my
system (Pentium III)
I have included the relevant assembly code below.
Now is this a bug in the intel chip, or allowable because FLT_EPSILON
makes no promises when added to numbers greater than 1.0?
It's allowable because FLT_EPSILON makes no promises when added to
numbers greater than 1.0.

Since this is an IEEE implementation with FLT_RADIX of 2, I'd expect
it to work
if (d + 4.0*FLT_EPSILON < f)
puts("huh?");

for values of f where f >= 4.0 and f < 8.0 .
It doesn't happen when 'd' is another float, so presumably it has
something to do with double-precision/single-precision conversions
too.

Curious...


There is no exact representation of most non-integer decimal numbers
in binary floating point. This is the case for 5.15002 above.
You can't get an exact representation without an INFINITE number of
bits, but the more bits you use, the closer you'll get. Expect
double -> float to lose accuracy. And don't think about "decimal digits".
0.1 in binary is an infinite repeating fraction.

Gordon L. Burditt

Nov 13 '05 #2
On 15 Jul 2003 19:25:24 -0700, dp****@optushome.com.au (Dylan
Nicholson) wrote in comp.lang.asm.x86:
The following C code:

float f = 5.15002;
double d = 5.15002;
if (d + FLT_EPSILON < f)
puts("huh?");

Where 'FLT_EPSILON' is defined as the minimum number whereby 1.0 +
FLT_EPSILON != 1.0, causes the string "huh!" to be printed out on my
system (Pentium III)
I have included the relevant assembly code below.
Now is this a bug in the intel chip, or allowable because FLT_EPSILON
makes no promises when added to numbers greater than 1.0?

It doesn't happen when 'd' is another float, so presumably it has
something to do with double-precision/single-precision conversions
too.

Curious...

Dylan


[snip]

You're misunderstanding the meaning of FLT_EPSILON.

If you want to check the minimum representable difference between the
float representation of 10.0 and the next higher float value, you need
to use FLT_EPSILON * 10. If you were checking a value around 100, you
would need to use FLT_EPSILON * 100.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

Nov 13 '05 #3
Jack Klein wrote:
On 15 Jul 2003 19:25:24 -0700, dp****@optushome.com.au (Dylan
Nicholson) wrote in comp.lang.asm.x86:
The following C code:

float f = 5.15002;
double d = 5.15002;
if (d + FLT_EPSILON < f)
puts("huh?");

Where 'FLT_EPSILON' is defined as the minimum number whereby 1.0 +
FLT_EPSILON != 1.0, causes the string "huh!" to be printed out on my
system (Pentium III)
I have included the relevant assembly code below.
Now is this a bug in the intel chip, or allowable because FLT_EPSILON
makes no promises when added to numbers greater than 1.0?

It doesn't happen when 'd' is another float, so presumably it has
something to do with double-precision/single-precision conversions
too.

Curious...

Dylan


[snip]

You're misunderstanding the meaning of FLT_EPSILON.

If you want to check the minimum representable difference between the
float representation of 10.0 and the next higher float value, you need
to use FLT_EPSILON * 10. If you were checking a value around 100, you
would need to use FLT_EPSILON * 100.

"close enough for government work" More accurately, FLT_EPSILON times the
largest integer power of FLT_RADIX which is less than the number to be
compared. 4*FLT_EPSILON in the case above. Not a good idea to redefine a
constant defined by Standard C, as the OP implies he is doing.
--
Tim Prince

Nov 13 '05 #4

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...
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...
3
by: Mark L Pappin | last post by:
<puts on Compiler Vendor hat> I've recently discovered that our compilers don't make any attempt to handle floating point overflow in add/subtract/ multiply/divide, with the result that...
15
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this...
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...
4
by: alex | last post by:
hi friends ... i am facing a problem while detecting floating point operations in my project, please help me. i want to find out the places in my C/C++ project where i am doing floating...
1
by: Shhnwz.a | last post by:
Hi, I have a problem regarding handling floating point error, specially Denormalisation error. I want to know is there any trick or technique to find bit length of the resultant before using and...
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
5
by: Keflavich | last post by:
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...
7
by: ma740988 | last post by:
Consider the equation (flight dynamics stuff): Yaw (Degrees) = Azimuth Angle(Radians) * 180 (Degrees) / 3.1415926535897932384626433832795 (Radians) There's a valid reason to use single...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.