如何检查是否双重可以放入一个浮动无需转换到无穷远 [英] How to check if a double can fit into a float without conversion to infinity

查看:194
本文介绍了如何检查是否双重可以放入一个浮动无需转换到无穷远的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有检查一个64位浮点数可转换成32位的一个没有被转换为+/-无穷?

Is there a standard way to check if a 64 bit floating point number can be converted to a 32 bit one without being converted to +/- Infinity?

我的标准方式知道这事后能为+/-天道酬勤进行检查。

I know it can be checked afterwards for +/- Inf.

Convert.ToSingle()将无法做到这一点

推荐答案

为了测试是否双击 D 将转换成浮动不产生无穷,您可以测试是否 D 的绝对值严格低于双击写成 3.4028235677973366e + 38 (即 0x1.ffffffp + 127 十六进制)。这个数字是双打可以转换到浮动和那些不能之间的精确限

In order to test whether a double value d will convert to float without yielding infinity, you can test whether d's absolute value is strictly below the double written as 3.4028235677973366e+38 (that is, 0x1.ffffffp+127 in hexadecimal). This number is the exact limit between doubles that can be converted to float and those that cannot.

要扩大一点,最后有限浮动是:

To expand a little, the last finite float is:

0x1.fffffep+127   (3.4028234663852886e+38 in decimal)

什么是下一个浮动号码,如果键入浮动有更多的指数位是:

What would be the next float number if type float had more exponent bits is:

0x2.000000p+127   (3.4028236692093846e+38 in decimal)

这比后者更靠近前被向下舍入到前者的数字。该限制是两个数字的中点:

The numbers that are closer to the former than to the latter are rounded down to the former. The limit is the midpoint of the two numbers:

0x1.ffffffp+127   (3.4028235677973366e+38 in decimal)

这篇关于如何检查是否双重可以放入一个浮动无需转换到无穷远的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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