转换时双精度数字是否总是四舍五入为相同的浮点值? [英] Will double numbers always round off to the same float value when casted?

查看:89
本文介绍了转换时双精度数字是否总是四舍五入为相同的浮点值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



先问一个简单问题.

我有这个代码.

Hi,

simple question first.

i have this code.

double d = 0.123456789; 
float f;

f = (float)d;



假设答案是0.123456791f.

是否给出相同的浮点值(在这种情况下为0.123456791f)
不考虑语言,CPU(x86)和平台(例如Windows).

有没有提及这是事实的参考文献?
(仅需要证明文件即可,这就是原因.)

另外,是否有一个函数或某些东西定义了舍入的方式?
例如,如果此值到来,则放置此值,依此类推.

现在是个大问题.

我有这个不等式.



assume the answer is 0.123456791f.

does it give the same float value (in this case 0.123456791f)
regardless of language, CPU(x86) and platform (Windows for example).

are there any references that speak of this as a fact?
(just need proof for documentation, that''s why.)

also, is there a function or something that defines how the rounding happen?
for example, if this value comes, you put this value, so on and so forth.

now the big question.

i have this inequation.

int myFunc(float f);

double L = someVal1;
double H = someVal1 + 1;
double X = myVal;
int answer;

//we have to pass myVal to a function that excepts only float
//if X >= L && X < H then we are fine. 
//otherwise, we need to do this.
if (X < L || X >= H)
{
    X = (L + H)/2;
}

//however, since in that function the double value 
//gets casted to float, we have to do it this way. 
//otherwise we might get a wrong value because of loss
//of precision 
//for example, if X == L, then we won't change X to mid value, 
//but if after float conversion it becomes less than L, 
//we will not get the value we anticipated. 
//so the code becomes, 

if ((float)X < L || (float)X >= H)
{
    X = (L + H)/2;
}

answer myFunc(X); //i will simply get a warning when compiling



所以我做错什么了吗?
例如,(float)X
产生的值是否可以 因为如果条件不同于从
看到的值 在myFunc()内;

不知道这是否适用或正确,所以请让我知道,
(浮动)X< L发生在myFunc()中的寄存器和变量中
被保存在内存中,可能会有区别吗?



so am i doing something wrong?
for example, can the value that results from (float)X
in that if condition be different to the value seen from
within myFunc();

not sure if this is applicable or true, so please let me know if not,
(float)X < L happens in the registers and variables inside myFunc()
are held in memory and there can be a difference?

thanks.

推荐答案

首先,浮点数和双数的表示与平台,语言和指令集体系结构无关,因为它受标准IEEE的约束. 754.请参阅:
http://en.wikipedia.org/wiki/IEEE_754 [ http://en.wikipedia.org/wiki/Rounding_error [ http://en.wikipedia.org/wiki/Truncation [ http://en.wikipedia.org/wiki/Rounding [ http://en.wikipedia.org/wiki/Precision_%28arithmetic%29 [ ^ ].

使用近似计算是一个大问题,对于使用数字方法或数字模拟的人们或多或少来说是很熟悉的,因此很难在一个简短的答案中对其进行概述.

关于您的大问题",由于最后一行,我无法完全分析它,只是因为您的代码无法编译.您可能需要从目标开始进行梳理,并更好地解释问题.我只建议一件事:不允许混合使用具有不同精度的变量.如果必须使用double和float,请确保所有中间操作都以double形式完成;否则,请执行两次.仅在计算的最后阶段将结果强制转换为float.顺便说一下,您应该了解几乎在所有情况下浮点精度都不够.它通常用于表示某些最终结果,例如屏幕输出,最终结果的存储,图形.还请记住:舍入误差会累积.

—SA
First of all, the presentation of float and double numbers is independent from platform, language and instruction-set architectures, because it is governed by the standard IEEE 754. Please see:
http://en.wikipedia.org/wiki/IEEE_754[^].

In general case, cast never means true rounding, it''s more of the truncation. Rounding is a pretty complex problem. Please see:
http://en.wikipedia.org/wiki/Rounding_error[^],
http://en.wikipedia.org/wiki/Truncation[^],
http://en.wikipedia.org/wiki/Rounding[^],
http://en.wikipedia.org/wiki/Precision_%28arithmetic%29[^].

Using approximate calculation is a big problem, more or less familiar to people working at numeric methods or numeric simulation, it''s very hard to overview in a short answer.

As to your "big question", I cannot fully analyze it, just because your code won''t compile, because of the last line. You may need to sort it out and explain the problem better, starting with your goal. I would only advise one general thing: don''t allow for a mix of variables with different precision. If you have to work with double and float, make sure all intermediate operations are done in double; cast the result in float only at the final stage of calculation. By the way, you should understand that precision of float is insufficient in almost all cases. It is usually used for presentation of some final results, such as screen output, storage of final results, graphics. Also remember: the rounding error can accumulate.

—SA


转换的执行方式取决于编译器和CPU.对于具有集成FPU的x86 CPU,FPU用于执行转换.要检查这一点,可以在编译时生成一个汇编程序输出文件:
How the conversion is performed is compiler and CPU specific. With x86 CPUs with integrated FPU, the FPU is used to perform the conversion. To check this, you can generate an assembler output file when compiling:
; 45   : 	double d = 1.23;
	fld	QWORD PTR __real@3ff3ae147ae147ae
	fstp	QWORD PTR _d


[ ebp ] ; 46:float f =(float)d; fld QWORD PTR _d
[ebp] ; 46 : float f = (float)d; fld QWORD PTR _d


这篇关于转换时双精度数字是否总是四舍五入为相同的浮点值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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