整数浮点转换有问题 [英] Trouble with integer floating point conversion

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

问题描述

大家好!


下面这段代码(对我来说)完全出乎意料的行为。

(我用gcc-Version 4.0编译它。 3)

整数到浮动转换出了问题。

也许有人知道会发生什么。

基本上,当我减去(双)函数值GRID_POINT(2)来自

a变量,在此之前已赋值相同的值给出了

非零结果,我真的不明白为什么。


程序打印

5.000000000000000000e-01; -2.775557561562891351e-17;

0.000000000000000000e + 00


和比较

if(temp1 == GRID_POINT(2) )

有负面结果。


当我用2.0替换

((双)(i))时

一切都按预期运行。所以输出是

5.000000000000000000e-01; 0.000000000000000000e + 00; 0.000000000000000000e + 00


但是:即使整数到浮动转换是不精确的(我认为,

不应该是这种情况)类似于

temp1 = GRID_POINT(2);

temp3 = temp1-GRID_POINT(2);

仍应导致temp3 == 0.0,无论函数如何GRID_POINT。


您怎么看?


谢谢!


--- ------------------------------------------------ <无线电通信/>
#include< stdio.h>


double GRID_POINT(int i);


double GRID_POINT(int i )

{

return(0.1 +((80.1-0.1)/(400.0))*((double)(i)));

}


int main(无效){


double temp1,temp2,temp3;


temp1 = GRID_POINT(2);

temp2 = GRID_POINT(2);

temp3 = temp1-GRID_POINT(2);


printf("%。18e;%。18e;%。18e \ n",temp1,temp3,temp1-temp2);


if(temp1 == GRID_POIN T(2)){

printf(这两个是相等的\ n);

}


返回0;

}

------------------------------- --------------------

Hi all!

The following piece of code has (for me) completely unexpected behaviour.
(I compile it with gcc-Version 4.0.3)
Something goes wrong with the integer to float conversion.
Maybe somebody out there understands what happens.
Essentially, when I subtract the (double) function value GRID_POINT(2) from
a variable which has been assigned the same value before this gives a
non-zero result and I really do not understand why.

The program prints
5.000000000000000000e-01; -2.775557561562891351e-17;
0.000000000000000000e+00

And the comparison
if(temp1==GRID_POINT(2))
has negative outcome.

When I replace
((double)(i)) by 2.0
everything behaves as expected. So the output is
5.000000000000000000e-01; 0.000000000000000000e+00; 0.000000000000000000e+00

But: even if the integer to float conversion is inexact (which, I think,
should not be the case) something like
temp1 = GRID_POINT(2);
temp3 = temp1-GRID_POINT(2);
should still result in temp3==0.0, whatever function GRID_POINT does.

What do You think?

Thank you!

---------------------------------------------------
#include <stdio.h>

double GRID_POINT(int i);

double GRID_POINT(int i)
{
return ( 0.1 + ( (80.1-0.1)/(400.0) )*((double)(i)) );
}

int main (void) {

double temp1, temp2, temp3;

temp1 = GRID_POINT(2);
temp2 = GRID_POINT(2);
temp3 = temp1-GRID_POINT(2);

printf("%.18e; %.18e; %.18e\n", temp1, temp3, temp1-temp2 );

if(temp1==GRID_POINT(2)){
printf("these two are equal\n");
}

return 0;
}
---------------------------------------------------

推荐答案

我应该补充一下,那个问题,描述一下下面,当使用

英特尔编译器时不会发生。

使用gcc编译时我不会使用任何优化选项。

然而,这并不能解决我的问题。


rembremading写道:
I should add, that the problem, described below, does not occur, when the
Intel compiler is used.
When compiling with gcc I don''t use any optimisation options.
This does not solve my problem, however.

rembremading wrote:

大家好!


下面这段代码(对我来说)有完全出乎意料的行为。

(我用gcc-Version 4.0.3编译它)

有些事情发生了整数浮动转换错误。

也许有人在那里了解会发生什么。

基本上,当我减去(双)函数值时GRID_POINT(2)

来自之前已分配相同值的变量这给了一个

非零结果,我真的不明白为什么。


程序打印

5.000000000000000000e-01 ; -2.775557561562891351e-17;

0.000000000000000000e + 00


和比较

if(temp1 == GRID_POINT(2) )

有负面结果。


当我用2.0替换

((双)(i))时

一切都按预期运行。所以输出是

5.000000000000000000e-01; 0.000000000000000000e + 00;

0.000000000000000000e + 00


但是:即使整数到浮动转换是不精确的(我认为,

不应该是这种情况)类似

temp1 = GRID_POINT(2);

temp3 = temp1-GRID_POINT(2);

仍应导致temp3 == 0.0,无论GRID_POINT的功能如何。


您怎么看?


谢谢!


---------------------------------------- -----------

#include< stdio.h>


double GRID_POINT(int i);


double GRID_POINT(int i)

{

return(0.1 +((80.1-0.1)/(400.0))*((double )(i)));

}


int main(无效){


double temp1,temp2 ,temp3;


temp1 = GRID_POINT(2);

temp2 = GRID_POINT(2);

temp3 = temp1-GRID_POINT (2);


printf("%。18e;%。18e;%。18e \ n",temp1,temp3,temp1-temp2);


if(temp1 == GRID_POINT(2)){

printf("这两个是等于$ b | b;

返回0;

}

- -------------------------------------------------
Hi all!

The following piece of code has (for me) completely unexpected behaviour.
(I compile it with gcc-Version 4.0.3)
Something goes wrong with the integer to float conversion.
Maybe somebody out there understands what happens.
Essentially, when I subtract the (double) function value GRID_POINT(2)
from a variable which has been assigned the same value before this gives a
non-zero result and I really do not understand why.

The program prints
5.000000000000000000e-01; -2.775557561562891351e-17;
0.000000000000000000e+00

And the comparison
if(temp1==GRID_POINT(2))
has negative outcome.

When I replace
((double)(i)) by 2.0
everything behaves as expected. So the output is
5.000000000000000000e-01; 0.000000000000000000e+00;
0.000000000000000000e+00

But: even if the integer to float conversion is inexact (which, I think,
should not be the case) something like
temp1 = GRID_POINT(2);
temp3 = temp1-GRID_POINT(2);
should still result in temp3==0.0, whatever function GRID_POINT does.

What do You think?

Thank you!

---------------------------------------------------
#include <stdio.h>

double GRID_POINT(int i);

double GRID_POINT(int i)
{
return ( 0.1 + ( (80.1-0.1)/(400.0) )*((double)(i)) );
}

int main (void) {

double temp1, temp2, temp3;

temp1 = GRID_POINT(2);
temp2 = GRID_POINT(2);
temp3 = temp1-GRID_POINT(2);

printf("%.18e; %.18e; %.18e\n", temp1, temp3, temp1-temp2 );

if(temp1==GRID_POINT(2)){
printf("these two are equal\n");
}

return 0;
}
---------------------------------------------------


rembremading写道:
rembremading wrote:

我应该补充一下,问题如下所述,不会当使用

英特尔编译器时会发生。

使用gcc编译时我不会使用任何优化选项。

这不是但是,解决了我的问题。
I should add, that the problem, described below, does not occur, when the
Intel compiler is used.
When compiling with gcc I don''t use any optimisation options.
This does not solve my problem, however.



英特尔编译器可能将机器的精度设置为64位。


gcc编译器和其他编译器(如lcc-win)设置80位机器的精度




这意味着计算使用更高精度

比double prec完成什么可以导致不同的结果。


您可以在gcc运行时间内将机器的精度设置为64位



-

jacob navia

jacob at jacob point remcomp point fr

logiciels / informatique
http://www.cs.virginia.edu/~lcc-win32


12月12日9:05 pm,jacob navia< ja ... @ nospam.comwrote:
On Dec 12, 9:05 pm, jacob navia <ja...@nospam.comwrote:

rembremading写道:
rembremading wrote:

我应该添加,当使用

英特尔编译器时,下面描述的问题不会发生。

使用gcc编译时我不会使用任何优化选项。

然而,这并不能解决我的问题。
I should add, that the problem, described below, does not occur, when the
Intel compiler is used.
When compiling with gcc I don''t use any optimisation options.
This does not solve my problem, however.



英特尔编译器可能将机器的精度设置为/ b $ b到64位。


gcc编译器和其他编译器(如lcc-win)将机器的精度设置为80位。


这意味着使用更高精度完成计算

比双精度更能导致不同的结果。


您可以将机器的精度设置为64位

在gcc运行时间内。


The Intel compiler sets probably the precision of the machine
to 64 bits.

The gcc compiler and other compilers like lcc-win set the precision
of the machine at 80 bits.

This means that the calculations are done using MORE precision
than double precision what can lead to different results.

You can set the precision of the machine yourself to 64 bits
within the gcc run time.



更好地假设标准保证浮动 -

点操作,如果需要则使用GMP等第三方库

精确度。

Much better to assume only what the Standard guarantees about floating-
point operations, and use third-party libraries like GMP if you need
extra precision.


-

jacob navia

雅各布jacob point remcomp point fr

logiciels / informatiquehttp://www.cs.virginia.edu/~lcc-win32
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatiquehttp://www.cs.virginia.edu/~lcc-win32


这篇关于整数浮点转换有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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