gpt4 book ai didi

c++ - 作为有理数的 float 的精确值

转载 作者:太空狗 更新时间:2023-10-29 21:11:14 26 4
gpt4 key购买 nike

我正在寻找一种方法将 float 的精确值转换为两个整数的有理数商,即 a / b ,其中 b 不大于指定的最大分母 b_max 。如果满足条件 b <= b_max 是不可能的,那么结果会回落到仍然满足条件的最佳近似值。

等等。这里有很多关于截断实数的最佳有理逼近的问题/答案,表示为一个 float 。不过,我对 float 的精确 值很感兴趣,它本身就是一个具有不同表示形式的有理数。更具体地说, float 的数学集是有理数的子集。在 IEEE 754 二进制浮点标准的情况下,它是 dyadic rationals 的子集。无论如何,任何 float 都可以转换为两个有限精度整数的有理商,如 a / b

因此,例如假设 IEEE 754 单精度二进制浮点格式,float f = 1.0f / 3.0f 的合理等价物不是 1 / 3 ,而是 11184811 / 33554432 。这是 f精确值,它是 IEEE 754 单精度二进制 float 数学集中的一个数字。

根据我的经验,遍历(通过二进制搜索)Stern-Brocot tree 在这里没有用,因为当 float 被解释为 截断实数 而不是精确的理性

可能,continued fractions 是要走的路。

这里的另一个问题是整数溢出。想一想我们想要将有理数表示为两个 int32_t 的商,其中最大分母 b_max = INT32_MAX。我们不能依赖像 b > b_max 这样的停止标准。因此该算法绝不能溢出,或者它必须检测到溢出。

到目前为止我发现的an algorithm from Rosetta Code ,它基于连分数,但它的来源提到它“仍然不完全”。一些基本测试给出了不错的结果,但我无法确认它的整体正确性,我认为它很容易溢出。

// https://rosettacode.org/wiki/Convert_decimal_number_to_rational#C

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdint.h>

/* f : number to convert.
* num, denom: returned parts of the rational.
* md: max denominator value. Note that machine floating point number
* has a finite resolution (10e-16 ish for 64 bit double), so specifying
* a "best match with minimal error" is often wrong, because one can
* always just retrieve the significand and return that divided by
* 2**52, which is in a sense accurate, but generally not very useful:
* 1.0/7.0 would be "2573485501354569/18014398509481984", for example.
*/
void rat_approx(double f, int64_t md, int64_t *num, int64_t *denom)
{
/* a: continued fraction coefficients. */
int64_t a, h[3] = { 0, 1, 0 }, k[3] = { 1, 0, 0 };
int64_t x, d, n = 1;
int i, neg = 0;

if (md <= 1) { *denom = 1; *num = (int64_t) f; return; }

if (f < 0) { neg = 1; f = -f; }

while (f != floor(f)) { n <<= 1; f *= 2; }
d = f;

/* continued fraction and check denominator each step */
for (i = 0; i < 64; i++) {
a = n ? d / n : 0;
if (i && !a) break;

x = d; d = n; n = x % n;

x = a;
if (k[1] * a + k[0] >= md) {
x = (md - k[0]) / k[1];
if (x * 2 >= a || k[1] >= md)
i = 65;
else
break;
}

h[2] = x * h[1] + h[0]; h[0] = h[1]; h[1] = h[2];
k[2] = x * k[1] + k[0]; k[0] = k[1]; k[1] = k[2];
}
*denom = k[1];
*num = neg ? -h[1] : h[1];
}

最佳答案

所有有限的 double 都是 rational numbers,正如 OP 所述......

使用 frexp() 将数字分解为分数和指数。由于范围的要求,最终结果还是需要用double来表示整数值。有些数字太小,(x 小于 1.0/(2.0,DBL_MAX_EXP))和无穷大,非数字是问题。

The frexp functions break a floating-point number into a normalized fraction and an integral power of 2. ... interval [1/2, 1) or zero ...
C11 §7.12.6.4 2/3

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

_Static_assert(FLT_RADIX == 2, "TBD code for non-binary FP");

// Return error flag
int split(double x, double *numerator, double *denominator) {
if (!isfinite(x)) {
*numerator = *denominator = 0.0;
if (x > 0.0) *numerator = 1.0;
if (x < 0.0) *numerator = -1.0;
return 1;
}
int bdigits = DBL_MANT_DIG;
int expo;
*denominator = 1.0;
*numerator = frexp(x, &expo) * pow(2.0, bdigits);
expo -= bdigits;
if (expo > 0) {
*numerator *= pow(2.0, expo);
}
else if (expo < 0) {
expo = -expo;
if (expo >= DBL_MAX_EXP-1) {
*numerator /= pow(2.0, expo - (DBL_MAX_EXP-1));
*denominator *= pow(2.0, DBL_MAX_EXP-1);
return fabs(*numerator) < 1.0;
} else {
*denominator *= pow(2.0, expo);
}
}

while (*numerator && fmod(*numerator,2) == 0 && fmod(*denominator,2) == 0) {
*numerator /= 2.0;
*denominator /= 2.0;
}
return 0;
}

void split_test(double x) {
double numerator, denominator;
int err = split(x, &numerator, &denominator);
printf("e:%d x:%24.17g n:%24.17g d:%24.17g q:%24.17g\n",
err, x, numerator, denominator, numerator/ denominator);
}

int main(void) {
volatile float third = 1.0f/3.0f;
split_test(third);
split_test(0.0);
split_test(0.5);
split_test(1.0);
split_test(2.0);
split_test(1.0/7);
split_test(DBL_TRUE_MIN);
split_test(DBL_MIN);
split_test(DBL_MAX);
return 0;
}

输出

e:0 x:      0.3333333432674408 n:                11184811 d:                33554432 q:      0.3333333432674408
e:0 x: 0 n: 0 d: 9007199254740992 q: 0
e:0 x: 1 n: 1 d: 1 q: 1
e:0 x: 0.5 n: 1 d: 2 q: 0.5
e:0 x: 1 n: 1 d: 1 q: 1
e:0 x: 2 n: 2 d: 1 q: 2
e:0 x: 0.14285714285714285 n: 2573485501354569 d: 18014398509481984 q: 0.14285714285714285
e:1 x: 4.9406564584124654e-324 n: 4.4408920985006262e-16 d: 8.9884656743115795e+307 q: 4.9406564584124654e-324
e:0 x: 2.2250738585072014e-308 n: 2 d: 8.9884656743115795e+307 q: 2.2250738585072014e-308
e:0 x: 1.7976931348623157e+308 n: 1.7976931348623157e+308 d: 1 q: 1.7976931348623157e+308

b_max 稍后再考虑。


pow(2.0, expo) 替换为 ldexp(1, expo) @gammatesterexp2(expo) 可以使用更方便的代码@Bob__

while (*numerator && fmod(*numerator,2) == 0 && fmod(*denominator,2) == 0) 也可以使用一些性能改进。但首先,让我们根据需要获取功能。

关于c++ - 作为有理数的 float 的精确值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51142275/

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