确定哪个总和更大 [英] Determine which sum is larger

查看:49
本文介绍了确定哪个总和更大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我有四个无符号长多头a,b,c,d。我想确定

a + b或c + d是否更大,好像没有溢出。也就是说,ULLONG_MAX

+ 5应该被认为大于10 + 20.


到目前为止我想到的最好的方法是检查溢出

两笔金额;如果只有一个溢出,那个更大。否则,

通常比较总和。有更好的方法吗?


感谢您的任何想法!

-Peter

解决方案

彼得阿蒙说:

你好,


彼得您好,ltns


>

我有四个无符号长long a,b,c,d。我想确定

a + b或c + d是否更大,好像没有溢出。



当然没有溢出,因为无符号类型不会溢出。


您的问题涉及unsigned long long int,但实际上我们可以忽略

,并关注它们是无符号类型的事实,而不是我们的b $ b不能保证有更宽的类型可用。


也就是说,ULLONG_MAX

+ 5应该被认为大于10 + 20.


到目前为止,我想到的最好的方法是检查

两个总和的溢出;如果只有一个溢出,那个更大。否则,

通常比较总和。有没有更好的办法?



除了关于溢出这个词的挑剔之外,我认为现有解决方案没有问题。

。任何其他方式都可能更复杂。


我确实玩了一些减少值 - 例如,如果(ac){

a - = c; c = 0; } else {c - = a; a = 0;和b,d一样 - 但是我没有看到它带来任何有用的东西。如果a和c已知共享一个

高值位(也就是说,两者都至少是该类型的最大可能值的一半

),并且b和d同样地,如果你真的因为某种原因需要避免价值包装,可以想象它是有用的。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日


11月19日上午12:18,Peter Ammon< gersh ... @ splintermac.comwrote:


你好,


我有四个无符号长多头a,b,c,d。我想确定

a + b或c + d是否更大,好像没有溢出。也就是说,ULLONG_MAX

+ 5应该被认为大于10 + 20.


到目前为止我想到的最好的方法是检查溢出

两笔金额;如果只有一个溢出,那个更大。否则,

通常比较总和。有更好的方法吗?


感谢您的任何想法!

-Peter



您好彼得!


如果检查一半,我们不会溢出。由于我们丢失数据,我们通过检查我们剪掉的余数来澄清不清楚的结果。


int compareULLSums(unsigned long long a,unsigned long long b,

unsigned long long c,unsigned long long d)

{

unsigned long long s1 =(a / 2 + b / 2), s2 =(c / 2 + d / 2);

if(s1 s2 ||(s1 == s2&&((a%2 + b%2)(c%2 + d%2))))

返回1;

返回0;

}


希望这会有所帮助!

- Sam


Richard Heathfield写道:


Peter Ammon说:


>我有四个无符号长long a,b,c,d。我想确定是否a + b或c + d更大,好像没有溢出。



你的问题涉及unsigned long long int,但事实上我们可以忽略

,并关注它们是无符号类型的事实比我们b $ b不能保证有更广泛的类型。


>也就是说,应该考虑ULLONG_MAX
+ 5大于10 + 20.

到目前为止我想到的最好的方法是检查两个总和的溢出;如果只有一个溢出,那个更大。否则,
通常比较总和。有没有更好的办法?



除了关于溢出这个词的挑剔之外,我认为现有解决方案没有问题。

。任何其他方式都可能更复杂。


我确实玩了一些减少值 - 例如,如果(ac){

a - = c; c = 0; } else {c - = a; a = 0;和b,d一样 - 但是我没有看到它带来任何有用的东西。



在此过程之后,您有四种可能性:

a == b == 0,a == d == 0,c = = b == 0,并且c == d == 0.

如果a == b == 0,那么找到较小的金额是微不足道的。

否则,如果a == d == 0,则bc给出你想要的比较。

其他情况也是如此。


我同意这个可能比OP的原始

想法更复杂。


-

Philip Potter pgp< atdoc .ic.ac.uk


Howdy,

I have four unsigned long longs a, b, c, d. I want to determine whether
a+b or c+d is larger, as if there were no overflow. That is, ULLONG_MAX
+ 5 should be considered larger than 10 + 20.

The best approach I have thought of so far is to check for overflow in
both sums; if exactly one overflows, that one is larger. Otherwise,
compare the sums normally. Is there a better way?

Thanks for any ideas!
-Peter

解决方案

Peter Ammon said:

Howdy,

Hi Peter, ltns.

>
I have four unsigned long longs a, b, c, d. I want to determine whether
a+b or c+d is larger, as if there were no overflow.

There isn''t any overflow, of course, because unsigned types can''t overflow.

Your question involves unsigned long long ints, but in fact we can ignore
that, and focus on the fact that they are unsigned types than which we
cannot guarantee having a wider type available.

That is, ULLONG_MAX
+ 5 should be considered larger than 10 + 20.

The best approach I have thought of so far is to check for overflow in
both sums; if exactly one overflows, that one is larger. Otherwise,
compare the sums normally. Is there a better way?

Apart from the nitpick about the word "overflow", I can see no problem with
your existing solution. Any other way is likely to be more complicated.

I did play around with reducing the values a bit - for example, if(a c) {
a -= c; c = 0; } else { c -= a; a = 0; } and the same for b, d - but I
couldn''t see it leading anywhere useful. If a and c were known to share a
high value bit (that is, both were at least half the maximum possible
value for the type), and b and d likewise, it could conceivably have been
useful if you really needed to avoid value wrapping for some reason.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


On Nov 19, 12:18 am, Peter Ammon <gersh...@splintermac.comwrote:

Howdy,

I have four unsigned long longs a, b, c, d. I want to determine whether
a+b or c+d is larger, as if there were no overflow. That is, ULLONG_MAX
+ 5 should be considered larger than 10 + 20.

The best approach I have thought of so far is to check for overflow in
both sums; if exactly one overflows, that one is larger. Otherwise,
compare the sums normally. Is there a better way?

Thanks for any ideas!
-Peter

Hi Peter!

We won''t overflow if we check half the sum. Since we lose data, we
clarify unclear results by checking the remainders we snipped out.

int compareULLSums( unsigned long long a, unsigned long long b,
unsigned long long c, unsigned long long d )
{
unsigned long long s1 = (a/2+b/2), s2 = (c/2+d/2);
if( s1 s2 || ( s1 == s2 && ( (a%2+b%2) (c%2+d%2) ) ) )
return 1;
return 0;
}

Hope this helps!
- Sam


Richard Heathfield wrote:

Peter Ammon said:

>I have four unsigned long longs a, b, c, d. I want to determine whether
a+b or c+d is larger, as if there were no overflow.


Your question involves unsigned long long ints, but in fact we can ignore
that, and focus on the fact that they are unsigned types than which we
cannot guarantee having a wider type available.

>That is, ULLONG_MAX
+ 5 should be considered larger than 10 + 20.

The best approach I have thought of so far is to check for overflow in
both sums; if exactly one overflows, that one is larger. Otherwise,
compare the sums normally. Is there a better way?


Apart from the nitpick about the word "overflow", I can see no problem with
your existing solution. Any other way is likely to be more complicated.

I did play around with reducing the values a bit - for example, if(a c) {
a -= c; c = 0; } else { c -= a; a = 0; } and the same for b, d - but I
couldn''t see it leading anywhere useful.

After that process, you have four possibilities:
a == b == 0, a == d == 0, c == b == 0, and c == d == 0.

If a == b == 0, then finding the smaller sum is trivial.
Otherwise if a == d == 0, then b c gives the comparison you want.
Other cases are handled similarly.

I agree that this is probably more complicated than the OP''s original
idea though.

--
Philip Potter pgp <atdoc.ic.ac.uk


这篇关于确定哪个总和更大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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