增加了双到下一个最接近的值? [英] Increase a double to the next closest value?

查看:123
本文介绍了增加了双到下一个最接近的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是不是一个真实生活中的项目的问题;我只是好奇。

我们可以使用增量运算符的增加 INT 我++ )。您可以定义此操作为:结果
这增加最接近的值的变量 I 的。这是在这种情况下,只需+1。

但我想定义根据IEEE 754-2008系统在特定的范围内可用的双精度值的数量。我将能够建立在某些范围内演示了这些量的曲线图,看看它是如何下降。

我想应该是增加一倍,达到最接近的值比原来更大两倍的按位的方式。

我在维基百科是这样的:


  

双击precision例子

  0X 3ff0 0000 0000 0000 = 1
0X 3ff0 0000 0000 0001 = 1.0000000000000002,下一个较大编号> 1
0X 3ff0 0000 0000 0002 = 1.0000000000000004


在这里,你可以看到下一个更高的数字是通过增加二进制内容获得。但我不认为这会继续工作,因为双方案是这样的:

我想别的东西应该执行,使当所有的小数位的设置为一个最小涨幅。

也许这种操作有一个名字?有趣的参考?结果
任何信息欢迎:D

感谢


解决方案

  

在这里,你可以看到下一个更高的数字是通过增加二进制内容获得。但我不认为这会继续工作,因为双方案是这样的:


  
  

[图片省略]


  
  

我想别的东西应该执行,使当所有的小数位被设置为一个最小涨幅。


要第一个近似值,是的,这的确实的工作。

考虑标准化的正数:这是一个值的 M 的* 2 电子其中1< = M 的< 2,即 M = 1.xxxxxxx (二进制)。存储值的 1 之前被省略了存储的值的二进制点,因此,部分(或尾数或尾数)部分包括二进制点之后的位

让我们想象一下,只有4中的小数部分位,而不是52:存储值 1111 (二进制)再presents在小数部分 M = 1.1111 (二进制)。这当作一个整数递增它给 0000 的小数部分用随身携带。

但进进入指数,从而增加它。这是完全正确的:在 1.1111 * 2 电子,接下来我们预计数量为 10.0000 ,这确实是 1.0000 * 2 E + 1


我说:到第一个近似......再presentation转换为整数,递增,并转换回双,并正规格化数很好地工作。这也适用于正denormalised数(小于最小规格化数越小;这些都为0的指数,通常是隐藏的是外显位)。

它为负数,如果你的整数重新presentation也登录幅度;它通常不会。对于更典型的补,你必须减去一个增量负一倍。

最后,最终你会溢出最大规格化数并增加指数到无穷大和NaN范围。

有一个有趣的文章,其中包括这 rel=\"nofollow\">。

This isn't a question for a real-life project; I'm only curious.

We can increase an int using the increment operator (i++). You can define this operation as:
This increases the variable with the closest value to i. Which is in this case simply +1.

But I was thinking of defining the number of double values available in a specific range according the IEEE 754-2008 system. I would be able to set up a graph which demonstrates these amounts in some ranges and see how it is decreasing.

I guess there should be a bitwise way of increasing a double to the closest value greater than the original double.

What I found on Wikipedia is this:

Double precision examples

0x 3ff0 0000 0000 0000   = 1
0x 3ff0 0000 0000 0001   = 1.0000000000000002, the next higher number > 1
0x 3ff0 0000 0000 0002   = 1.0000000000000004

Here, you can see that the next higher number is obtained by increasing the binary content. But I don't think that this will keep working since the double scheme looks like this:

I think something else should be performed to make the smallest increase when all the fraction bits are set to one.

Maybe this operation has a name? Interesting references?
Any information is welcome :D

Thanks

解决方案

Here, you can see that the next higher number is obtained by increasing the binary content. But I don't think that this will keep working since the double scheme looks like this:

[picture omitted]

I think something else should be performed to make the smallest increase when all the fraction bits are set to one.

To a first approximation, yes, this does work.

Consider a normalised positive number: this is a value m * 2e where 1 <= m < 2 , i.e. m = 1.xxxxxxx (in binary) . The "1" before the binary point is omitted in the stored value, so the "fraction" (or "mantissa" or "significand") part of the stored value consists of the bits after the binary point.

Let's imagine that there are only 4 bits in the fraction part, rather than 52: the stored value 1111 (binary) represents in the fraction part m = 1.1111 (binary). Treating this as an integer and incrementing it gives a fraction part of 0000 with a carry.

But the carry goes into the exponent, which increments it. That's exactly right: after 1.1111 * 2e, the next number we expect is 10.0000, which is indeed 1.0000 * 2e+1 !


I said "to a first approximation"... converting the representation to an integer, incrementing, and converting back to a double, does work nicely for positive normalised numbers. It also works for positive denormalised numbers (smaller than the smallest normalised number; these have an exponent of 0 and the bit which is usually hidden is explicit).

It works for negative numbers if your integer representation is also sign-magnitude; which it usually won't be. For the more typical two's complement, you have to subtract one to "increment" a negative double.

Finally, eventually you will overflow the largest normalised number and increment the exponent into the infinity and NaN range.

There is an interesting article which covers this here.

这篇关于增加了双到下一个最接近的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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