static_cast double to __int64在linux 32中加倍

时间:2015-01-26 16:01:06

标签: c++ linux gcc static-cast

我有代码:

#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;
}

我尝试使用GCC 4.8.2(g ++ test.cpp -o test64)在Linux x86_64上编译此代码,我在控制台中得到结果:

_v=33.33
var=33330

但是当我尝试使用GCC 4.4.5(g ++ test.cpp -o test32)在Linux i686上编译此代码时,我得到了控制台中的结果:

_v=33.33
var=33329

为什么呢? 我尝试分析目标文件。 objdump -t test64.o

SYMBOL TABLE:
 l    df *ABS*   test.cpp
 l    d  .text   .text
 l    d  .data   .data
 l    d  .bss    .bss
 l     O .bss    _ZStL8__ioinit
 l    d  .rodata     .rodata
 l     F .text   _Z41__static_initialization_and_destruction_0ii
 l     F .text   _GLOBAL__sub_I__Z3fund
 l    d  .init_array     .init_array
 l    d  .note.GNU-stack     .note.GNU-stack
 l    d  .eh_frame   .eh_frame
 l    d  .comment    .comment
 g     F .text   _Z3fund
         *UND*   _ZSt4cout
         *UND*   _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
         *UND*   _ZNSolsEd
         *UND*   _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
         *UND*   _ZNSolsEPFRSoS_E
         *UND*   _ZNSolsEx
 g     F .text   main
         *UND*   _ZNSt8ios_base4InitC1Ev
         *UND*   .hidden __dso_handle
         *UND*   _ZNSt8ios_base4InitD1Ev
         *UND*   __cxa_atexit

objdump -t test32.o

SYMBOL TABLE:
 l    df *ABS*   test.cpp
 l    d  .text   .text
 l    d  .data   .data
 l    d  .bss    .bss
 l     O .bss    _ZStL8__ioinit
 l    d  .rodata     .rodata
 l     F .text   _Z41__static_initialization_and_destruction_0ii
 l     F .text   _GLOBAL__I__Z3fund
 l    d  .ctors  .ctors
 l    d  .note.GNU-stack     .note.GNU-stack
 l    d  .eh_frame   .eh_frame
 l    d  .comment    .comment
 g     F .text   _Z3fund
         *UND*   __gxx_personality_v0
         *UND*   _ZSt4cout
         *UND*   _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
         *UND*   _ZNSolsEd
         *UND*   _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
         *UND*   _ZNSolsEPFRSoS_E
         *UND*   _ZNSolsEx
 g     F .text   main
         *UND*   _ZNSt8ios_base4InitC1Ev
         *UND*   _ZNSt8ios_base4InitD1Ev
         *UND*   __dso_handle
         *UND*   __cxa_atexit

修改 如果我将long long int var=static_cast< long long int >(_v*1000.);替换为double t = _v*1000.; long int var=static_cast< long long int >(t);,我会在使用GCC 4.4.5的Linux i686上获得结果:

_v=33.33
var=33330

为什么?

0 个答案:

没有答案