如何通过最小增量(或接近它)改变浮点数?
我有一个 double
值 f
并且想要一种方法将其微调得稍微大一点(或小一点)以获得一个尽可能接近的新值与原始值相比,但仍严格大于(或小于)原始值.
I have a double
value f
and would like a way to nudge it very slightly larger (or smaller) to get a new value that will be as close as possible to the original but still strictly greater than (or less than) the original.
它不必关闭到最后一点――更重要的是,我所做的任何更改都保证会产生不同的值,而不是返回到原来的值.
It doesn't have to be close down to the last bit―it's more important that whatever change I make is guaranteed to produce a different value and not round back to the original.
推荐答案
检查你的 math.h 文件.如果你幸运的话,你有 nextafter
和 nextafterf
函数定义.它们以可移植且独立于平台的方式完全满足您的需求,并且是 C99 标准的一部分.
Check your math.h file. If you're lucky you have the nextafter
and nextafterf
functions defined. They do exactly what you want in a portable and platform independent way and are part of the C99 standard.
另一种方法(可能是备用解决方案)是将浮点数分解为尾数和指数部分.递增很简单:只需将尾数加一即可.如果你得到一个溢出,你必须通过增加你的指数来处理这个问题.递减的工作方式相同.
Another way to do it (could be a fallback solution) is to decompose your float into the mantissa and exponent part. Incrementing is easy: Just add one to the mantissa. If you get an overflow you have to handle this by incrementing your exponent. Decrementing works the same way.
编辑:正如评论中所指出的,只需在其二进制表示中增加浮点数就足够了.尾数溢出将增加指数,这正是我们想要的.
EDIT: As pointed out in the comments it is sufficient to just increment the float in it's binary representation. The mantissa-overflow will increment the exponent, and that's exactly what we want.
简而言之,这与 nextafter 所做的相同.
That's in a nutshell the same thing that nextafter does.
不过,这不会是完全可移植的.您将不得不处理字节顺序以及并非所有机器都具有 IEEE 浮点数的事实(好的 - 最后一个原因更具学术性).
This won't be completely portable though. You would have to deal with endianess and the fact that not all machines do have IEEE floats (ok - the last reason is more academic).
同时处理 NAN 和无限可能有点棘手.您不能简单地增加它们,因为它们根据定义而不是数字.
Also handling NAN's and infinites can be a bit tricky. You cannot simply increment them as they are by definition not numbers.
相关文章