Unexpected results while working with floats

B

bintom

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?
 
B

bintom

I ran the following simple code in C++ and got unexpected results:
float f = 139.4;
cout << f;

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-lite/newbie.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.
 
L

Lionel B

I ran the following simple code in C++ and got unexpected results:
float f = 139.4;
cout << f;

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-lite/newbie.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
 
E

Erik Wikström

I ran the following simple code in C++ and got unexpected results:
float f = 139.4;
cout << f;

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-lite/newbie.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";
}
 
L

Lionel B

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-lite/newbie.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?
 
G

Greg Herlihy

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
 
L

Lionel B

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...
 
L

Lionel B

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
 
G

Greg Herlihy

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
 
L

Lionel B

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.
 
J

Juha Nieminen

bintom said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,939
Members
47,484
Latest member
JackRichard

Latest Threads

Top