Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: Subtracting...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 9 of 12 Topic 26187 of 26972
Post > Topic >>

Re: Subtracting unsigned entities

by Eric Sosman <Eric.Sosman@[EMAIL PROTECTED] > May 13, 2008 at 11:03 AM

fred.l.kleinschmidt@[EMAIL PROTECTED]
 wrote:
> On May 12, 3:58 pm, Bart <b...@[EMAIL PROTECTED]
> wrote:
>> On May 12, 11:18 pm, fred.l.kleinschm...@[EMAIL PROTECTED]
 wrote:
>>
>>> If one knows that x and y are unsigned integers (of unknown size -
>>> they may be short, int, long, long long), what is the most ****table
>>> way to determine their difference?
>>> If x is smaller than y, then x-y is negative.
>>> A naive approach is
>>>    if ( x > y ) {
>>>       ...
>>>    }
>>> but a compiler may code (x>y) as  (x-y > 0).
>> What do you mean by difference?
>>
>> If you mean x-y, then this will give strange results when x<y (it
>> can't be negative).
>>
>> If you mean max(x,y)-min(x,y) then your 'naive' approach will likely
>> work well.
>>
> 
> The problem came up on a Unix platform, where some code was trying
> to check for a double-click. Each time button1 is clicked, it checks
> the time with the previous time it was clicked; if the difference is
> small enough, a double-click is assumed.
> 
> The times are stored in a variable of type Time, defined as
> an unsigned int on some platforms, unsigned long on others, storing
> clock time in milliseconds.
> 
> For an unsigned int, the value will wrap every 47 days. The original
> code was
> 
>    if ( (newtime - oldtime) < doubleclicktime ) {
>        /* perform double-click action */
>    }
> 
> That code is unsafe: as oldtime approaches UINT_MAX, the risk
> is that newtime might occur after time wraps, thus might be
> a small integer, and (newtime - oldtime) is mathematically
> negative.

     Doesn't matter.  You've said the times are either
`unsigned int' or `unsigned long' depending on the platform,
and neither of these is subject to promotion.  Hence, the
arithmetic will be carried out according to the unsigned
rules, and `tiny - huge' will yield `moderate', as desired.

> Since others have said that the compiler is obliged to
> perform the if-test properly, my solution is
> 
>    if ( t2 > t1 ) {
>         delta = t2 - t1;
>     }
>     else {
>         delta = (UINT_MAX - t1) + t2 + 1;
>     }

     The `else' clause is just an obfuscated version of
the `then' clause, producing the same result.

-- 
Eric.Sosman@[EMAIL PROTECTED]

 




 12 Posts in Topic:
Subtracting unsigned entities
fred.l.kleinschmidt@[EMAI  2008-05-12 15:18:14 
Re: Subtracting unsigned entities
Richard Heathfield <rj  2008-05-12 22:24:45 
Re: Subtracting unsigned entities
Ben Pfaff <blp@[EMAIL   2008-05-12 15:26:55 
Re: Subtracting unsigned entities
=?ISO-8859-1?Q?Tom=E1s_=D  2008-05-12 15:44:11 
Re: Subtracting unsigned entities
Bart <bc@[EMAIL PROTEC  2008-05-12 15:58:10 
Re: Subtracting unsigned entities
Peter Nilsson <airia@[  2008-05-12 20:00:00 
Re: Subtracting unsigned entities
Chris Dollin <chris.do  2008-05-13 10:15:45 
Re: Subtracting unsigned entities
fred.l.kleinschmidt@[EMAI  2008-05-13 07:50:27 
Re: Subtracting unsigned entities
Eric Sosman <Eric.Sosm  2008-05-13 11:03:17 
Re: Subtracting unsigned entities
Chris Dollin <chris.do  2008-05-13 16:11:24 
Re: Subtracting unsigned entities
=?iso-8859-1?Q?=D8yvind_R  2008-05-13 22:55:41 
Re: Subtracting unsigned entities
Harald van =?UTF-8?b?RMSz  2008-05-13 23:16:38 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 16:05:42 CDT 2008.