20

标题非常不言自明,输入被赋予双倍值,我想加/减尽可能少的数量。

4

4 回答 4

28

您可以使用nextafter,如果您的编译器实现了 C99 的数学函数(即 C++11 及更高版本),则可用。这个函数(及其各种重载)可以描述为:

double nextafter(double value, double target);

它将以尽可能小的量从value 的方向移动(通常通过调整浮点数的位表示)。 target如果value已经在target,则什么也不做。

如果target大于value这将增加value最小的可能量。如果targetvalue于此值,将减少value可能的最小量。

常见的用法是传递DBL_MAXINFINITY作为目标以增加最小数量(或否定它们以减少最小数量)。

DBL_MAX和之间的选择INFINITY取决于你想在边界上做什么 -nextafter(DBL_MAX, DBL_MAX) == DBL_MAX但是nextafter(DBL_MAX, INFINITY) == INFINITY

是的,它的名字很糟糕。另见nextafternexttoward:为什么这个特殊的界面?.


#include <cfloat> // DBL_MAX
#include <cmath> // std::nextafter

double x = 0.1;

// Next representable number **after** x in the direction of DBL_MAX.
// (i.e., this is larger than x, and there is no double between it and x)
double xPlusSmallest = std::nextafter(x, DBL_MAX); 

// Next representable number **before** x in the direction of -DBL_MAX.
// (i.e., this is smaller than x, and there is no double between it and x)
double xMinusSmallest = std::nextafter(x, -DBL_MAX); 

即使你的编译器不支持它,它也可能有它的内在特性。(例如,MSVC_nextafter自 2005 年以来就有。GCC 可能将其作为标准实现。)

如果您的编译器不支持它,但您可以使用 Boost,您可以这样做:

#include <boost/math/special_functions/next.hpp>
#include <cfloat> 

double x = 0.1;

double xPlusSmallest = boost::math::nextafter(x, DBL_MAX); 
double xMinusSmallest = boost::math::nextafter(x, -DBL_MAX); 

如果这些都不适合您,您只需打开 Boost 标头并复制它。

于 2012-04-15T07:13:23.433 回答
7

这是一个非常肮脏的技巧,实际上是不合法的,并且仅在您的平台使用 IEEE754 浮点数时才有效:浮点数的二进制表示形式与浮点值的排序方式相同,因此您可以递增二进制表示形式:

double x = 1.25;

uint64_t * const p = reinterpret_cast<uint64_t*>(&x);

++*p;   // undefined behaviour! but it gets the next value

// now x has the next value

通过执行通常的二进制复制体操以获得适当的uint64_t值,您可以完全合法地达到相同的效果。确保也正确检查零、无穷大和 NaN。

于 2012-04-15T08:51:18.580 回答
-4

怎么样:

x += fabs(x) * std::numeric_limits<double>::epsilon();
于 2012-04-15T09:04:28.630 回答
-5
#define FLT_MIN         1.175494351e-38F        /* min positive value */
#define FLT_MAX         3.402823466e+38F        /* max value */
#define DBL_MIN         2.2250738585072014e-308 /* min positive value */
#define DBL_MAX         1.7976931348623158e+308 /* max value */

http://xona.com/2006/07/26.html

于 2012-04-15T06:49:09.170 回答