c - 从 double 值中提取指数作为无符号值

标签 c

您好,我对 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/

相关文章:

c - 在给定位置插入节点

c - 从 void 双指针数组输出 void 指针

c - Linux 信号带有额外的信息参数

c - 通过 epoll 处理监听套接字

c - 程序在屏幕上显示奇怪的字符

无法将数组值排入队列

python - 对需要另一个扩展的 Python 进行 C 扩展

由于函数结构重复而导致的 c 段错误

c - 符合 IEEE754 的结构

C 将链表写入文件