难道铸造双浮动总是返回相同的值? [英] Does casting double to float always return same value?

查看:122
本文介绍了难道铸造双浮动总是返回相同的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问铸造双击浮动总是产生相同的结果,还是能有一些整差异<? / p>

例如,为 X

 浮动X =(浮点)0.123456789d; 



总是相同的价值呢?



什么有关转换时,浮子双,然后铸造它回到浮动IE浏览器。 (浮点)(双)someFloat



在什么结果在C#中最感兴趣,但随时份额如果您对如何与其他的语言知识。


解决方案

结果不应该依赖于语言的,除非语言偏离从IEEE规范。



所有花车可以精确地表示为双打,所以从浮往返翻番浮动应该产生,你开始用相同的值。



同样,铸造任何双重价值浮动应该总是产生相同的结果,但是,当然,也有将截断为相同的浮点值许多不同的双重价值。


Does casting double to float always produce same result, or can there be some "rounding differences"?

For example, is x in

float x = (float)0.123456789d;

always the same value?

What about when casting float to double, and then casting it back to float ie. (float)(double)someFloat ?

Mostly interested in what the results are in C#, but feel free to share if you have knowledge about how this works on other languages.

解决方案

The results should not be language dependent, unless the language deviates from the IEEE specification.

All floats can be exactly represented as doubles, so the round trip from float to double to float should yield the same value that you started with.

Similarly, casting any double value to float should always yield the same result, but, of course, there are many different double values that would truncate to the same float value.

这篇关于难道铸造双浮动总是返回相同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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