Linux奇怪 [英] Linux oddity

查看:59
本文介绍了Linux奇怪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计,


将double转换为int时,结果不如我在Linux上所期望的那样:b / b >

#include< stdio.h>

int

main(int argc,char ** argv)

{

double val = 0.24;

int multiplier = 2000;

int result = static_cast< int> (val *乘数);

printf(" result =%d(应为480)\ n,结果);

返回0;

}


以上代码在SunOS和Windows上打印480,但在

Linux上使用gcc 3.2打印479.是否有效

对这种差异的解释是什么?


- 基思

Hi Folks,

When converting a double to an int, the result is not as
I''d expect on Linux:

#include <stdio.h>
int
main(int argc, char **argv)
{
double val = 0.24;
int multiplier = 2000;
int result = static_cast<int> (val * multiplier);
printf("result = %d (should be 480)\n", result);
return 0;
}

The above code prints 480 on SunOS and Windows, but on
Linux with gcc 3.2 it prints 479. Is there a valid
explanation for this difference?

- Keith

推荐答案

Keith S.写道:
Keith S. wrote:
嗨伙计,

将double转换为int时,结果不如
我会期待在Linux上:

#include< stdio.h>
int
main(int argc,char ** argv)
{/ / = 0.24;
int multiplier = 2000;
int result = static_cast< int> (val *乘数);
printf(" result =%d(应为480)\ n,结果);
返回0;
}
上面的代码在SunOS和Windows上打印480,但在使用gcc 3.2的Linux上打印479.这种差异是否有一个有效的解释?

- Keith
Hi Folks,

When converting a double to an int, the result is not as
I''d expect on Linux:

#include <stdio.h>
int
main(int argc, char **argv)
{
double val = 0.24;
int multiplier = 2000;
int result = static_cast<int> (val * multiplier);
printf("result = %d (should be 480)\n", result);
return 0;
}

The above code prints 480 on SunOS and Windows, but on
Linux with gcc 3.2 it prints 479. Is there a valid
explanation for this difference?

- Keith




嗯,接缝是个数字问题:


#include< cstdio>

int

main(int argc,char ** argv)

{

double val = 0.24;

int multiplier = 2000;

int result = static_cast< int>(val * multiplier);

printf(" val * multiplier - 480 =%20.20f \ n", val *乘数 - 480);

返回0;

}


但这很奇怪。我假设结果只取决于CPU

而不是操作系统...



Hmmm, seams to be a numerical problem:

#include <cstdio>
int
main(int argc, char **argv)
{
double val = 0.24;
int multiplier = 2000;
int result = static_cast<int>(val * multiplier);
printf("val * multiplier - 480 = %20.20f\n", val * multiplier - 480);
return 0;
}

but it''s strange. I assumed that the result would only depend on the CPU
not the Operating System...


Keith S.写道新闻:bm ************ @ ID-169434.news.uni-berlin.de:
Keith S. wrote in news:bm************@ID-169434.news.uni-berlin.de:
嗨伙计,
将double转换为int时,结果不如我在Linux上期望的那样:

#include< stdio.h>
int
main(int argc,char ** argv)
{
double val = 0.24;
int multiplier = 2000;
int result = static_cast< int> (val *乘数);
printf(" result =%d(应为480)\ n,结果);
返回0;
}
以上代码在SunOS和Windows上打印480,但在
Linux上使用gcc 3.2打印479.这种差异是否有正确的解释?
Hi Folks,

When converting a double to an int, the result is not as
I''d expect on Linux:

#include <stdio.h>
int
main(int argc, char **argv)
{
double val = 0.24;
int multiplier = 2000;
int result = static_cast<int> (val * multiplier);
printf("result = %d (should be 480)\n", result);
return 0;
}

The above code prints 480 on SunOS and Windows, but on
Linux with gcc 3.2 it prints 479. Is there a valid
explanation for this difference?




漫画者正在使用不同的舍入规则,因为我相信他们可以做的事情是
。请注意,0.24不能在大多数机器上

表示为精确值,实际得到的是

类似于0.23999999999997。所以

给出480的2个编译器向上舍入到最接近的值,并且

linux框是四舍五入的。它的static_cast< int>()

正在进行四舍五入。


查找函数'sd :: floor()和std: :ceil()在#include< cmath>中定义了




HTH


Rob。

-
http:// www .victim-prime.dsl.pipex.com /


Rob Williscroft写道:
Rob Williscroft wrote:
编译器正在使用不同的舍入规则,我相信他们被允许这样做。请注意,0.24不能在大多数机器上表示为精确值,实际得到的是
类似于0.23999999999997。因此,给出480的两个编译器向上舍入到最接近的值,并且
linux盒子正在向下舍入。它的static_cast< int>()正在进行四舍五入。
The comilers are using diferent rounding rules, as I believe
they are allowed to do. Note that 0.24 can''t on most machines
be represented as an exact value, what you actually get is
something like 0.23999999999997. So the 2 compilers that
give 480 are rounding to up or to the nearest value, and the
linux box is rounding down. Its the static_cast<int>() that
is doing the rounding.




理解,虽然我原以为static_cast< int>'s <

不同平台上的
行为是相同的(关于舍入方法),特别是在使用相同的编译器时...


- 基思



Understood, although I would have expected that static_cast<int>''s
behaviour was the same (with respect to rounding method) on
different platforms, especially when using the same compiler...

- Keith


这篇关于Linux奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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