有效地提取双*的小数部分*用C [英] Extract fractional part of double *efficiently* in C

查看:116
本文介绍了有效地提取双*的小数部分*用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待采取IEEE双并删除以最有效的方式是任何整数部分可能的。

I'm looking to take an IEEE double and remove any integer part of it in the most efficient manner possible.

我想

1035 ->0
1045.23->0.23
253e-23=253e-23

我不关心正确地处理非正规,无穷大或NaN的。我不介意位操作,因为我知道我与IEEE合作双打,因此它应该在整个机器工作。

I do not care about properly handling denormals, infinities, or NaNs. I do not mind bit twiddling, as I know I am working with IEEE doubles, so it should work across machines.

网点code将远preferred。

Branchless code would be much preferred.

我首先想到的是(以伪code)

My first thought is (in pseudo code)

char exp=d.exponent;
(set the last bit of the exponent to 1)
d<<=exp*(exp>0);
(& mask the last 52 bits of d)
(shift d left until the last bit of the exponent is zero, decrementing exp each time)
d.exponent=exp;

但问题是,我想不出一个有效的方式为D向左移动,直到指数的最后一位是零,再加上现在看来,这将需要输出的零,如果所有的最后位未设置。这似乎与在基座2的对数问题。

But the problem is that I can't think of an efficient way to shift d left until the last bit of the exponent is zero, plus it seems it would need to output zero if all of the last bits weren't set. This seems to be related to the base 2 logarithm problem.

帮助这个算法或任何更好的将是更AP preciated。

Help with this algorithm or any better ones would be much appreciated.

我也许应该注意到,我之所以想网点code是因为我希望它能够有效地量化。

I should probably note that the reason I want branchless code is because I want it to efficiently vectorize.

推荐答案

如何简单的东西?

double fraction = whole - ((long)whole);

此只减去值本身的双重的整数部分,剩余部分应该是小数部分。这是可能的,当然,这可能有一些重新presentation的问题。

This just subtracts the integer portion of the double from the value itself, the remainder should be the fractional component. It's possible, of course, this could have some representation issues.

这篇关于有效地提取双*的小数部分*用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆