470,814 Members | 1,231 Online

# Unexpected results while working with floats

I ran the following simple code in C++ and got unexpected results:

float f = 139.4;
cout << f;

Output:
139.399994;

if( f == 139.4)
cout << "Expected result";
else
cout << "Unexpected result";

Output:
Unexpected reult
I ran the following equivalent code in VB but got the correct results.

Dim f As Single
f = 139.4

Print f
If f = 139.4 Then
Print "Expected result"
Else
Print "Unexpected result"
End If
Doesn't this look bad on C++'s resume?
Jun 27 '08 #1
13 3314
On Mon, 12 May 2008 08:09:47 -0700, bintom wrote:
I ran the following simple code in C++ and got unexpected results:

float f = 139.4;
cout << f;

Output:
139.399994;

if( f == 139.4)
cout << "Expected result";
else
cout << "Unexpected result";

Output:
Unexpected reult
[...]
Doesn't this look bad on C++'s resume?
No, it looks bad on *your* resume ;-)

http://www.parashift.com/c++-faq-lit...html#faq-29.16

--
Lionel B
Jun 27 '08 #2
On May 12, 8:23*pm, Lionel B <m...@privacy.netwrote:
On Mon, 12 May 2008 08:09:47 -0700, bintom wrote:
I ran the following simple code in C++ and got unexpected results:
float f = 139.4;
cout << f;
Output:
139.399994;
if( f == 139.4)
* cout << "Expected result";
else
* cout << "Unexpected result";
Output:
Unexpected reult

[...]
Doesn't this look bad on C++'s resume?

No, it looks bad on *your* resume ;-)

http://www.parashift.com/c++-faq-lit...html#faq-29.16

--
Lionel B- Hide quoted text -

- Show quoted text -
Thanks Lionel for directing me to the link, but my question remains
unanswered. Should it be a computer science issue as it says on the
site, VB should've produced a similar (inaccurate) result. I'm just
trying to defend my resume.
Jun 27 '08 #3
On Mon, 12 May 2008 08:59:36 -0700, bintom wrote:
On May 12, 8:23Â*pm, Lionel B <m...@privacy.netwrote:
>On Mon, 12 May 2008 08:09:47 -0700, bintom wrote:
I ran the following simple code in C++ and got unexpected results:
float f = 139.4;
cout << f;
Output:
139.399994;
if( f == 139.4)
Â* cout << "Expected result";
else
Â* cout << "Unexpected result";
Output:
Unexpected reult

[...]
Doesn't this look bad on C++'s resume?

No, it looks bad on *your* resume ;-)

http://www.parashift.com/c++-faq-lit...html#faq-29.16

Thanks Lionel for directing me to the link, but my question remains
unanswered. Should it be a computer science issue as it says on the
site, VB should've produced a similar (inaccurate) result. I'm just
trying to defend my resume.
As it happens, on my system your C++ code produces the "correct" result.
But that's irrelevant - as the FAQ says: floating point is an
approximation. This is inescapable. If you're interested in pursuing the
issue further, have a look at the classic article: "What Every Computer
Scientist Should Know About Floating-Point Arithmetic"

http://docs.sun.com/source/806-3568/ncg_goldberg.html

--
Lionel B
Jun 27 '08 #4
bintom wrote:
>[..]
http://www.parashift.com/c++-faq-lit...html#faq-29.16

--
Lionel B- Hide quoted text -

- Show quoted text -

Thanks Lionel for directing me to the link, but my question remains
unanswered. Should it be a computer science issue as it says on the
site, VB should've produced a similar (inaccurate) result. I'm just
trying to defend my resume.
Defending one's resume is a waste of time. You need to learn what
you don't know and _update_ your resume. If you like how VB handles
things, you should stay with VB.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #5
On 2008-05-12 17:59, bintom wrote:
On May 12, 8:23 pm, Lionel B <m...@privacy.netwrote:
>On Mon, 12 May 2008 08:09:47 -0700, bintom wrote:
I ran the following simple code in C++ and got unexpected results:
float f = 139.4;
cout << f;
Output:
139.399994;
if( f == 139.4)
cout << "Expected result";
else
cout << "Unexpected result";
Output:
Unexpected reult

[...]
Doesn't this look bad on C++'s resume?

No, it looks bad on *your* resume ;-)

http://www.parashift.com/c++-faq-lit...html#faq-29.16
Please do not quote signatures.
Thanks Lionel for directing me to the link, but my question remains
unanswered. Should it be a computer science issue as it says on the
site, VB should've produced a similar (inaccurate) result. I'm just
trying to defend my resume.
You are comparing apples to pears, f is a float while 13.4 is a double
(an advice, always turn up the warning levels and fix as many of them as
you can), try this code and see what it does:

#include <iostream>

int main()
{
float f = 139.4f;
std::cout << f << "\n";

if( f == 139.4f)
std::cout << "Expected result\n";
else
std::cout << "Unexpected result\n";
}

--
Erik WikstrÃ¶m
Jun 27 '08 #6
On Mon, 12 May 2008 16:42:20 +0000, Erik WikstrÃ¶m wrote:
On 2008-05-12 17:59, bintom wrote:
>On May 12, 8:23 pm, Lionel B <m...@privacy.netwrote:
>>On Mon, 12 May 2008 08:09:47 -0700, bintom wrote:
I ran the following simple code in C++ and got unexpected results:

float f = 139.4;
cout << f;

Output:
139.399994;

if( f == 139.4)
cout << "Expected result";
else
cout << "Unexpected result";

Output:
Unexpected reult

[...]

Doesn't this look bad on C++'s resume?

No, it looks bad on *your* resume ;-)

http://www.parashift.com/c++-faq-lit...html#faq-29.16

Please do not quote signatures.
>Thanks Lionel for directing me to the link, but my question remains
unanswered. Should it be a computer science issue as it says on the
site, VB should've produced a similar (inaccurate) result. I'm just
trying to defend my resume.

You are comparing apples to pears, f is a float while 13.4 is a double
(an advice, always turn up the warning levels and fix as many of them as
you can), try this code and see what it does:

#include <iostream>

int main()
{
float f = 139.4f;
std::cout << f << "\n";

if( f == 139.4f)
std::cout << "Expected result\n";
else
std::cout << "Unexpected result\n";
}
On my system (compiler GCC 4.1.2) with

float f = 139.4;

and compiled with:

g++ -std=c++98 -pedantic -Wall -Wextra

I get no warnings and the output is 'Expected result'. Should the
compiler warn if there's an implicit conversion? Or only if it results in
loss of precision?

--
Lionel B
Jun 27 '08 #7
On May 12, 3:08*pm, Lionel B <m...@privacy.netwrote:
>
On my system (compiler GCC 4.1.2) with

* float f = 139.4;

and compiled with:

* g++ -std=c++98 -pedantic -Wall -Wextra

I get no warnings and the output is 'Expected result'. Should the
compiler warn if there's an implicit conversion? Or only if it results in
loss of precision?
The g++ compiler can issue a warning if you want one:

g++ -Wshorten-64-to-32

Greg

Jun 27 '08 #8
On Mon, 12 May 2008 16:02:54 -0700, Greg Herlihy wrote:
On May 12, 3:08Â*pm, Lionel B <m...@privacy.netwrote:
>>
On my system (compiler GCC 4.1.2) with

Â* float f = 139.4;

and compiled with:

Â* g++ -std=c++98 -pedantic -Wall -Wextra

I get no warnings and the output is 'Expected result'. Should the
compiler warn if there's an implicit conversion? Or only if it results
in loss of precision?

The g++ compiler can issue a warning if you want one:

g++ -Wshorten-64-to-32
cc1plus: error: unrecognized command line option "-Wshorten-64-to-32"

GCC 4.3.0 on x86_64

Anyway, that doesn't sound like a floating-point warning...

--
Lionel B
Jun 27 '08 #9
On Tue, 13 May 2008 08:55:23 +0000, Lionel B wrote:
On Mon, 12 May 2008 16:02:54 -0700, Greg Herlihy wrote:
>On May 12, 3:08Â*pm, Lionel B <m...@privacy.netwrote:
>>>
On my system (compiler GCC 4.1.2) with

Â* float f = 139.4;

and compiled with:

Â* g++ -std=c++98 -pedantic -Wall -Wextra

I get no warnings and the output is 'Expected result'. Should the
compiler warn if there's an implicit conversion? Or only if it results
in loss of precision?

The g++ compiler can issue a warning if you want one:

g++ -Wshorten-64-to-32

cc1plus: error: unrecognized command line option "-Wshorten-64-to-32"

GCC 4.3.0 on x86_64

Anyway, that doesn't sound like a floating-point warning...
This does it: -Wconversion. With

float f = 139.4;

warning: conversion to â€˜floatâ€™ alters â€˜doubleâ€™ constant value

--
Lionel B
Jun 27 '08 #10
On May 13, 2:04*am, Lionel B <m...@privacy.netwrote:
On Tue, 13 May 2008 08:55:23 +0000, Lionel B wrote:
On Mon, 12 May 2008 16:02:54 -0700, Greg Herlihy wrote:
On May 12, 3:08*pm, Lionel B <m...@privacy.netwrote:
>On my system (compiler GCC 4.1.2) with
>* float f = 139.4;
>and compiled with:
>* g++ -std=c++98 -pedantic -Wall -Wextra
>I get no warnings and the output is 'Expected result'. Should the
compiler warn if there's an implicit conversion? Or only if it results
in loss of precision?
The g++ compiler can issue a warning if you want one:
* *g++ -Wshorten-64-to-32
cc1plus: error: unrecognized command line option "-Wshorten-64-to-32"
GCC 4.3.0 on x86_64
Anyway, that doesn't sound like a floating-point warning...

This does it: -Wconversion. With

* float f = 139.4;

warning: conversion to ‘float’ alters ‘double’ constant value
On my machine, using gcc (version 4.2.1 (Apple Inc. build 5559)) -
Wconversion reports nothing.
Whereas -Wshorten-64-to-32 reports:

warning: implicit conversion shortens 64-bit value into a 32-bit
value

Greg
Jun 27 '08 #11
On Tue, 13 May 2008 02:50:53 -0700, Greg Herlihy wrote:
On May 13, 2:04Â*am, Lionel B <m...@privacy.netwrote:
>On Tue, 13 May 2008 08:55:23 +0000, Lionel B wrote:
On Mon, 12 May 2008 16:02:54 -0700, Greg Herlihy wrote:
>On May 12, 3:08Â*pm, Lionel B <m...@privacy.netwrote:
>>On my system (compiler GCC 4.1.2) with
>>Â* float f = 139.4;
>>and compiled with:
>>Â* g++ -std=c++98 -pedantic -Wall -Wextra
>>I get no warnings and the output is 'Expected result'. Should the
compiler warn if there's an implicit conversion? Or only if it
results in loss of precision?
>The g++ compiler can issue a warning if you want one:
>Â* Â*g++ -Wshorten-64-to-32
cc1plus: error: unrecognized command line option "-Wshorten-64-to-32"
GCC 4.3.0 on x86_64
Anyway, that doesn't sound like a floating-point warning...

This does it: -Wconversion. With

Â* float f = 139.4;

warning: conversion to â€˜floatâ€™ alters â€˜doubleâ€™ constant value

On my machine, using gcc (version 4.2.1 (Apple Inc. build 5559)) -
Wconversion reports nothing.
Whereas -Wshorten-64-to-32 reports:

warning: implicit conversion shortens 64-bit value into a 32-bit
value
Sure, I see that -Wshorten-64-to-32 is a Mac thing - it's all going to be
highly machine-specific. I'm surprised -Wconversion doesn't report
anything, though.

--
Lionel B
Jun 27 '08 #12
bintom wrote:
float f = 139.4;
if( f == 139.4)
You are converting a 64-bit floating point number into a 32-bit one,
and then comparing the result to a 64-bit one. All bets are off.

The difference with some other languages is that C++ doesn't try to
second-guess what you are really trying to do, and it does what and only
what you asked it to do.

That the equality comparison is giving false is a result of the
underlying hardware and the floating point format used, not a flaw in
C++. The underlying hardware is dropping some bits from this value in
question when you are making the 64->32 bit conversion. When you try to
compare it to the original 64-bit value, the result will be different.
Jun 27 '08 #13
A big thanks to Erik and Juha for showing me where I was going wrong.
You saved my day.
Jun 27 '08 #14

### This discussion thread is closed

Replies have been disabled for this discussion.