Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #168581 > unrolled thread

Increment/decrement a variable with a maximum

Started bypozz <pozzugno@gmail.com>
First post2022-12-18 23:59 +0100
Last post2022-12-23 13:53 -0800
Articles 12 — 10 participants

Back to article view | Back to comp.lang.c


Contents

  Increment/decrement a variable with a maximum pozz <pozzugno@gmail.com> - 2022-12-18 23:59 +0100
    Re: Increment/decrement a variable with a maximum Siri Cruise <chine.bleu@yahoo.com> - 2022-12-18 15:07 -0800
    Re: Increment/decrement a variable with a maximum Richard Damon <Richard@Damon-Family.org> - 2022-12-18 19:25 -0500
    Re: Increment/decrement a variable with a maximum Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-12-18 16:41 -0800
    Re: Increment/decrement a variable with a maximum antispam@math.uni.wroc.pl - 2022-12-19 14:39 +0000
    Re: Increment/decrement a variable with a maximum Kaz Kylheku <864-117-4973@kylheku.com> - 2022-12-20 13:44 +0000
      Re: Increment/decrement a variable with a maximum Kaz Kylheku <864-117-4973@kylheku.com> - 2022-12-20 13:54 +0000
      Re: Increment/decrement a variable with a maximum Jame O' Brian <invalid@gmail.com> - 2022-12-20 18:12 +0000
        Re: Increment/decrement a variable with a maximum Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2022-12-20 18:21 +0000
        Re: Increment/decrement a variable with a maximum "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-12-20 12:52 -0800
          Re: Increment/decrement a variable with a maximum Öö Tiib <ootiib@hot.ee> - 2022-12-23 05:21 -0800
            Re: Increment/decrement a variable with a maximum "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-12-23 13:53 -0800

#168581 — Increment/decrement a variable with a maximum

Frompozz <pozzugno@gmail.com>
Date2022-12-18 23:59 +0100
SubjectIncrement/decrement a variable with a maximum
Message-ID<tno602$2cj2$1@dont-email.me>
Suppose you have two uint32_t variables (a and b) that you want to add 
together and another uint32_t variable (max) that is the maximum that 
the result could assume. A silly code could be:

   a += b;
   if (a > max) {
     a = max;
   }

Of course, this doesn't work well for every values of a, b and max.

For example, for a=0xFFFF'FFFC, b=10 and max=0xFFFF'FFFE.


Could you suggest a better method? I'm thinking of:

     a += b;
     if ((a < b) || (a > max)) {
         a = max;
     }

And for decrement?

[toc] | [next] | [standalone]


#168582

FromSiri Cruise <chine.bleu@yahoo.com>
Date2022-12-18 15:07 -0800
Message-ID<chine.bleu-00A958.15071418122022@news.eternal-september.org>
In reply to#168581
In article <tno602$2cj2$1@dont-email.me>,
 pozz <pozzugno@gmail.com> wrote:

>    a += b;
>    if (a > max) {
>      a = max;
>    }
> 
> Of course, this doesn't work well for every values of a, b and max.

If you have to worry about maximal and minimal representable 
values, you have to use more complicated code than can end up 
machine dependent. If you can assumed this safely away from 
those, just treat it like normal integer arithmetic.

-- 
:-<> Siri Seal of Disavowal #000-001. Disavowed. Denied. Deleted.    @
'I desire mercy, not sacrifice.'                                    /|\
Discordia: not just a religion but also a parody. This post         / \
I am an Andrea Chen sockpuppet.                  insults Islam.  Mohammed

[toc] | [prev] | [next] | [standalone]


#168584

FromRichard Damon <Richard@Damon-Family.org>
Date2022-12-18 19:25 -0500
Message-ID<1cOnL.14222$0dpc.2893@fx33.iad>
In reply to#168581
On 12/18/22 5:59 PM, pozz wrote:
> Suppose you have two uint32_t variables (a and b) that you want to add 
> together and another uint32_t variable (max) that is the maximum that 
> the result could assume. A silly code could be:
> 
>    a += b;
>    if (a > max) {
>      a = max;
>    }
> 
> Of course, this doesn't work well for every values of a, b and max.
> 
> For example, for a=0xFFFF'FFFC, b=10 and max=0xFFFF'FFFE.
> 
> 
> Could you suggest a better method? I'm thinking of:
> 
>      a += b;
>      if ((a < b) || (a > max)) {
>          a = max;
>      }
> 
> And for decrement?
> 

Decrement is simple since b is unsigned:

   if(a < b) a = 0;
   else a -= b;


The increment case can be simplified a bit if we can assume "reasonable" 
values for b, such that b <= max to:

   if(a > max-b) a = max;
   else a += b;

[toc] | [prev] | [next] | [standalone]


#168585

FromTim Rentsch <tr.17687@z991.linuxsc.com>
Date2022-12-18 16:41 -0800
Message-ID<86a63k2pjp.fsf@linuxsc.com>
In reply to#168581
pozz <pozzugno@gmail.com> writes:

> Suppose you have two uint32_t variables (a and b) that you want to add
> together and another uint32_t variable (max) that is the maximum that
> the result could assume.  A silly code could be:
>
>   a += b;
>   if (a > max) {
>     a = max;
>   }
>
> Of course, this doesn't work well for every values of a, b and max.
>
> For example, for a=0xFFFF'FFFC, b=10 and max=0xFFFF'FFFE.
>
>
> Could you suggest a better method?  [...]

    a =   max > a  &&  max - a > b   ? a + b   : max;

> And for decrement?

After seeing the expression for increment, exercise for the
reader.

[toc] | [prev] | [next] | [standalone]


#168592

Fromantispam@math.uni.wroc.pl
Date2022-12-19 14:39 +0000
Message-ID<tnpt3k$4ul$1@gioia.aioe.org>
In reply to#168581
pozz <pozzugno@gmail.com> wrote:
> Suppose you have two uint32_t variables (a and b) that you want to add 
> together and another uint32_t variable (max) that is the maximum that 
> the result could assume. A silly code could be:
> 
>   a += b;
>   if (a > max) {
>     a = max;
>   }
> 
> Of course, this doesn't work well for every values of a, b and max.
> 
> For example, for a=0xFFFF'FFFC, b=10 and max=0xFFFF'FFFE.


   {
        int64_t tmp = (int64_t)a + (int64_t)b;
        a = (tmp > max)?max:tmp;
   }

Tell compiler what you want and let it do its job at optimization.


> Could you suggest a better method? I'm thinking of:
> 
>     a += b;
>     if ((a < b) || (a > max)) {
>         a = max;
>     }
> 
> And for decrement?

The above generalize in obvious way.

-- 
                              Waldek Hebisch

[toc] | [prev] | [next] | [standalone]


#168606

FromKaz Kylheku <864-117-4973@kylheku.com>
Date2022-12-20 13:44 +0000
Message-ID<20221220053840.192@kylheku.com>
In reply to#168581
On 2022-12-18, pozz <pozzugno@gmail.com> wrote:
> Suppose you have two uint32_t variables (a and b) that you want to add 
> together and another uint32_t variable (max) that is the maximum that 
> the result could assume. A silly code could be:
>
>    a += b;
>    if (a > max) {
>      a = max;
>    }
>
> Of course, this doesn't work well for every values of a, b and max.

if (a + b > a || a + b > max)
  a = max;
else
  a = a + b; // written this way to make common subexpression obvious

-- 
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

[toc] | [prev] | [next] | [standalone]


#168608

FromKaz Kylheku <864-117-4973@kylheku.com>
Date2022-12-20 13:54 +0000
Message-ID<tnseqi$mr7g$3@dont-email.me>
In reply to#168606
On 2022-12-20, Kaz Kylheku <864-117-4973@kylheku.com> wrote:
> if (a + b > a || a + b > max)
>   a = max;
> else
>   a = a + b; // written this way to make common subexpression obvious

Oops, what? That should be:

  if (a + b < a || a + b > max)

If a + b wraps or else exceeds max, then clamp to max; otherwise update
a with a + b.

-- 
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

[toc] | [prev] | [next] | [standalone]


#168615

FromJame O' Brian <invalid@gmail.com>
Date2022-12-20 18:12 +0000
Message-ID<tnsu2g$85f$1@gioia.aioe.org>
In reply to#168606
On 20/12/2022 13:44, Kaz Kylheku wrote:
> if (a + b > a


Under what circumstances will ( a + b ) not greater than a? Example please.

Thank you.


[toc] | [prev] | [next] | [standalone]


#168616

FromLew Pitcher <lew.pitcher@digitalfreehold.ca>
Date2022-12-20 18:21 +0000
Message-ID<tnsuer$mh7i$4@dont-email.me>
In reply to#168615
On Tue, 20 Dec 2022 18:12:44 +0000, Jame O' Brian wrote:

> On 20/12/2022 13:44, Kaz Kylheku wrote:
>> if (a + b > a
> 
> 
> Under what circumstances will ( a + b ) not greater than a? Example
> please.

 a == UINT_MAX
 b == 1




-- 
Lew Pitcher
"In Skills, We Trust"

[toc] | [prev] | [next] | [standalone]


#168621

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2022-12-20 12:52 -0800
Message-ID<tnt7ak$pcng$5@dont-email.me>
In reply to#168615
On 12/20/2022 10:12 AM, Jame O' Brian wrote:
> On 20/12/2022 13:44, Kaz Kylheku wrote:
>> if (a + b > a
> 
> 
> Under what circumstances will ( a + b ) not greater than a? Example please.

Overflow.

[toc] | [prev] | [next] | [standalone]


#168630

FromÖö Tiib <ootiib@hot.ee>
Date2022-12-23 05:21 -0800
Message-ID<644ff5b9-a628-466c-bfc6-7b4d73d9a7f3n@googlegroups.com>
In reply to#168621
On Tuesday, 20 December 2022 at 22:52:51 UTC+2, Chris M. Thomasson wrote:
> On 12/20/2022 10:12 AM, Jame O' Brian wrote: 
> > On 20/12/2022 13:44, Kaz Kylheku wrote:
> >> if (a + b > a 
> > 
> >
> > Under what circumstances will ( a + b ) not greater than a? Example please.
> Overflow.

Or when b is 0. 

[toc] | [prev] | [next] | [standalone]


#168632

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2022-12-23 13:53 -0800
Message-ID<to5818$1rch0$2@dont-email.me>
In reply to#168630
On 12/23/2022 5:21 AM, Öö Tiib wrote:
> On Tuesday, 20 December 2022 at 22:52:51 UTC+2, Chris M. Thomasson wrote:
>> On 12/20/2022 10:12 AM, Jame O' Brian wrote:
>>> On 20/12/2022 13:44, Kaz Kylheku wrote:
>>>> if (a + b > a
>>>
>>>
>>> Under what circumstances will ( a + b ) not greater than a? Example please.
>> Overflow.
> 
> Or when b is 0.

Touche! I totally missed that case. Thanks.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web