您好,我对 Codewars 网站上的一项练习有疑问。我的工作是从 double 值中提取指数作为无符号数。但我不允许使用任何算术和二进制运算并包含任何额外的文件。那么我就不能使用字符:+-*/%&|^~<>#?。我认为解决这个问题的唯一方法是使用函数。您能给我一些如何完成这项任务的建议
int exponent(double d)
{
int result;
frexp(d,&result);
return result;
}
最佳答案
注意:我尚未测试以下内容,因此可能需要一些“调整”
#include <math.h>
#include <stdint.h>
typedef union
{
double f;
struct
{
uint64_t mantisa : 52;
uint64_t exponent : 11;
uint64_t sign : 1;
} parts;
} double_cast;
uint64_t normalizedExponent(double d)
{
double_cast mydouble.f = d;
return ( mydouble.parts.sign )? mydouble.parts.exponent << 1: mydouble.parts.exponent;
}
关于c - 从 double 值中提取指数作为无符号值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56611166/