Ken*_*ihe 2 c binary double types 128-bit
我有以下代码...
ran_int = rand(); //check READ ME Citation B
if (input == 32){
//rand() with 2^31 as lower limit and 2^32 as its upper
long long int upper_limit = 4294967295;
long long int lower_limit = 2147483649
ran_32_64 = (rand() * 2) % (upper_limit - lower_limit);
ran_32_64 += lower_limit;
}
else if(input == 64){
//rand() x 4 with 2^63 as lower limit and 2^64 as its upper
unsigned long long int upper_limit = powl(2, 64);
long long int lower_limit = powl(2, 63);
ran_32_64 = (rand() * 4) % (upper_limit - lower_limit);
ran_32_64 += lower_limit;
}
else if(input == 128){
//rand() x 8 with 2^127 as lower limit
unsigned _int128 upper_limit =
}
Run Code Online (Sandbox Code Playgroud)
对于最后一个块,我需要表示一个 128 位二进制数。我知道我可以使用 _int128 类型来表示 128 位值,但这并不完全精确。
所以我想完全精确地我可以使用 double 类型,但我找不到任何关于 128 位 double 类型的文献。
有任何想法吗?
如果它是 128b,则它不是双精度数,但 GNU 和 Intel C/C++ 编译器都支持 128b 浮点通过__float128(较旧的 Intel C/C++ 编译器有Quad)。
GCC 文档非常详尽。
有库实现包括:
很难说出您的代码想要做什么,但下面是与您之前编写的代码等效的 128 位整数。
老实说,我不知道您打算在哪里使用任何宽度的浮点数,因此如果您需要 128b 浮点数的详细信息,请提供更合适的示例。
#include <stdint.h>
#include <stdlib.h>
#include <math.h>
#include <quadmath.h>
void foo(int input) {
int ran_int = rand(); //check READ ME Citation B
if (input == 32){
//rand() with 2^31 as lower limit and 2^32 as its upper
uint32_t upper_limit = 4294967295;
int32_t lower_limit = 2147483649;
uint32_t ran_32_64 = (rand() * 2) % (upper_limit - lower_limit);
ran_32_64 += lower_limit;
}
/*
else if(input == 64){
//rand() x 4 with 2^63 as lower limit and 2^64 as its upper
uint64_t upper_limit = powl(2L, 64);
int64_t lower_limit = powl(2L, 63);
uint64_t ran_32_64 = (rand() * 4) % (upper_limit - lower_limit);
ran_32_64 += lower_limit;
}
*/
else if(input == 128){
//rand() x 8 with 2^127 as lower limit
unsigned __int128 upper_limit = powq(2, 64);
__int128 lower_limit = powq(2, 63);
unsigned __int128 ran_32_64 = (rand() * 4) % (upper_limit - lower_limit);
ran_32_64 += lower_limit;
}
}
Run Code Online (Sandbox Code Playgroud)