如果 A<B == true 如果 A 和 B 是双打。它我复制到位到两个 uint64_t 将 A<B 仍然是真的
If A<B == true if A and B are Doubles. It I copy to the Bits to two uint64_t will A<B still be true
我正在用一些加密库和零知识证明做一些事情,我使用的库只支持整数。
如果我有两个数字存储为双精度浮点数,我将每个数字的位复制到一个整数中,然后比较整数。
A_Double>B_Double==A_UINT>B_UINT
对于所有值。
我的猜测是它可能适用于所有正值,但不适用于负值。
虽然如果我翻转第一位可能会起作用。
您没有说明使用的浮点格式。 IEEE 754 和大多数浮点系统使用有效符号和大小的格式。最重要位置的位是符号位。取反值是通过翻转符号位来完成的。相比之下,通过翻转 所有 位并加一来对二进制补码格式的值取反。
浮点格式通常在符号位之后是表示值的下一个最重要的位、指数位,然后是有效位。这样做的结果是,对于两个正浮点数 x 和 y,x > y 等价于 x > y,其中 x 和 y 是 x 和 y[=28= 的位的重新解释] 作为无符号或二进制补码整数。但是,对于两个负浮点数,x > y 等价于 x < y,无论是无符号数还是二进制补码。
此外,浮点格式通常具有非数字值,称为 NaN。对于这些值,整数比较会失败,因为对于所有比较,浮点比较都被定义为假——NaN 不小于、等于或大于数字或 NaN。
总而言之,您通常不希望使用位的整数解释来比较浮点值。
接近尾声
关于浮点编码有很多假设:
当 a < 0
的整数版本翻转 value,折叠在 0footnote 所以 +0.0 和 -0.0 比较平等。 FP 值通常编码为 sign-magnitude.
同时查找 Nan 编码。
A C 溶液
#include <stdint.h>
#include <stdlib.h>
_Static_assert(sizeof(int64) == sizeof(double), "Unexpected sizes");
#define INT_NAN_TEST(a) (llabs(a) > 0x7FF0000000000000u)
/*
* Compare 2 integers as is they overlay a same size, same endian 64-bit `double`.
* Return -1,0,1 when they compare a > b, a===b, a< b
* or `'?` for un-comparable as at least one of `a,b` is NaN
*
* This code assumes a lot about the representation of a `double`.
* Same size
* Same endian
* FP with leading sign bit, then a biased exponent, then significand
*/
int cmp_double(int64_t a, int64_t b) {
if (a < 0) {
a = 0x8000000000000000 - a;
}
if (b < 0) {
b = 0x8000000000000000 - b;
}
if (INT_NAN_TEST(a) || INT_NAN_TEST(b))
return '?';
return (a > b) - (a < b);
}
测试
#include <stdlib.h>
#include <stdio.h>
#include <float.h>
#include <limits.h>
#include <string.h>
#include <math.h>
int main() {
const double d[] = {0.0, DBL_TRUE_MIN, DBL_MIN, 1, DBL_MAX, INFINITY, -0.0,
-DBL_TRUE_MIN, -DBL_MIN, -1, -DBL_MAX, -INFINITY, NAN};
size_t n = sizeof d / sizeof *d;
for (size_t i = 0; i < n; i++) {
double a = d[i];
int64_t ai;
memcpy(&ai, &a, sizeof ai);
for (size_t bb = 0; bb < n; bb++) {
double b = d[bb];
int64_t bi;
memcpy(&bi, &b, sizeof bi);
int cmp_d = (isnan(a) || isnan(b)) ? '?' : (a > b) - (a < b);
int cmp_i = cmp_double(ai, bi);
if (cmp_d != cmp_i) {
printf("% 20g %16llX % 20g %16llX: %2d %2d\n", a, 1llu * ai, b,
1llu * bi, cmp_d, cmp_i);
}
}
}
puts("Done");
}
footnote 通过折叠负数使 0000_0000_0000_0000 和 8000_0000_0000_0000 都是“零”,最负的“双倍整数”值将是比最负的可编码整数多 1,从而防止 llabs()
计算中的 UB。
我正在用一些加密库和零知识证明做一些事情,我使用的库只支持整数。
如果我有两个数字存储为双精度浮点数,我将每个数字的位复制到一个整数中,然后比较整数。
A_Double>B_Double==A_UINT>B_UINT
对于所有值。
我的猜测是它可能适用于所有正值,但不适用于负值。
虽然如果我翻转第一位可能会起作用。
您没有说明使用的浮点格式。 IEEE 754 和大多数浮点系统使用有效符号和大小的格式。最重要位置的位是符号位。取反值是通过翻转符号位来完成的。相比之下,通过翻转 所有 位并加一来对二进制补码格式的值取反。
浮点格式通常在符号位之后是表示值的下一个最重要的位、指数位,然后是有效位。这样做的结果是,对于两个正浮点数 x 和 y,x > y 等价于 x > y,其中 x 和 y 是 x 和 y[=28= 的位的重新解释] 作为无符号或二进制补码整数。但是,对于两个负浮点数,x > y 等价于 x < y,无论是无符号数还是二进制补码。
此外,浮点格式通常具有非数字值,称为 NaN。对于这些值,整数比较会失败,因为对于所有比较,浮点比较都被定义为假——NaN 不小于、等于或大于数字或 NaN。
总而言之,您通常不希望使用位的整数解释来比较浮点值。
接近尾声
关于浮点编码有很多假设:
当 a < 0
的整数版本翻转 value,折叠在 0footnote 所以 +0.0 和 -0.0 比较平等。 FP 值通常编码为 sign-magnitude.
同时查找 Nan 编码。
A C 溶液
#include <stdint.h>
#include <stdlib.h>
_Static_assert(sizeof(int64) == sizeof(double), "Unexpected sizes");
#define INT_NAN_TEST(a) (llabs(a) > 0x7FF0000000000000u)
/*
* Compare 2 integers as is they overlay a same size, same endian 64-bit `double`.
* Return -1,0,1 when they compare a > b, a===b, a< b
* or `'?` for un-comparable as at least one of `a,b` is NaN
*
* This code assumes a lot about the representation of a `double`.
* Same size
* Same endian
* FP with leading sign bit, then a biased exponent, then significand
*/
int cmp_double(int64_t a, int64_t b) {
if (a < 0) {
a = 0x8000000000000000 - a;
}
if (b < 0) {
b = 0x8000000000000000 - b;
}
if (INT_NAN_TEST(a) || INT_NAN_TEST(b))
return '?';
return (a > b) - (a < b);
}
测试
#include <stdlib.h>
#include <stdio.h>
#include <float.h>
#include <limits.h>
#include <string.h>
#include <math.h>
int main() {
const double d[] = {0.0, DBL_TRUE_MIN, DBL_MIN, 1, DBL_MAX, INFINITY, -0.0,
-DBL_TRUE_MIN, -DBL_MIN, -1, -DBL_MAX, -INFINITY, NAN};
size_t n = sizeof d / sizeof *d;
for (size_t i = 0; i < n; i++) {
double a = d[i];
int64_t ai;
memcpy(&ai, &a, sizeof ai);
for (size_t bb = 0; bb < n; bb++) {
double b = d[bb];
int64_t bi;
memcpy(&bi, &b, sizeof bi);
int cmp_d = (isnan(a) || isnan(b)) ? '?' : (a > b) - (a < b);
int cmp_i = cmp_double(ai, bi);
if (cmp_d != cmp_i) {
printf("% 20g %16llX % 20g %16llX: %2d %2d\n", a, 1llu * ai, b,
1llu * bi, cmp_d, cmp_i);
}
}
}
puts("Done");
}
footnote 通过折叠负数使 0000_0000_0000_0000 和 8000_0000_0000_0000 都是“零”,最负的“双倍整数”值将是比最负的可编码整数多 1,从而防止 llabs()
计算中的 UB。