Visual Studio版本中的Math函数问题 [英] Issues with Math functions in Visual Studio Versions

查看:249
本文介绍了Visual Studio版本中的Math函数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include "string.h"
#include "math.h"

int main ()
{
   
double result = (double)sqrt(2.3456F);
   
unsigned char data[8]; //holds the bytes of the result

    memmove
(data,&result,8);
   
for(int i=0;i<8;i++)
        printf
("%d\n",data[i]);
   
return 0;
}

上面的相同代码将sqrt()结果转换为double,然后存储在另一个double变量中.这里的数据保存浮点数结果的二进制表示形式.同一代码在Visual C ++ 6.0和Visual C ++ 2010上运行时,会产生不同的结果. 结果.为什么会这样?

The above same code convert the sqrt() result to double and then stores in another double variable. Here the data holds the binary representation of the floating point number result.The same code when run on Visual C++ 6.0 and Visual C++ 2010 gives different results. Why is this so?

还有一件事是,如果结果为浮点型,则Visual C ++ 6.0和Visual C ++ 2010会为结果提供相同的值.

and one more thing is If the result had been a float then Visual C++ 6.0 and Visual C++ 2010 are giving the same value for the result.

任何人都可以解决这个问题吗?

Can any body sort out this??

结果变量为double的输出::

Output with result variable being double::

使用Visual C ++ 6.0

with Visual C++ 6.0

结果变量数据的字节表示:

byte representation of result variable-data::

[0] 196 'Ä'
[1] 12 ''
[2] 25 ''
[3] 254 'þ'
[4] 42 '*'
[5] 129 ''
[6] 248 'ø'
[7] 63 '?'

使用Visual C ++ 2010

with Visual C++ 2010

数据::

    [0] 0   unsigned char
   
[1] 0   unsigned char
   
[2] 0   unsigned char
   
[3] 0   unsigned char
   
[4] 43 '+'  unsigned char
   
[5] 129 ''  unsigned char
   
[6] 248 'ø' unsigned char
   
[7] 63 '?'  unsigned char

结果变量为float的输出::

Output with result variable being float::

使用Visual C ++ 6.0

with Visual C++ 6.0

    [0] 88 'X'  
   
[1] 9 ' '  
   
[2] 196 'Ä'
   
[3] 63 '?'  

使用Visual C ++ 2010

with Visual C++ 2010

我得到的结果变量的二进制值与上面相同.

I got the same binary value for result variable as above.

推荐答案

似乎VC6处理的sqrt与VC2010不同. VC6采用了sqrt的双重版本,在您的特定示例中,我认为VC2010采用了float版本.

如果将计算的行更改为:

双重结果=(double)sqrt(2.3456);

结果似乎是一个双精度值,而不是(加倍)强制转换的浮点值.
It seems VC6 handles the sqrt different to VC2010. VC6 takes the double version of sqrt, in your particualr example VC2010 takes the float version in my opinion.

If you change the line with your calculation to:

double result = (double)sqrt(2.3456);

the result seems a double value and not a (to double) casted float value.


这篇关于Visual Studio版本中的Math函数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆