Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: Unexpected ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 7 of 14 Topic 45880 of 47017
Post > Topic >>

Re: Unexpected results while working with floats

by Lionel B <me@[EMAIL PROTECTED] > May 12, 2008 at 10:08 PM

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...@[EMAIL PROTECTED]
> wrote:
>>> 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.para****ft.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?

-- 
Lionel B
 




 14 Posts in Topic:
Unexpected results while working with floats
bintom <binoythomas110  2008-05-12 08:09:47 
Re: Unexpected results while working with floats
Lionel B <me@[EMAIL PR  2008-05-12 15:23:03 
Re: Unexpected results while working with floats
bintom <binoythomas110  2008-05-12 08:59:36 
Re: Unexpected results while working with floats
"Victor Bazarov"  2008-05-12 12:38:44 
Re: Unexpected results while working with floats
=?UTF-8?B?RXJpayBXaWtzdHL  2008-05-12 16:42:20 
Re: Unexpected results while working with floats
Lionel B <me@[EMAIL PR  2008-05-12 16:33:21 
Re: Unexpected results while working with floats
Lionel B <me@[EMAIL PR  2008-05-12 22:08:09 
Re: Unexpected results while working with floats
Greg Herlihy <greghe@[  2008-05-12 16:02:54 
Re: Unexpected results while working with floats
Lionel B <me@[EMAIL PR  2008-05-13 08:55:23 
Re: Unexpected results while working with floats
Lionel B <me@[EMAIL PR  2008-05-13 09:04:36 
Re: Unexpected results while working with floats
Greg Herlihy <greghe@[  2008-05-13 02:50:53 
Re: Unexpected results while working with floats
Lionel B <me@[EMAIL PR  2008-05-13 10:11:49 
Re: Unexpected results while working with floats
Juha Nieminen <nospam@  2008-05-13 16:43:14 
Re: Unexpected results while working with floats
bintom <binoythomas110  2008-05-14 09:19:34 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 16:04:22 CDT 2008.