gpt4 book ai didi

c# - float 范围内最大的不准确度是多少?

转载 作者:行者123 更新时间:2023-11-30 20:03:14 24 4
gpt4 key购买 nike

给定两个浮点值(fLowfHigh),您如何计算两个连续值之间的最大或最大步幅/间隙?

例如:

16777217f20000000f 的范围内,答案将是 2,因为值实际上四舍五入到最接近的两位。

将其推广到任意范围让我摸不着头脑——有什么建议吗?

干杯,

这应该是语言中立的,但我使用的是 C#(我认为它符合 IEEE-754)。

最佳答案

这是在 C 中。它需要一些 IEEE 754 行为,用于舍入等。对于 IEEE 754 64 位二进制(double),SmallestPositive 为 2-1074,大约为 4.9406564584124654417656879286822137236505980261e-324,DBL_EPSILON 为 2-52, 2.220446049250313080847263336181640625e-16。对于 32 位二进制(float),将 DBL 更改为 FLT 并将 double 更改为 float 它们出现的任何地方(以及 fabsfabsffmaxfmaxf,尽管它应该在没有这些更改的情况下工作). Then SmallestPositive is 2-149, approximately 1.401298464324817070923729583289916131280261941876515771757068283889791e-45, and FLT_EPSILON is 2-23, 1.1920928955078125e-07.

对于两个值之间的区间,最大步长当然是端点处幅度较大的步长。 (如果该端点恰好是 2 的幂,则从该点到下一个点的步长不会出现在区间本身中,因此这将是一种特殊情况。)

#include <float.h>
#include <math.h>

/* Return the ULP of q.

This was inspired by Algorithm 3.5 in Siegfried M. Rump, Takeshi Ogita, and
Shin'ichi Oishi, "Accurate Floating-Point Summation", _Technical Report
05.12_, Faculty for Information and Communication Sciences, Hamburg
University of Technology, November 13, 2005.
*/
double ULP(double q)
{
// SmallestPositive is the smallest positive floating-point number.
static const double SmallestPositive = DBL_EPSILON * DBL_MIN;

/* Scale is .75 ULP, so multiplying it by any significand in [1, 2) yields
something in [.75 ULP, 1.5 ULP) (even with rounding).
*/
static const double Scale = 0.75 * DBL_EPSILON;

q = fabs(q);

return fmax(SmallestPositive, q - (q - q * Scale));
}

关于c# - float 范围内最大的不准确度是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15536200/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com