避免浮动 [英] Avoiding float

查看:78
本文介绍了避免浮动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做一些长整数的乘法并且除了
除以10。我可以避免使用花车吗?我可以使用两个long作为64位值

吗?谢谢。

解决方案

ga *** **@hotmail.com 写道:


我需要做几次长整数并且有一个鸿沟

十点。我可以避免使用花车吗?我可以使用两个long作为64位值

吗?谢谢。



嗨!


你能说清楚你想做什么吗? :-)


康斯坦丁


文章< 11 ************** ********@b28g2000cwb.googlegroups .com> ;,

< ga ***** @ hotmail.comwrote:


>我需要做几个长整数乘以和十分之一。我可以避免使用花车吗?我可以用某种方式使用两个long作为64位值吗?



你必须综合算术才能做到这一点,但如果你只是为了一对而做,那就不是很难了。


下面,让W为一次乘以的位数,

,让**代表取幂


然后

(Ahigh * 2 ** W + Alow)*(Bhigh * 2 ** W + Blow)是


Ahigh * Bhigh * 2 **(W + W)+ Ahigh * Blow * 2 ** W +

Alow * Bhigh * 2 ** W + Alow * Blow


这是


(Ahigh * Bhigh)* 2 **(2 * W)+

(Ahigh * Blow + Alow * Bhigh)* 2 ** W +

Alow * Blow

现在的诀窍:让Ahigh成为原始无符号长A的高位A

让Alow成为低位 - 例如,


Alow = A& 〜(1L<< W);

Ahigh = A> W;


并且同样来自B的Bhigh和Blow。例如,如果你的

无符号长整数可以容纳32位,你可以让W为16,而Ahigh会将a / b $ b作为A的前16位,而Alow则是16位的底部位A.


如果你再乘以Alow * Blow并将结果放入

无符号长整数,比如说T1,那么结果的前W位是随身携带。

所以你计算Ahigh * Blow + Alow * Bhigh +(T1>> W)并且

给你下一个比特块 - 除了你必须

帐户的沿途。假设结果在T2,那么

为低。无符号长的乘法是

(T2&〜(1L<< W))<< W | ((T1&〜(1L<< W))


等等,只要确保你考虑每个点的运费
只要你操作的每次打开的位数不超过无符号长度的一半,

那么结果将始终适合在一个未签名的长期内,

的最高位是结转到下一阶段。

-

有没有什么东西可以这么说,看,这是新的吗?它已经过了我们面前的旧时代了。 - 传道书


Konstantin Miller写道:

ga*****@hotmail.com 写道:


我需要做几次长整数乘以

除以十。我可以避免使用浮点数吗?我可以用两个长的

作为64位值吗?谢谢。


嗨!


你能说清楚你想做什么吗? :-)


康斯坦丁



我有一些长值的升,我希望转换为加仑(和

返回)。如果我可以做整数乘法除以10我可以

避免浮动库。谢谢。


I need to do just a few multiplies of long integers and have a divide
by ten. Can I avoid using floats? Can I use two longs as a 64 bit value
somehow? Thanks.

解决方案

ga*****@hotmail.com wrote:

I need to do just a few multiplies of long integers and have a divide
by ten. Can I avoid using floats? Can I use two longs as a 64 bit value
somehow? Thanks.

Hi!

Can you specify more clearly what you want to do? :-)

Konstantin


In article <11**********************@b28g2000cwb.googlegroups .com>,
<ga*****@hotmail.comwrote:

>I need to do just a few multiplies of long integers and have a divide
by ten. Can I avoid using floats? Can I use two longs as a 64 bit value
somehow?

You will have to synthesize the arithmetic to do this, but it is not
difficult if you are only doing it for a pair.

Below, let W be the number of bits being multiplied at a time,
and let ** represent exponentiation

Then
(Ahigh * 2**W + Alow) * (Bhigh * 2**W + Blow) is

Ahigh * Bhigh * 2**(W+W) + Ahigh * Blow * 2**W +
Alow * Bhigh * 2**W + Alow * Blow

which is

(Ahigh * Bhigh) * 2**(2*W) +
(Ahigh * Blow + Alow * Bhigh) * 2**W +
Alow * Blow
Now the trick: let Ahigh be the high bits of the original unsigned long A
and let Alow be the low bits -- e.g.,

Alow = A & ~(1L<<W);
Ahigh = A >W;

and similiarily for Bhigh and Blow derived from B. For example, if your
unsigned long can hold 32 bits, you could let W be 16, and Ahigh would
be the top 16 bits of A and Alow would be the bottom 16 bits of A.

If you then multiply Alow * Blow and put the result into an
unsigned long, say T1, then the top W bits of that result are the "carry".
So you calculate Ahigh * Blow + Alow * Bhigh + (T1>>W) and that
gives you the next chunk of bits over -- except you have to
account for carries along the way. Say the result is in T2, then
the "low" unsigned long of the multiplication is
(T2 & ~(1L<<W)) << W | ((T1 & ~(1L<<W))

and so on, just making sure you take into account the carries at
each point. As long as the number of bits at a time that you operate
on does not exceed half the number of bits in an unsigned long,
then the result will always fit within an unsigned long, with the
top bits of that being the carry over to the next stage.
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes


Konstantin Miller wrote:

ga*****@hotmail.com wrote:

I need to do just a few multiplies of long integers and have a
divide by ten. Can I avoid using floats? Can I use two longs
as a 64 bit value somehow? Thanks.


Hi!

Can you specify more clearly what you want to do? :-)

Konstantin

I have some long values in Liters and I want to convert to gallons (and
back). If I could do integer multiplication and divide by 10 I could
avoid the float library. Thanks.


这篇关于避免浮动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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