有一个算法可以同时进行整数乘法和除法吗? [英] Is there an algorithm for integer multiply and divide at the same time?

查看:55
本文介绍了有一个算法可以同时进行整数乘法和除法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




如果我有三个64位整数并且我想对它们执行此操作:


x * y / z


让我们假设我们乘以(y)的东西被我们所取代的
除以(z)以便最终的答案适合一个64位整数。


让我用无符号字符(8位)简化它:


254 * 253/252 = 255


但是,如果我们只有8位整数可以使用,是否有一个算法

只使用8位整数来提出正确的回答255?

显而易见的问题是,乘法产生的数字远大于
8位整数。


我在哪里与此相关的是,有时候我需要乘以并除去

大的64位整数,只要得到的数字适合64位


谢谢,


艾伦
www.sadevelopment.com

解决方案

7月1日,下午10:49,默认用户 < nospam38 ... @ forme.comwrote:





如果我有三个64位整数和我想对它们进行这样的操作:


x * y / z


但是,如果我们只有8位整数可以使用,是有一个算法,

只能使用8位整数来得出255的正确答案?

明显的问题是乘法产生的数字比8位整数大得多。



我认为这应该运作良好


x *(y / z)或试试这个(x / z)* y

$ b $bDarío


默认用户写道:





如果我有三个64位整数并且我想对它们执行此操作:


x * y / z


让我们假设我们乘以(y)的是我们的偏移

除以(z)以便最终答案适合64位整数。


让我用无符号字符(8位)简化它:


254 * 253/252 = 255


但是,如果我们只有8位整数可以使用,是否有一个算法



只会使用8位整数正确答案为255?

显而易见的问题是乘法产生的数字比8位整数大得多。


我要去的地方就是我有时候d乘以并除去

大的64位整数,只要得到的数字适合64位



使用double或long

double进行计算然后返回后(可能更容易)(也可能更高效)他们在你的目标平台上有超过
64位的尾数)。但是,可能会有一些

棘手的分析来证明代码是正确的。

Best


Kai-Uwe Bux


7月2日凌晨4:29,Kai-Uwe Bux< jkherci ... @ gmx.netwrote:


默认用户写道:


如果我有三个64位整数并且我想这样做

操作它们:


x * y / z


让我们假设我们乘以(y)的东西被

抵消我们除以(z)以便最终答案将

符合64位整数。


让我通过使用无符号字符(8位)来简化它:


254 * 253/252 = 255


但是,如果我们只有8位整数可以使用,是否有一个

算法只使用8位整数

255的正确答案?显而易见的问题是

乘法产生的数字远大于8位


我要去的地方有时我需要

乘以并除以大的64位整数,只要

得到的数字适合64位整数,我可以这样做吗

64位整数单独?


使用

double或long double可能更容易(也可能更高效)计算然后再回到案例

(如果你的目标上有超过64位的尾数

平台)。



你知道这样的平台吗?他所关心的中间值最多可以有127位,所以你需要一个尾数

的127位。我觉得有些已经存在(也许还有一些旧的
CDC大型机),但今天我也不知道。


但是,可能需要进行一些棘手的分析来证明代码是正确的。



*如果*的条件完全如他所述,则很容易证明代码不正确(甚至找不到)对于IEEE double,或者任何表示

在尾数中使用少于127位的示例值

将无法通过测试。


-

James Kanze(GABI软件)电子邮件:ja ********* @ gmail.com

Conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9placeSémard,78210 St.-Cyr-l''coco,France,+ 33(0)1 30 23 00 34


Hi,

If I have three 64 bit integers and I want to do this operation on them:

x*y/z

Lets say that what we are multiplying by (y) is offset by what we are
dividing by (z) so that the final answer will fit in a 64-bit integer.

Let me simplify it by using unsigned chars (8 bits):

254*253/252 = 255

But, if we only had 8 bit integers to work with, is there an algorithm that
would use 8 bit integers only to come up with the right answer of 255 ? The
obvious problem is that the multiplication yields a number much larger than
an 8-bit integer.

Where I am going with this is that sometimes I need to multiply and divide
large 64-bit integers and as long as the resulting number fits in a 64-bit
integer, can I do this with 64-bit integers alone?

Thanks,

Alan
www.sadevelopment.com

解决方案

On Jul 1, 10:49 pm, "Default User" <nospam38...@forme.comwrote:

Hi,

If I have three 64 bit integers and I want to do this operation on them:

x*y/z

But, if we only had 8 bit integers to work with, is there an algorithm that
would use 8 bit integers only to come up with the right answer of 255 ? The
obvious problem is that the multiplication yields a number much larger than
an 8-bit integer.

I think this should work well

x*(y/z) or try this (x/z)*y

Darío


Default User wrote:

Hi,

If I have three 64 bit integers and I want to do this operation on them:

x*y/z

Lets say that what we are multiplying by (y) is offset by what we are
dividing by (z) so that the final answer will fit in a 64-bit integer.

Let me simplify it by using unsigned chars (8 bits):

254*253/252 = 255

But, if we only had 8 bit integers to work with, is there an algorithm
that
would use 8 bit integers only to come up with the right answer of 255 ?
The obvious problem is that the multiplication yields a number much larger
than an 8-bit integer.

Where I am going with this is that sometimes I need to multiply and divide
large 64-bit integers and as long as the resulting number fits in a 64-bit
integer, can I do this with 64-bit integers alone?

It might be easier (and maybe also more efficient) to use double or long
double for the computation and then case back (provided they have more than
64 bit mantissa on your target platform). However, there could be some
tricky analysis involved to prove the code correct.
Best

Kai-Uwe Bux


On Jul 2, 4:29 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:

Default User wrote:

If I have three 64 bit integers and I want to do this
operation on them:

x*y/z

Lets say that what we are multiplying by (y) is offset by
what we are dividing by (z) so that the final answer will
fit in a 64-bit integer.

Let me simplify it by using unsigned chars (8 bits):

254*253/252 = 255

But, if we only had 8 bit integers to work with, is there an
algorithm that would use 8 bit integers only to come up with
the right answer of 255 ? The obvious problem is that the
multiplication yields a number much larger than an 8-bit
integer.

Where I am going with this is that sometimes I need to
multiply and divide large 64-bit integers and as long as the
resulting number fits in a 64-bit integer, can I do this
with 64-bit integers alone?

It might be easier (and maybe also more efficient) to use
double or long double for the computation and then case back
(provided they have more than 64 bit mantissa on your target
platform).

Do you know of such a platform? The intermediate value he''s
concerned with can have up to 127 bits, so you''d need a mantissa
of 127 bits. I think some have existed (perhaps some of the old
CDC mainframes), but I don''t know of any today.

However, there could be some tricky analysis involved to prove
the code correct.

*If* the conditions are exactly as he stated, then it''s easy to
prove the code incorrect (and even find example values which
would fail a test) for IEEE double, or any representation which
uses less than 127 bits in the mantissa.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


这篇关于有一个算法可以同时进行整数乘法和除法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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