如何通过最小增量(或接近它)改变浮点数?

How to alter a float by its smallest increment (or close to it)?(如何通过最小增量(或接近它)改变浮点数?)
本文介绍了如何通过最小增量(或接近它)改变浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 doublef 并且想要一种方法将其微调得稍微大一点(或小一点)以获得一个尽可能接近的新值与原始值相比,但仍严格大于(或小于)原始值.

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 文件.如果你幸运的话,你有 nextafternextafterf 函数定义.它们以可移植且独立于平台的方式完全满足您的需求,并且是 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.

这篇关于如何通过最小增量(或接近它)改变浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)