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

Floating point error (more info)

I just posted regarding a possible floating point error (not sure
where), and have since discovered that:

float f = 5.15002;
double d = 5.15002;
if (float(d) < f)
puts("huh 1?");
float f2 = float(d);
if (f2 < f)
puts("huh 2?");
Causes 'huh 1' to be printed, but NOT 'huh 2'.

I can't tell if this is a compiler program (MSVC 6), an FPU problem or
what!

The assembly code is:

6: float f = 5.15002;
00401028 C7 45 FC F7 CC A4 40 mov dword ptr [ebp-4],40A4CCF7h
7: 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
8: if (float(d) < f)
0040103D DD 45 F4 fld qword ptr [ebp-0Ch]
00401040 D9 55 EC fst dword ptr [ebp-14h]
00401043 D8 5D FC fcomp dword ptr [ebp-4]
00401046 DF E0 fnstsw ax
00401048 F6 C4 01 test ah,1
0040104B 74 0D je main+4Ah (0040105a)
9: puts("huh 1?");
0040104D 68 24 60 42 00 push offset string "huh 1?"
(00426024)
00401052 E8 39 00 00 00 call puts (00401090)
00401057 83 C4 04 add esp,4
10: float f2 = float(d);
0040105A DD 45 F4 fld qword ptr [ebp-0Ch]
0040105D D9 5D F0 fstp dword ptr [ebp-10h]
11: if (f2 < f)
00401060 D9 45 F0 fld dword ptr [ebp-10h]
00401063 D8 5D FC fcomp dword ptr [ebp-4]
00401066 DF E0 fnstsw ax
00401068 F6 C4 01 test ah,1
0040106B 74 0D je main+6Ah (0040107a)
12: puts("huh 2?");
0040106D 68 1C 60 42 00 push offset string "huh 2?"
(0042601c)
00401072 E8 19 00 00 00 call puts (00401090)
00401077 83 C4 04 add esp,4

Nov 13 '05 #1
13 2874
In general, floating point numbers cannot be represented precisely.

You can probably find various texts on this by searching the web for IEEE
Floating Pointing with your favorite search engine.
One such reference that may be helpful is:

http://msdn.microsoft.com/library/de...us/vccore/html
/_core_why_floating_point_numbers_may_lose_precisio n.asp

If you build your program for debug, step thru it, and look at the memory
contents
(the actual hex values, not the converted base 10 representation) being
compared this will probably be clearer.

- joel
"Dylan Nicholson" <dp****@optushome.com.au> wrote in message
news:73**************************@posting.google.c om...
I just posted regarding a possible floating point error (not sure
where), and have since discovered that:

float f = 5.15002;
double d = 5.15002;
if (float(d) < f)
puts("huh 1?");
float f2 = float(d);
if (f2 < f)
puts("huh 2?");
Causes 'huh 1' to be printed, but NOT 'huh 2'.

I can't tell if this is a compiler program (MSVC 6), an FPU problem or
what!

The assembly code is:

6: float f = 5.15002;
00401028 C7 45 FC F7 CC A4 40 mov dword ptr [ebp-4],40A4CCF7h
7: 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
8: if (float(d) < f)
0040103D DD 45 F4 fld qword ptr [ebp-0Ch]
00401040 D9 55 EC fst dword ptr [ebp-14h]
00401043 D8 5D FC fcomp dword ptr [ebp-4]
00401046 DF E0 fnstsw ax
00401048 F6 C4 01 test ah,1
0040104B 74 0D je main+4Ah (0040105a)
9: puts("huh 1?");
0040104D 68 24 60 42 00 push offset string "huh 1?"
(00426024)
00401052 E8 39 00 00 00 call puts (00401090)
00401057 83 C4 04 add esp,4
10: float f2 = float(d);
0040105A DD 45 F4 fld qword ptr [ebp-0Ch]
0040105D D9 5D F0 fstp dword ptr [ebp-10h]
11: if (f2 < f)
00401060 D9 45 F0 fld dword ptr [ebp-10h]
00401063 D8 5D FC fcomp dword ptr [ebp-4]
00401066 DF E0 fnstsw ax
00401068 F6 C4 01 test ah,1
0040106B 74 0D je main+6Ah (0040107a)
12: puts("huh 2?");
0040106D 68 1C 60 42 00 push offset string "huh 2?"
(0042601c)
00401072 E8 19 00 00 00 call puts (00401090)
00401077 83 C4 04 add esp,4

Nov 13 '05 #2
On 15 Jul 2003 19:33:07 -0700, dp****@optushome.com.au (Dylan Nicholson)
wrote:
I just posted regarding a possible floating point error (not sure
where), and have since discovered that:

float f = 5.15002;
double d = 5.15002;
if (float(d) < f)
puts("huh 1?");
float f2 = float(d);
if (f2 < f)
puts("huh 2?");
Causes 'huh 1' to be printed, but NOT 'huh 2'.

First, this appears to be c++, not c, although the concepts are
essentially the same in this case. VC6 supports C90, and not C99, so
there are several compiler errors in the above.

Second, set your compiler to conforming mode:
/Za Disable Language Extensions (check box selected) ANSI C
compatibility. Language constructs not compatible with ANSI C are
flagged as errors. Note that this also turns on the /Op (Improve Float
Consistency) flag, which is what is required here.

With /Za set, neither "huh 1" nor "huh 2" is output, and there are no
anomalies, and the behavior is conforming.

However, without /Za set, VC will default to a mode where floating-point
values remain in registers, and are not type-converted in all cases
dictated by the standard. I got both "huh 1" and "huh 2" output, which
is what I would expect, given VC's default behavior.

Relative to your first post,
float f = 5.15002;
double d = 5.15002;
if (d + FLT_EPSILON < f)
puts("huh?");


it would appear that f is rounded up when converted from double to
float, more than FLT_EPSILON. Note that 5.15002 has type double.

Informally, what I see in the debugger is
5.1500201225281 = f (as double)
5.1500201192093 = d + FLT_EPSILON
5.1500200000000 = d
so the result we get is consistent with what we see here.

As another poster has suggested, you may want to look into the problems
associated with floating-point representations.

Regards,
Bruce Wheeler
Nov 13 '05 #3

"Dylan Nicholson" <dp****@optushome.com.au> wrote in message
news:73**************************@posting.google.c om...

(snip)
| float f = 5.15002;
| double d = 5.15002;
| if (float(d) < f)
| puts("huh 1?");
| float f2 = float(d);
| if (f2 < f)
| puts("huh 2?");
|
|
| Causes 'huh 1' to be printed, but NOT 'huh 2'.
|
| I can't tell if this is a compiler program (MSVC 6), an FPU problem or
| what!

<snip>
If you first cast the double to a float, both numbers are
truncated to 6 digits and compare equal.

Um, if you look at the code, that's exactly what I did, in both cases.
However only one of the comparisons behaves unexpectedly.
I can't see how the above can be anything other than a compiler error
or a FPU instruction set inconsistency (unfortunately I'm not at all
familiar with FPU instructions).


That is not how you cast in C. I don't know that a float() function exists,
though.

It should be if((float)d<f) or, to be sure about precedence
if(((float)d)<f)

In any case, because of the way the internal registers in the floating point
processor it is common for values to be held with a higher precision than
specified. Good or bad, C allows this. Sometimes more precision is good,
sometimes bad.

-- glen
-- glen
Nov 13 '05 #4
On Thu, 17 Jul 2003 08:21:15 GMT, "Glen Herrmannsfeldt"
<ga*@ugcs.caltech.edu> wrote:

"Dylan Nicholson" <dp****@optushome.com.au> wrote in message
news:73**************************@posting.google. com...

(snip)
> | float f = 5.15002;
> | double d = 5.15002;
> | if (float(d) < f)
> | puts("huh 1?");
> | float f2 = float(d);
> | if (f2 < f)
> | puts("huh 2?");
> |
> |
> | Causes 'huh 1' to be printed, but NOT 'huh 2'.
> |
> | I can't tell if this is a compiler program (MSVC 6), an FPU problem or
> | what!
>

<snip>
> If you first cast the double to a float, both numbers are
> truncated to 6 digits and compare equal.
>

Um, if you look at the code, that's exactly what I did, in both cases.
However only one of the comparisons behaves unexpectedly.
I can't see how the above can be anything other than a compiler error
or a FPU instruction set inconsistency (unfortunately I'm not at all
familiar with FPU instructions).


That is not how you cast in C. I don't know that a float() function exists,
though.

It should be if((float)d<f) or, to be sure about precedence
if(((float)d)<f)

In any case, because of the way the internal registers in the floating point
processor it is common for values to be held with a higher precision than
specified. Good or bad, C allows this. Sometimes more precision is good,
sometimes bad.

-- glen
-- glen


(Removing crossposts, and posting only to clc )

In general, that is true. However, there are 2 exceptions: casts
and assignments. In both of the above cases, C doesn't allow an
implementation to maintain extra precision.

---------
N869
5.1.2.3 Program execution
....
[#13] EXAMPLE 4 Implementations employing wide registers have
to take care to honor appropriate semantics. Values are
independent of whether they are represented in a register
or in memory. For example, an implicit spilling of a register
is not permitted to alter the value. Also, an explicit store
and load is required to round to the precision of the
storage type. In particular, casts and assignments are
required to perform their specified conversion. For the
fragment
double d1, d2;
float f;
d1 = f = expression;
d2 = (float) expressions;

the values assigned to d1 and d2 are required to have been
converted to float.
---------

See my post of 30 June 2003 to clc: 'Why is this not a modifiable
lvalue' for a little more information. Also see my post in
another subthread, relative to compiler options, and using VC as
a conforming implementation.

Regards,
Bruce Wheeler

Nov 13 '05 #5
In comp.lang.c Glen Herrmannsfeldt <ga*@ugcs.caltech.edu> wrote:
[...]
It should be if((float)d<f) or, to be sure about precedence
if(((float)d)<f)


There is a simple rule that applies here, which is worth memorising -
all unary operators (typecasts are unary operators) have higher
precedence than the binary operators.

- Kevin

Nov 13 '05 #6
On Thu, 17 Jul 2003 18:54:41 GMT, "Glen Herrmannsfeldt"
<ga*@ugcs.caltech.edu> wrote:
>> > | float f = 5.15002;
>> > | double d = 5.15002;
>> > | if (float(d) < f)
>> > | puts("huh 1?");
>> > | float f2 = float(d);
>> > | if (f2 < f)
>> > | puts("huh 2?");

But note that there are no casts in the above expression. It uses the
float() function (which I don't know what does) but not a cast. It could be
a typographical mistake, though.


Actually, it's C++ code, as I pointed out in my first post. In
C++, a cast can be specified using the above syntax.

The concepts are the same for C and C++ in this case, so I
answered the question in a C context, since he posted to clc.
It is nice to know that casts are required to do the conversion. Though
programs that depend on specific values of floating point values
except under very controlled circumstances (!)
should be considered as bugs.

-- glen


Regards,
Bruce Wheeler

Nov 13 '05 #7
On Thu, 17 Jul 2003 20:00:26 GMT, in comp.lang.c ,
bs*********@hotmail.com (Bruce Wheeler) wrote:
Actually, it's C++ code, as I pointed out in my first post. In
C++, a cast can be specified using the above syntax.


<OT> well, sort of. Its not actually a cast tho is it, its an
instantiation from data.
</ot>

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #8
bs*********@hotmail.com (Bruce Wheeler) wrote in message news:<3f**************@news.siemens.at>...
On 15 Jul 2003 19:33:07 -0700, dp****@optushome.com.au (Dylan Nicholson)
wrote:
I just posted regarding a possible floating point error (not sure
where), and have since discovered that:

float f = 5.15002;
double d = 5.15002;
if (float(d) < f)
puts("huh 1?");
float f2 = float(d);
if (f2 < f)
puts("huh 2?");
Causes 'huh 1' to be printed, but NOT 'huh 2'.

First, this appears to be c++, not c, although the concepts are
essentially the same in this case. VC6 supports C90, and not C99, so
there are several compiler errors in the above.


Ah yes, sorry...I meant to convert to C before posting it...
<snip>
However, without /Za set, VC will default to a mode where floating-point
values remain in registers, and are not type-converted in all cases
dictated by the standard. I got both "huh 1" and "huh 2" output, which
is what I would expect, given VC's default behavior.
So you can't get it to just output huh 1 but not huh 2? You may need
to do it in debug mode (in fact in release mode the floating
comparisons are optimized out, as they are fixed at compile time).
I still don't understand how it could matter which way the casting is
done (explictly inline, or implicity via storing to a float variable).
And I just don't know the x87 FPU instruction set well enough to
understand the difference in the compiled output.
Relative to your first post,
float f = 5.15002;
double d = 5.15002;
if (d + FLT_EPSILON < f)
puts("huh?");
it would appear that f is rounded up when converted from double to
float, more than FLT_EPSILON. Note that 5.15002 has type double.

Informally, what I see in the debugger is
5.1500201225281 = f (as double)
5.1500201192093 = d + FLT_EPSILON
5.1500200000000 = d
so the result we get is consistent with what we see here.

Still, even in this example, it's because I'm using a mixture of
doubles and floats that the problem occurs.
As another poster has suggested, you may want to look into the problems
associated with floating-point representations.

Well yes I'm well aware of those problems, but I don't see how they're
relevant here. I know I was using FLT_EPSILON incorrectly, but
likewise I can't see how that should matter. In general if you assign
the *same* decimal value to two floats, they should never compare as
anything but equal, even if they aren't exactly the value I assigned
to them.

Dylan

Nov 13 '05 #9

"Dylan Nicholson" <dp****@optushome.com.au> wrote in message
news:73**************************@posting.google.c om...
bs*********@hotmail.com (Bruce Wheeler) wrote in message

news:<3f**************@news.siemens.at>...
On 15 Jul 2003 19:33:07 -0700, dp****@optushome.com.au (Dylan Nicholson)
wrote:
I just posted regarding a possible floating point error (not sure
where), and have since discovered that:

float f = 5.15002;
double d = 5.15002;
if (float(d) < f)
puts("huh 1?");
float f2 = float(d);
if (f2 < f)
puts("huh 2?");
Causes 'huh 1' to be printed, but NOT 'huh 2'.


First, this appears to be c++, not c, although the concepts are
essentially the same in this case. VC6 supports C90, and not C99, so
there are several compiler errors in the above.


Ah yes, sorry...I meant to convert to C before posting it...
<snip>
However, without /Za set, VC will default to a mode where floating-point
values remain in registers, and are not type-converted in all cases
dictated by the standard. I got both "huh 1" and "huh 2" output, which
is what I would expect, given VC's default behavior.

So you can't get it to just output huh 1 but not huh 2? You may need
to do it in debug mode (in fact in release mode the floating
comparisons are optimized out, as they are fixed at compile time).
I still don't understand how it could matter which way the casting is
done (explictly inline, or implicity via storing to a float variable).
And I just don't know the x87 FPU instruction set well enough to
understand the difference in the compiled output.
Relative to your first post,
float f = 5.15002;
double d = 5.15002;
if (d + FLT_EPSILON < f)
puts("huh?");


it would appear that f is rounded up when converted from double to
float, more than FLT_EPSILON. Note that 5.15002 has type double.

Informally, what I see in the debugger is
5.1500201225281 = f (as double)
5.1500201192093 = d + FLT_EPSILON
5.1500200000000 = d
so the result we get is consistent with what we see here.

Still, even in this example, it's because I'm using a mixture of
doubles and floats that the problem occurs.
As another poster has suggested, you may want to look into the problems
associated with floating-point representations.

Well yes I'm well aware of those problems, but I don't see how they're
relevant here. I know I was using FLT_EPSILON incorrectly, but
likewise I can't see how that should matter. In general if you assign
the *same* decimal value to two floats, they should never compare as
anything but equal, even if they aren't exactly the value I assigned
to them.

But you didn't have the "same" value, one was float, one was double.
The two approximations of 5.15002 are different. If you wanted them
to be the same, you might have said something like:

float f = (float)5.15002;
double d = (float)5.15002;

You chose instead to allow the compiler to decide what particular
approximation of 5.15002 was loaded into each variable.

In other languages you would have been more precise in your
description of the precision of each variable and each constant.

Just for grins, try it with a number that has an exact representation
in a float, and see what happens :-)

--

... Hank

Hank: http://horedson.home.att.net
W0RLI: http://w0rli.home.att.net
Nov 13 '05 #10
On Thu, 17 Jul 2003 23:52:21 +0100, Mark McIntyre
<ma**********@spamcop.net> wrote:
On Thu, 17 Jul 2003 20:00:26 GMT, in comp.lang.c ,
bs*********@hotmail.com (Bruce Wheeler) wrote:
Actually, it's C++ code, as I pointed out in my first post. In
C++, a cast can be specified using the above syntax.


<OT> well, sort of. Its not actually a cast tho is it, its an
instantiation from data.
</ot>

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


<OT>
Perhaps my terminology was misleading, but type conversion via
functional notation is equivalent to a cast in the case of floats
(or other simple data types), as per section 5.2.3 in the C++
standard.

from the C++ standard:

5.2.3 Explicit type conversion (functional notation)
A simple-type-specifier (7.1.5) followed by a parenthesized
expression-list constructs a value of the specified type
given the expression list. If the expression list is a single
expression, the type conversion expression is equivalent
(in definedness, and if defined in meaning) to the
corresponding cast expression (5.4).
</OT>

Regards,
Bruce Wheeler

Nov 13 '05 #11
bs*********@hotmail.com (Bruce Wheeler) wrote in message news:<3f178ba5.2545420@news>...

<good explanations>

Thanks for the replies, Bruce, I'm glad *someone* here understood my confusion!
Which group were you posting from, btw?

Dylan

Nov 13 '05 #12

"Bruce Wheeler" <bs*********@hotmail.com> wrote in message
news:3f1841bd.5760873@news...

(snip)
<OT>
Perhaps my terminology was misleading, but type conversion via
functional notation is equivalent to a cast in the case of floats
(or other simple data types), as per section 5.2.3 in the C++
standard.

from the C++ standard:

5.2.3 Explicit type conversion (functional notation)
A simple-type-specifier (7.1.5) followed by a parenthesized
expression-list constructs a value of the specified type
given the expression list. If the expression list is a single
expression, the type conversion expression is equivalent
(in definedness, and if defined in meaning) to the
corresponding cast expression (5.4).
</OT>


This wasn't even crossposted to the C++ group.

It is not a cast in C, as someone attempted to claim that it was.

-- glen
Nov 13 '05 #13
On 18 Jul 2003 18:40:59 -0700, dp****@optushome.com.au (Dylan
Nicholson) wrote:
bs*********@hotmail.com (Bruce Wheeler) wrote in message news:<3f178ba5.2545420@news>...

<good explanations>

Thanks for the replies, Bruce, I'm glad *someone* here understood my confusion!
Which group were you posting from, btw?

Dylan


Glad to be of help.

I'm posting from comp.lang.c. However, clc is concerned with
portable c topics (topics which relate to all c implementations).
Generally, implementation-specific topics should not be discussed
there. I tried to answer your questions in the context of a
specific case of general floating-point topics.

I'm not sure what newsgroups would be appropriate for further VC
and Intel-specific questions. clc really isn't. Maybe you should
try comp.programming if you have more questions of this type
(although I am not active in that group).

Regards,
Bruce Wheeler

Nov 13 '05 #14

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...
8
by: janice | last post by:
Hi all, I read a lot of info on this issue, but still don't get it. Why does this happen? document.write(".05" + " \+ " + ".05" + " \= " + (.05 + .05) + "<br>"); document.write("1.05" + "...
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...
5
by: Steffen | last post by:
Hi, is it possible to have two fractions, which (mathematically) have the order a/b < c/d (a,b,c,d integers), but when (correctly) converted into floating point representation just have 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...
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
9
by: ssubbarayan | last post by:
Hi all, I am trying a program to convert floating point values to a byte array and printing the same to the screen.The idea behind this is we already have an existing function which can do byte...
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...
1
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: 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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.