Bug 64808 - static_cast double to int on linux 32
Summary: static_cast double to int on linux 32
Status: RESOLVED DUPLICATE of bug 323
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.8.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-01-26 18:37 UTC by friend1992friend1992
Modified: 2015-01-26 19:04 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description friend1992friend1992 2015-01-26 18:37:43 UTC
I have code :
#include <iostream>
void fun(double _v)
{
    std::cout<<"_v="<<_v<<std::endl;
    long long int var=static_cast< long long int >(_v*1000.);
    std::cout<<"var="<<var<<std::endl;
}
int main(int ac, char** av)
{
    fun(33.33);
    return 0;
}
I try to compile this code on Linux x86_64 with GCC 4.8.2 (g++ test.cpp -o test64), I get result in console:

_v=33.33
var=33330

but when I try to compile this code on Linux i686 with GCC 4.8.2 (g++ test.cpp -o test32), I get result in console:

_v=33.33
var=33329

But if I replace "long long int var=static_cast< long long int >(_v*1000.);" to "double t = _v*1000.; long int var=static_cast< long long int >(t);" I get the result on Linux i686 with GCC 4.4.5:

_v=33.33
var=33330
Comment 1 friend1992friend1992 2015-01-26 18:40:14 UTC
> get the result on Linux i686 with GCC 4.4.5:
Sorry, GCC 4.8.2:
Comment 2 Andreas Schwab 2015-01-26 19:04:13 UTC
You see the effect of excess precision of a floating point value that cannot be represented exactly.

*** This bug has been marked as a duplicate of bug 323 ***