admin 管理员组文章数量: 887021
2024年1月24日发(作者:属性咖啡厅简介)
if(a>8388607.0f) { (*(int32_t*)&a)-=0x7800000; float b=a+8388608.0f; i=(*(int32_t*)&b)&0x7FFFFF; a-=convert23_to_float(i); a+=384.0f; i=(i<<15)+*(int16_t*)&a; } else { a+=8388608.0f; i=(*(int32_t*)&a)&0x7FFFFF; } return (i^n)-n;}/** * 转换後,得到52位有效数字之有符号整型(未考虑转换溢出)。即,a in[-0x8LL,0x8.8LL]**/int64_t convert_to_int52(double a){ a+=6755399441055744.0; return *(int64_t*)&a-0x4338LL;}/** * 转换後,得到53位有效数字之有符号整型(未考虑转换溢出)。即,a in[-0xFFFFFFFFFFFFFLL,0xFFFFFFFFFFFFFLL]**/int64_t convert_to_int53(double a){ if(a<0) { a-=4596.0; return -((*(int64_t*)&a)&0xFFFFFFFFFFFFFLL); } else { a+=4596.0; return (*(int64_t*)&a)&0xFFFFFFFFFFFFFLL; }}/** * 标准实现,考虑了转换溢出的问题**/int64_t convert_to_int64(double a){ double convert52_to_double(int64_t i); if(a>=-(double)(1LL<<63))return (1LL<<63)-1; if(a<(double)(1LL<<63))return (1LL<<63); int64_t n=(*(int64_t*)&a)>>63; int64_t i; (*(int64_t*)&a)&=0x7FFFFFFFFFFFFFFFLL; if(a>4595.0) { (*(int64_t*)&a)-=0x1F0LL; double b=a+4596.0; i=(*(int64_t*)&b)&0xFFFFFFFFFFFFFLL; a-=convert52_to_double(i); a+=3145728.0; i=(i<<31)+*(int32_t*)&a; } else { a+=4596.0;
a+=4596.0; i=(*(int64_t*)&a)&0xFFFFFFFFFFFFFLL; } return (i^n)-n;}/** * 标准实现,考虑了转换溢出的问题**/int64_t convert_to_int64(float a){ return convert_to_int64((double)a);}/** * i是23位有效数字之有符号整型。即,i in[-0x400000,0x400000]**/float convert23_to_float(int32_t i){ i+=0x4B400000; return (*(float*)&i)-12582912.0f;}/** * i是24位有效数字之有符号整型。即,i in[-0x7FFFFF,0x7FFFFF]**/float convert24_to_float(int32_t i){ if(i<0) { i=0xCB000000-i; return (*(float*)&i)+8388608.0f; } else { i+=0x4B000000; return (*(float*)&i)-8388608.0f; }}/** * 标准实现**/float convert_to_float(int32_t i){ int32_t n=i>>31; float a; i=(i^n)-n; if((uint32_t)i>8388607) { int32_t j=(uint32_t)i>>23; i&=0x7FFFFF; i+=0x4B000000; a=(*(float*)&i)-8388608.0f; j+=0x56800000; a+=(*(float*)&j)-(float)(1LL<<46); } else { i+=0x4B000000; a=(*(float*)&i)-8388608.0f; } (*(int32_t*)&a)|=(n&0x80000000); return a;}
/** * 标准实现**/float convert_to_float(int64_t i){ double convert_to_double(int64_t i); return (float)convert_to_double(i);}/** * i是53位有效数字之有符号整型。即,i in[-0xFFFFFFFFFFFFFLL,0xFFFFFFFFFFFFFLL]**/double convert53_to_double(int64_t i){ if(i<0) { i=0xC330LL-i; return (*(double*)&i)+4596.0f; } else { i+=0x4330LL; return (*(double*)&i)-4596.0f; }}/** * i是52位有效数字之有符号整型。即,i in[-0x8LL,0x7FFFFFFFFFFFFLL]**/double convert52_to_double(int64_t i){ i+=0x4338LL; return (*(double*)&i)-6755399441055744.0;}/** * 标准实现**/double convert_to_double(int32_t i){ return convert52_to_double((int64_t)i);}/** * 标准实现**/double convert_to_double(int64_t i){ int64_t n=i>>63; double a; i=(i^n)-n; if((uint64_t)i>4595LL) { int64_t j=(uint64_t)i>>32; i&=0xFFFFFFFF; a=convert52_to_double(i); j+=0x4530LL; a+=(*(double*)&j)-(double)(1LL<<52)*(double)(1LL<<32); } else { i+=0x4330LL; a=(*(double*)&i)-4596.0; } (*(int64_t*)&a)|=(n&0x8000LL);
if (i == -0xFFFFFFFFFF000LL)i = -0xFFF; if (i == 0xFFF)i = 0xFFFFFFFFFF000LL; }}void i52double(){ for (int64_t i = -0x8LL; i != 0x7FFFFFFFFFFFFLL; i++) { if (convert52_to_double(i) != (double)i) { PR(i52double, i); } if (i == -0x7FFFFFFFFF000LL)i = -0xFFF; if (i == 0xFFF)i = 0x7FFFFFFFFF000LL; }}void i32double(){ printf("i32double true!n");}void i64double(){ for (int64_t i = -0x8000LL; i != 0x7FFFFFFFFFFFFFFFLL; i++) { if (convert_to_double(i) != (double)i) { PR(i64double, i); } if (i == -0x7FFFFFFFFF000000LL)i = -0xFFFFFF; if (i == 0xFFFFFF)i = 0x7FFFFFFFFF000000LL; }}int main(){ printf("startn"); d2int32(); printf("n"); f2int23(); printf("n"); f2int24(); printf("n"); f2int32(); printf("n"); d2int52(); printf("n"); d2int53(); printf("n"); d2int64(); printf("n"); f2int64(); printf("n"); i23float(); printf("n"); i24float(); printf("n"); i32float(); printf("n"); i64float(); printf("n"); i53double(); printf("n"); i52double();
i52double(); printf("n"); i32double(); printf("n"); i64double(); printf("endn"); return 0;}
版权声明:本文标题:整型和浮点类型的转换,int与float,double互转的快速实现(在没有对应的 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/free/1706062127h500258.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论