- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
标题是不言自明的,输入是 double 值,我想加/减尽可能少的数量。
最佳答案
您可以使用 nextafter
,如果您的编译器实现了 C99 的数学函数(即 C++11 及更高版本),则可以使用该功能。这个函数(及其各种重载)可以描述为:
double nextafter(double value, double target);
它将从 value
向 target
方向移动尽可能小的量(通常通过调整 float 的位表示) .如果 value
已经在 target
处,则什么也不做。
如果target
大于value
,这将增加value
的最小量。如果 target
小于 value
这将减少 value
的最小量。
常见的用法是传递 DBL_MAX
或 INFINITY
作为目标以增加最小数量(或否定它们以减少最小数量)。
DBL_MAX
和 INFINITY
之间的选择取决于您要在边界处执行的操作 - nextafter(DBL_MAX, DBL_MAX) == DBL_MAX
, 但是 nextafter(DBL_MAX, INFINITY) == INFINITY
.
是的,它的名字很糟糕。另见 nextafter
and nexttoward
: why this particular interface? .
#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 自 2005 年以来就有 _nextafter
。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 头文件并复制它。
关于c++ - 如何找到最近的下一个/上一个 double 值(numeric_limits::epsilon 对于给定的数字),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10160079/
例如,我有一个父类Author: class Author { String name static hasMany = [ fiction: Book,
代码如下: dojo.query(subNav.navClass).forEach(function(node, index, arr){ if(dojo.style(node, 'd
我有一个带有 Id 和姓名的学生表和一个带有 Id 和 friend Id 的 Friends 表。我想加入这两个表并找到学生的 friend 。 例如,Ashley 的 friend 是 Saman
我通过互联网浏览,但仍未找到问题的答案。应该很容易: class Parent { String name Child child } 当我有一个 child 对象时,如何获得它的 paren
我正在尝试创建一个以 Firebase 作为我的后端的社交应用。现在我正面临如何(在哪里?)找到 friend 功能的问题。 我有每个用户的邮件地址。 我可以访问用户的电话也预订。 在传统的后端中,我
我主要想澄清以下几点: 1。有人告诉我,在 iOS 5 及以下版本中,如果您使用 Game Center 设置多人游戏,则“查找 Facebook 好友”(如与好友争夺战)的功能不是内置的,因此您需要
关于redis docker镜像ENTRYPOINT脚本 docker-entrypoint.sh : #!/bin/sh set -e # first arg is `-f` or `--some-
我是一名优秀的程序员,十分优秀!