Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

System going down at 5 this afternoon to install scheduler bug.


devel / comp.lang.c / Re: clamp operator

SubjectAuthor
* clamp operatorfir
+* Re: clamp operatorfir
|+* Re: clamp operatorfir
||`- Re: clamp operatorfir
|+* Re: clamp operatorBart
||+- Re: clamp operatorfir
||`* Re: clamp operatorfir
|| `* Re: clamp operatorBart
||  `* Re: clamp operatorfir
||   `* Re: clamp operatorBart
||    +- Re: clamp operatorfir
||    `* Re: clamp operatorfir
||     `* Re: clamp operatorBart
||      `* Re: clamp operatorfir
||       `* Re: clamp operatorfir
||        `* Re: clamp operatorfir
||         `- Re: clamp operatorfir
|`* Re: clamp operatorRichard Damon
| `* Re: clamp operatorfir
|  `* Re: clamp operatorfir
|   `- Re: clamp operatorfir
+- Re: clamp operatorChris M. Thomasson
`* Re: clamp operatorBonita Montero
 +* Re: clamp operatorfir
 |`* Re: clamp operatorBonita Montero
 | +* Re: clamp operatorfir
 | |+* Re: clamp operatorBart
 | ||+* Re: clamp operatorfir
 | |||`- Re: clamp operatorfir
 | ||`* Re: clamp operatorDavid Brown
 | || +- Re: clamp operatorfir
 | || +* [OT] Re: clamp operatorBen Bacarisse
 | || |`- Re: [OT] Re: clamp operatorfir
 | || `- Re: clamp operatorfir
 | |+* Re: clamp operatorBonita Montero
 | ||`- Re: clamp operatorKenny McCormack
 | |`* Re: clamp operatorRobert Latest
 | | +* Re: clamp operatorLew Pitcher
 | | |`- Re: clamp operatorDavid Brown
 | | `* Re: clamp operatorfir
 | |  `- Re: clamp operatorfir
 | +* Re: clamp operatorDavid Brown
 | |`- Re: clamp operatorChris M. Thomasson
 | `- Re: clamp operatorBGB
 `- Re: clamp operatorKenny McCormack

Pages:12
Re: clamp operator

<u7s474$1o80g$1@news.xmission.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=26039&group=comp.lang.c++#26039

  copy link   Newsgroups: comp.lang.c
 by: Kenny McCormack - Sun, 2 Jul 2023 15:13 UTC

In article <u7s20s$3bdlh$1@dont-email.me>,
Bonita Montero <Bonita.Montero@gmail.com> wrote:
....
>The language is already perfect in that respect.

You are sounding more and more like the Orange Menace, everyday.

--
They say that Trump is a stupid man's idea of a clever man, a poor man's
idea of a rich man, and a weak man's idea of a strong man.

Well, Melania is an unclassy man's idea of classy.

Re: clamp operator

<u7s58v$3bnoa$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=26040&group=comp.lang.c++#26040

  copy link   Newsgroups: comp.lang.c
 by: Bart - Sun, 2 Jul 2023 15:31 UTC

On 02/07/2023 15:57, fir wrote:
>
>
> noy much good
> the question is hovever what operator to use
>
> maybe x]b for min and a[x for max , a[x]b for clamp..thsi is
possibly the shortest

The shortest isn't much good if nobody understands what it means, or
can't remember.

I use 'min' and 'max' since everybody will know what they mean,
especially me!

And while I can write them as `a max b`, I nearly always use `max(a, b)`
instead, as it is clearer.

Now we come to your [, ] operators. One of them means min, the other
max, but which is which, and WHY?

If a syntax also uses [] for indexing, those mismatched braces also
introduce ambiguity:

A[i] ...

If this indexing with `i`, or is that `]` part of a min or max operation
(whichever one it is)?

For that matter, is that /first/ `[` part of a max or min operation? Are
/both/ operators min/max ones?

Have a look at what symbols other languages, or mathematics, use for
minimum and maximum.

I'm not aware of any except for APL that uses ⌊ and ⌈, funnily enough
not much different from [ and ], but now you can make a good stab at
which is which.

Re: clamp operator

<u7s5st$3bpd9$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=26041&group=comp.lang.c++#26041

  copy link   Newsgroups: comp.lang.c
 by: Bonita Montero - Sun, 2 Jul 2023 15:42 UTC

Am 02.07.2023 um 16:57 schrieb fir:
> niedziela, 2 lipca 2023 o 16:48:40 UTC+2 Bonita Montero napisał(a):
>> Am 02.07.2023 um 16:41 schrieb fir:
>>> niedziela, 2 lipca 2023 o 16:36:29 UTC+2 Bonita Montero napisał(a):
>>>> Am 01.07.2023 um 16:20 schrieb fir:
>>>>> i think there is probably a need of clamp operator
>>>>> in c
>>>>> the question is how it could look like
>>>>> it could be something like
>>>>>
>>>>> x = <0|x|650> but with some better operator
>>>>> one alos may think of some keyword like CL
>>>>> (clamp left) and CR (clamp right
>>>>>
>>>>> x = x CL 0;
>>>>> x = x CR 650;
>>>>>
>>>>> but how it should look better in details i dont know
>>>>>
>>>> This type of clamping is used too seldom to require a special
>>>> operator for it. Imagine you need this once for a thousand
>>>> lines of code. A special operator would be rather confusing.
>>>> The language is already perfect in that respect.
>>>
>>> not really.. clamping, min /max ids used relatively often,
>>> for sure more often this say xor..probably abut the same as or and or shifts..once per a thusand is not so rare btw as it would be once a 3 days of work (when i code its about 10k lines a month) (but then i must rest its not to hold whole year 10k a month imo)
>>>
>>> also abs/fabs and sign should be built in imo,s ame as error operator
>>>
>>> foo() --> printf("error!");
>>>
>>> -> is error operator here (my invention)
>> C++ has things like min(), max() and minmax() gerically, but
>> nothing like clamping; so it looks like that clamping isn't
>> needed that often. And there's the ternary operator which you
>> can nest like that:
>> clamped = value >= min ? value <= max ? value : max : min
>> That has a good readabity so you won't need clamping.
>
> noy much good
> the question is hovever what operator to use

Theres no need for such an operator since it would
be used to seldom compared to all other operators.

>
> maybe x]b for min and a[x for max , a[x]b for clamp..thsi is possibly the shortest

Re: clamp operator

<kgdktsFouhuU1@mid.individual.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=26042&group=comp.lang.c++#26042

  copy link   Newsgroups: comp.lang.c
 by: Robert Latest - Sun, 2 Jul 2023 15:54 UTC

fir wrote:
>> clamped = value >= min ? value <= max ? value : max : min
>> That has a good readabity so you won't need clamping.
>
> noy much good
> the question is hovever what operator to use

No. The question is what's wrong with putting the above into an (inline)
function or macro.

Re: clamp operator

<u7s7dj$3bf2g$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=26043&group=comp.lang.c++#26043

  copy link   Newsgroups: comp.lang.c
 by: Lew Pitcher - Sun, 2 Jul 2023 16:08 UTC

On Sun, 02 Jul 2023 15:54:36 +0000, Robert Latest wrote:

> fir wrote:
>>> clamped = value >= min ? value <= max ? value : max : min
>>> That has a good readabity so you won't need clamping.
>>
>> noy much good
>> the question is hovever what operator to use
>
> No. The question is what's wrong with putting the above into an (inline)
> function or macro.

A macro would evaluate "value" twice, leading to unintentional side-effects
with some values of "value"

Consider
#define CLAMP(min,value,max) (value >= min ? value <= max ? value : max : min)

int valu = 10,
c1;
c1 = CLAMP(5,valu++,15);

which would expand the macro into
c1 = (valu++ >= min ? valu++ <= max ? valu++ : max : min)

--
Lew Pitcher
"In Skills We Trust"

Re: clamp operator

<a62d16fc-79eb-4773-9b7d-93a8fa780639n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=26044&group=comp.lang.c++#26044

  copy link   Newsgroups: comp.lang.c
 by: fir - Sun, 2 Jul 2023 16:30 UTC

niedziela, 2 lipca 2023 o 17:31:58 UTC+2 Bart napisał(a):
> On 02/07/2023 15:57, fir wrote:
> >
> >
> > noy much good
> > the question is hovever what operator to use
> >
> > maybe x]b for min and a[x for max , a[x]b for clamp..thsi is
> possibly the shortest
> The shortest isn't much good if nobody understands what it means, or
> can't remember.
>
> I use 'min' and 'max' since everybody will know what they mean,
> especially me!
>
> And while I can write them as `a max b`, I nearly always use `max(a, b)`
> instead, as it is clearer.
>
> Now we come to your [, ] operators. One of them means min, the other
> max, but which is which, and WHY?
>
> If a syntax also uses [] for indexing, those mismatched braces also
> introduce ambiguity:
>
> A[i] ...
>
> If this indexing with `i`, or is that `]` part of a min or max operation
> (whichever one it is)?
>
> For that matter, is that /first/ `[` part of a max or min operation? Are
> /both/ operators min/max ones?
>
> Have a look at what symbols other languages, or mathematics, use for
> minimum and maximum.
>
> I'm not aware of any except for APL that uses ⌊ and ⌈, funnily enough
> not much different from [ and ], but now you can make a good stab at
> which is which.

max is related to clamp left and min to clamp right,
as symbol [ is related to clamp left so it is relted to max (simply mins max as in your mentioned "a max b" (i also like that kind of operator form and unliek what you say i like it more than function form -
at least in such case)

this [] is not fully colliding with array usage imo but i know its unfortunate ind thiught it could be as signs liek [ ] but with this
low lines erased so in fact i though on ⌈ and its vertical mirroring,
but those ⌊ and ⌈also not so bad as it suggest clearly what means max (max is this upper marked ⌈, min down marked)
x = 0 ⌈ x ⌊ 100
though it seems thet the ⌊ should be better vertically mirrored to lok better..is something like mirrored ⌊ in unicode?

Re: clamp operator

<81e96e56-dbc3-43b0-9e65-cac0859c0dafn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=26045&group=comp.lang.c++#26045

  copy link   Newsgroups: comp.lang.c
 by: fir - Sun, 2 Jul 2023 16:31 UTC

niedziela, 2 lipca 2023 o 17:54:50 UTC+2 Robert Latest napisał(a):
> fir wrote:
> >> clamped = value >= min ? value <= max ? value : max : min
> >> That has a good readabity so you won't need clamping.
> >
> > noy much good
> > the question is hovever what operator to use
> No. The question is what's wrong with putting the above into an (inline)
> function or macro.

side efects are wrong

Re: clamp operator

<eeb4b375-75b6-464c-b0c2-eef94024aa01n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=26046&group=comp.lang.c++#26046

  copy link   Newsgroups: comp.lang.c
 by: fir - Sun, 2 Jul 2023 16:47 UTC

niedziela, 2 lipca 2023 o 18:30:32 UTC+2 fir napisał(a):
> niedziela, 2 lipca 2023 o 17:31:58 UTC+2 Bart napisał(a):
> > On 02/07/2023 15:57, fir wrote:
> > >
> > >
> > > noy much good
> > > the question is hovever what operator to use
> > >
> > > maybe x]b for min and a[x for max , a[x]b for clamp..thsi is
> > possibly the shortest
> > The shortest isn't much good if nobody understands what it means, or
> > can't remember.
> >
> > I use 'min' and 'max' since everybody will know what they mean,
> > especially me!
> >
> > And while I can write them as `a max b`, I nearly always use `max(a, b)`
> > instead, as it is clearer.
> >
> > Now we come to your [, ] operators. One of them means min, the other
> > max, but which is which, and WHY?
> >
> > If a syntax also uses [] for indexing, those mismatched braces also
> > introduce ambiguity:
> >
> > A[i] ...
> >
> > If this indexing with `i`, or is that `]` part of a min or max operation
> > (whichever one it is)?
> >
> > For that matter, is that /first/ `[` part of a max or min operation? Are
> > /both/ operators min/max ones?
> >
> > Have a look at what symbols other languages, or mathematics, use for
> > minimum and maximum.
> >
> > I'm not aware of any except for APL that uses ⌊ and ⌈, funnily enough
> > not much different from [ and ], but now you can make a good stab at
> > which is which.
> max is related to clamp left and min to clamp right,
> as symbol [ is related to clamp left so it is relted to max (simply mins max as in your mentioned "a max b" (i also like that kind of operator form and unliek what you say i like it more than function form -
> at least in such case)
>
> this [] is not fully colliding with array usage imo but i know its unfortunate ind thiught it could be as signs liek [ ] but with this
> low lines erased so in fact i though on ⌈ and its vertical mirroring,
> but those ⌊ and ⌈also not so bad as it suggest clearly what means max (max is this upper marked ⌈, min down marked)
> x = 0 ⌈ x ⌊ 100
> though it seems thet the ⌊ should be better vertically mirrored to lok better..is something like mirrored ⌊ in unicode?

besides as a ⌈ b is symmetruc then some | with dot or circle on top for max and some | with circle on bottom would be probably better

Re: clamp operator

<b9591821-6efb-4960-9b35-ef67f3e5bc73n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=26047&group=comp.lang.c++#26047

  copy link   Newsgroups: comp.lang.c
 by: fir - Sun, 2 Jul 2023 16:59 UTC

niedziela, 2 lipca 2023 o 18:31:19 UTC+2 fir napisał(a):
> niedziela, 2 lipca 2023 o 17:54:50 UTC+2 Robert Latest napisał(a):
> > No. The question is what's wrong with putting the above into an (inline)
> > function or macro.
> side efects are wrong

many side efects..liek this is longer, not fully standarized and more quite important side effects

btw as to annoyances in c recently it showed to me that terribly
annoying is such thing

int foo(float x, float y, float z) instead of int foo(float x, y, z)
as it should be

when some code really writes this unnecessary floats in hundreds
(like tens a day) and thsi is practically one of practically worst annoyances

Re: clamp operator

<u7scam$1od0m$2@news.xmission.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=26048&group=comp.lang.c++#26048

  copy link   Newsgroups: comp.lang.c
 by: Kenny McCormack - Sun, 2 Jul 2023 17:32 UTC

In article <u7s5st$3bpd9$1@dont-email.me>,
Bonita Montero <Bonita.Montero@gmail.com> wrote:
....
>There's no need for such an operator since it would
>be used to seldom compared to all other operators.

I don't think "seldom" is a verb.

--

"If God wanted us to believe in him, he'd exist."

(Linda Smith on "10 Funniest Londoners", TimeOut, 23rd June, 2005.)

Re: clamp operator

<u7uj1c$3n1sa$4@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=26049&group=comp.lang.c++#26049

  copy link   Newsgroups: comp.lang.c
 by: David Brown - Mon, 3 Jul 2023 13:38 UTC

On 02/07/2023 16:48, Bonita Montero wrote:
> Am 02.07.2023 um 16:41 schrieb fir:
>> niedziela, 2 lipca 2023 o 16:36:29 UTC+2 Bonita Montero napisał(a):
>>> Am 01.07.2023 um 16:20 schrieb fir:
>>>> i think there is probably a need of clamp operator
>>>> in c
>>>> the question is how it could look like
>>>> it could be something like
>>>>
>>>> x = <0|x|650> but with some better operator
>>>> one alos may think of some keyword like CL
>>>> (clamp left) and CR (clamp right
>>>>
>>>> x = x CL 0;
>>>> x = x CR 650;
>>>>
>>>> but how it should look better in details i dont know
>>>>
>>> This type of clamping is used too seldom to require a special
>>> operator for it. Imagine you need this once for a thousand
>>> lines of code. A special operator would be rather confusing.
>>> The language is already perfect in that respect.
>>
>> not really.. clamping, min /max ids used relatively often,
>> for sure more often this say xor..probably abut the same as or and or
>> shifts..once per a thusand is not so rare btw as it would be once a 3
>> days of work (when i code its about 10k lines a month) (but then i
>> must rest its not to hold whole year 10k a month imo)
>>
>> also abs/fabs and sign should be built in imo,s ame as error operator
>>
>> foo() --> printf("error!");
>>
>> -> is error operator here (my invention)
>
> C++ has things like min(), max() and minmax() gerically, but
> nothing like clamping; so it looks like that clamping isn't
> needed that often. And there's the ternary operator which you
> can nest like that:
>     clamped = value >= min ? value <= max ? value : max : min
> That has a good readabity so you won't need clamping.
>

C++ /does/ have "clamp" in the standard library.

clamped = std::clamp(value, min, max);

has better readability.

<https://en.cppreference.com/w/cpp/algorithm/clamp>

It is, like min and max, rare enough that it should not be an operator,
but common enough that it is useful to have in the standard library.

Such functions were never in the C standard library. Before C99, there
were no generic functions (and a standardised way to write generic
functions had to wait for C11). Thus you'd need min, minl, minll, minf,
mind, minld variations for standard functions (and similarly for max and
clamp). You would not want a macro version of "min" in the standard
library, as it would evaluate at least one of the operands twice. So it
was a lot easier to let people write "a < b ? a : b". Since C11, a
type-generic version could have been added, but by then far too many
people had their own "min" functions or macros that would conflict with
the name.

Re: clamp operator

<u7ujmn$3nap2$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=26050&group=comp.lang.c++#26050

  copy link   Newsgroups: comp.lang.c
 by: David Brown - Mon, 3 Jul 2023 13:50 UTC

On 02/07/2023 17:31, Bart wrote:
> On 02/07/2023 15:57, fir wrote:
> >
> >
> > noy much good
> > the question is hovever what operator to use
> >
> > maybe x]b for min and  a[x for max , a[x]b for clamp..thsi is
> possibly the shortest
>
> The shortest isn't much good if nobody understands what it means, or
> can't remember.
>
> I use 'min' and 'max' since everybody will know what they mean,
> especially me!
>
> And while I can write them as `a max b`, I nearly always use `max(a, b)`
> instead, as it is clearer.
>
> Now we come to your [, ] operators. One of them means min, the other
> max, but which is which, and WHY?
>
> If a syntax also uses [] for indexing, those mismatched braces also
> introduce ambiguity:
>
>     A[i] ...
>
> If this indexing with `i`, or is that `]` part of a min or max operation
> (whichever one it is)?
>
> For that matter, is that /first/ `[` part of a max or min operation? Are
> /both/ operators min/max ones?
>
> Have a look at what symbols other languages, or mathematics, use for
> minimum and maximum.

In mathematics, "a ∨ b" is sometimes used for the maximum, and "a ∧ b"
for the minimum. But these are rare, partly because people get them
mixed up (think about what they would mean for Boolean logic, or
analogous to set union and intersection, to see why they are written
this way). It's more common to use "max" and "min", outside of specific
fields like lattice theory.

gcc used to have "a <? b" and "a >? b" as extensions for minimum and
maximum, but these were dropped long ago.

>
> I'm not aware of any except for APL that uses ⌊ and ⌈, funnily enough
> not much different from [ and ], but now you can make a good stab at
> which is which.
>
>

Re: clamp operator

<u7ukie$3ndhm$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=26051&group=comp.lang.c++#26051

  copy link   Newsgroups: comp.lang.c
 by: David Brown - Mon, 3 Jul 2023 14:05 UTC

On 02/07/2023 18:08, Lew Pitcher wrote:
> On Sun, 02 Jul 2023 15:54:36 +0000, Robert Latest wrote:
>
>> fir wrote:
>>>> clamped = value >= min ? value <= max ? value : max : min
>>>> That has a good readabity so you won't need clamping.
>>>
>>> noy much good
>>> the question is hovever what operator to use
>>
>> No. The question is what's wrong with putting the above into an (inline)
>> function or macro.
>
> A macro would evaluate "value" twice, leading to unintentional side-effects
> with some values of "value"
>
> Consider
> #define CLAMP(min,value,max) (value >= min ? value <= max ? value : max : min)
>
> int valu = 10,
> c1;
> c1 = CLAMP(5,valu++,15);
>
> which would expand the macro into
> c1 = (valu++ >= min ? valu++ <= max ? valu++ : max : min)
>
>

With C11, you could use a _Generic macro and a set of inline functions.

With gcc (or other compilers that copy the relevant extensions), you can
have :

#define clamp(value_, min_, max_) \
({ \
typeof(value_) _value = (value_); \
typeof(min_) _min = (min_); \
typeof(max_) _max = (max_); \
_value >= _min ? _value <= _max ? _value : _max : _min \
})

With C99, you could use a single "clamp" inline function working on
"long double" types, and trust the compiler to generate efficient code
even for integer values (gcc manages it for "int" on a quick test, but
it would not go well for 64-bit integer types).

Re: clamp operator

<cfe33b77-4c90-4e10-b564-1d535bf0db3cn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=26052&group=comp.lang.c++#26052

  copy link   Newsgroups: comp.lang.c
 by: fir - Mon, 3 Jul 2023 14:23 UTC

poniedziałek, 3 lipca 2023 o 15:50:29 UTC+2 David Brown napisał(a):
> On 02/07/2023 17:31, Bart wrote:
> > On 02/07/2023 15:57, fir wrote:
>
> gcc used to have "a <? b" and "a >? b" as extensions for minimum and
> maximum, but these were dropped long ago.
> >

if so thats not so bad i totally independently thought of a <? b as min..thought one sign is better , one sign in c could be a]b or better unicode vertically simetric with element down as min element up as max 7^a coud be as max but has no pair for min and maybe ^ is not bad for power 2^20 for megabyte etc

> > I'm not aware of any except for APL that uses ⌊ and ⌈, funnily enough
> > not much different from [ and ], but now you can make a good stab at
> > which is which.
> >
> >

[OT] Re: clamp operator

<87zg4dyryu.fsf_-_@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=26054&group=comp.lang.c++#26054

  copy link   Newsgroups: comp.lang.c
 by: Ben Bacarisse - Mon, 3 Jul 2023 14:44 UTC

David Brown <david.brown@hesbynett.no> writes:

> In mathematics, "a ∨ b" is sometimes used for the maximum, and "a ∧ b" for
> the minimum. But these are rare, partly because people get them mixed up
> (think about what they would mean for Boolean logic, or analogous to set
> union and intersection, to see why they are written this way).

This notation can be extended in a way that would also make it less
ambiguous by using / and \ as brackets. min(a,b,c) would be \a,b,c/ and
max(w,x,y,z) would be /w,x,y,z\.

This would be a very bad idea in C, but it might work in some other
language.

--
Ben.

Re: [OT] Re: clamp operator

<44e8007c-ad37-417d-a10b-4b3cc63eac99n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=26055&group=comp.lang.c++#26055

  copy link   Newsgroups: comp.lang.c
 by: fir - Mon, 3 Jul 2023 14:58 UTC

poniedziałek, 3 lipca 2023 o 16:45:13 UTC+2 Ben Bacarisse napisał(a):
> David Brown <david...@hesbynett.no> writes:
>
> > In mathematics, "a ∨ b" is sometimes used for the maximum, and "a ∧ b" for
> > the minimum. But these are rare, partly because people get them mixed up
> > (think about what they would mean for Boolean logic, or analogous to set
> > union and intersection, to see why they are written this way).
> This notation can be extended in a way that would also make it less
> ambiguous by using / and \ as brackets. min(a,b,c) would be \a,b,c/ and
> max(w,x,y,z) would be /w,x,y,z\.
>
> This would be a very bad idea in C, but it might work in some other
> language.

this is good idea but if you got 1-character min or max sy ^ for max it is shorter a^b^c^d is max of four

Re: clamp operator

<u7v5as$3p31n$3@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=26060&group=comp.lang.c++#26060

  copy link   Newsgroups: comp.lang.c
 by: Chris M. Thomasson - Mon, 3 Jul 2023 18:51 UTC

On 7/3/2023 6:38 AM, David Brown wrote:
> On 02/07/2023 16:48, Bonita Montero wrote:
>> Am 02.07.2023 um 16:41 schrieb fir:
>>> niedziela, 2 lipca 2023 o 16:36:29 UTC+2 Bonita Montero napisał(a):
>>>> Am 01.07.2023 um 16:20 schrieb fir:
>>>>> i think there is probably a need of clamp operator
>>>>> in c
>>>>> the question is how it could look like
>>>>> it could be something like
>>>>>
>>>>> x = <0|x|650> but with some better operator
>>>>> one alos may think of some keyword like CL
>>>>> (clamp left) and CR (clamp right
>>>>>
>>>>> x = x CL 0;
>>>>> x = x CR 650;
>>>>>
>>>>> but how it should look better in details i dont know
>>>>>
>>>> This type of clamping is used too seldom to require a special
>>>> operator for it. Imagine you need this once for a thousand
>>>> lines of code. A special operator would be rather confusing.
>>>> The language is already perfect in that respect.
>>>
>>> not really.. clamping, min /max ids used relatively often,
>>> for sure more often this say xor..probably abut the same as or and or
>>> shifts..once per a thusand is not so rare btw as it would be once a 3
>>> days of work (when i code its about 10k lines a month) (but then i
>>> must rest its not to hold whole year 10k a month imo)
>>>
>>> also abs/fabs and sign should be built in imo,s ame as error operator
>>>
>>> foo() --> printf("error!");
>>>
>>> -> is error operator here (my invention)
>>
>> C++ has things like min(), max() and minmax() gerically, but
>> nothing like clamping; so it looks like that clamping isn't
>> needed that often. And there's the ternary operator which you
>> can nest like that:
>>      clamped = value >= min ? value <= max ? value : max : min
>> That has a good readabity so you won't need clamping.
>>
>
> C++ /does/ have "clamp" in the standard library.
>
>     clamped = std::clamp(value, min, max);
>
> has better readability.
>
> <https://en.cppreference.com/w/cpp/algorithm/clamp>

Oh shit. I did not know about it! Thanks for the heads up.

>
>
> It is, like min and max, rare enough that it should not be an operator,
> but common enough that it is useful to have in the standard library.
>
> Such functions were never in the C standard library.  Before C99, there
> were no generic functions (and a standardised way to write generic
> functions had to wait for C11).  Thus you'd need min, minl, minll, minf,
> mind, minld variations for standard functions (and similarly for max and
> clamp).  You would not want a macro version of "min" in the standard
> library, as it would evaluate at least one of the operands twice.  So it
> was a lot easier to let people write "a < b ? a : b".  Since C11, a
> type-generic version could have been added, but by then far too many
> people had their own "min" functions or macros that would conflict with
> the name.
>
>
>

Re: clamp operator

<u7v72c$3pcrk$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=26061&group=comp.lang.c++#26061

  copy link   Newsgroups: comp.lang.c
 by: BGB - Mon, 3 Jul 2023 19:20 UTC

On 7/2/2023 9:48 AM, Bonita Montero wrote:
> Am 02.07.2023 um 16:41 schrieb fir:
>> niedziela, 2 lipca 2023 o 16:36:29 UTC+2 Bonita Montero napisał(a):
>>> Am 01.07.2023 um 16:20 schrieb fir:
>>>> i think there is probably a need of clamp operator
>>>> in c
>>>> the question is how it could look like
>>>> it could be something like
>>>>
>>>> x = <0|x|650> but with some better operator
>>>> one alos may think of some keyword like CL
>>>> (clamp left) and CR (clamp right
>>>>
>>>> x = x CL 0;
>>>> x = x CR 650;
>>>>
>>>> but how it should look better in details i dont know
>>>>
>>> This type of clamping is used too seldom to require a special
>>> operator for it. Imagine you need this once for a thousand
>>> lines of code. A special operator would be rather confusing.
>>> The language is already perfect in that respect.
>>
>> not really.. clamping, min /max ids used relatively often,
>> for sure more often this say xor..probably abut the same as or and or
>> shifts..once per a thusand is not so rare btw as it would be once a 3
>> days of work (when i code its about 10k lines a month) (but then i
>> must rest its not to hold whole year 10k a month imo)
>>
>> also abs/fabs and sign should be built in imo,s ame as error operator
>>
>> foo() --> printf("error!");
>>
>> -> is error operator here (my invention)
>
> C++ has things like min(), max() and minmax() gerically, but
> nothing like clamping; so it looks like that clamping isn't
> needed that often. And there's the ternary operator which you
> can nest like that:
>     clamped = value >= min ? value <= max ? value : max : min
> That has a good readabity so you won't need clamping.
>

One downside of this is that the compiler needs to figure it out and do
a more complex transform if the underlying ISA has an integer clamp
operation or similar (say, because a need for this operation often comes
up in performance-sensitive cases; such as clamping pixel values during
image and video decoding).

However... That said it makes more sense to do it like:
int __int_clamp(int val, int min, int max);
As a compiler builtin/intrinsic, and then maybe:
#define clampi(v, m, n) __int_clamp(v, m, n)
If the program includes the appropriate header.

Or, if some other compiler is used, it can decay into:
#define clampi(v, m, n) (((v)<(m))?(m):((v)>(n))?(n):(v))
Though, this may face issues if used with expressions that have
side-effects, ... (so, a static-inline or similar may make more sense in
this case).

But, no point for needlessly breaking the language syntax in any case...

Re: clamp operator

<e696a8a3-64cd-42c2-b9f7-32da896ec34fn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=26062&group=comp.lang.c++#26062

  copy link   Newsgroups: comp.lang.c
 by: fir - Mon, 3 Jul 2023 22:20 UTC

niedziela, 2 lipca 2023 o 16:17:19 UTC+2 fir napisał(a):
> niedziela, 2 lipca 2023 o 16:05:00 UTC+2 fir napisał(a):
> >
> > and the results TEST 26 ms TEST2 113 ms - ist probably by this stack passing thru..if so it shows that this kind of function pointers has not much sense..but may try yet some that not apsses arguments via stack
> tested with no args
>
> int testi;
> int x = 230, y=120, a=0;
> int f1() { return x+y+a;}
> int f2() { return x+y+a+1;}
> int f3() { return x+y+a+2;}
> int f4() { return x+y+a+3;}
> int f5() { return x+y+a+4;}
> int f6() { return x+y+a+5;}
> int f7() { return x+y+a+6;}
> int f8() { return x+y+a+7;}
>
> typedef int (*fp)() ;
> fp ftab[8] = {f1,f2,f3,f4,f5,f6,f7,f8};
>
> //[8] = {f1,f2,f3,f4,f5,f6,f7};
>
> void TEST()
> {
> for(int i=0; i<1000*1000*30; i++)
> {
> x++; y--; a=i;
> if(i&7==0) testi+=f1();
> else if(i&7==1) testi+=f2();
> else if(i&7==2) testi+=f3();
> else if(i&7==3) testi+=f4();
> else if(i&7==4) testi+=f5();
> else if(i&7==5) testi+=f6();
> else if(i&7==6) testi+=f7();
> else if(i&7==7) testi+=f8();
> }
>
> }
>
> void TEST2()
> {
> for(int i=0; i<1000*1000*30; i++)
> {
> x++; y--; a=i;
> testi+=ftab[i&7]();
> }
>
> }
>
> TEST 45 ms TEST2 455 ma - i dont get it, i was hoping the array of pointers would be faster..but im to weary to see the assembly etc
>
> (array of pointers avoid if logic so it should be faster for that noticably imo (in theory) byt maybe jumping thru pointers makes cache problems or what)
>
> testing on 10+ year old cpu (i got more new comp but more rarely use it as i like the old)

btw doesnt that terrible results dont mean that this polymorphism and jump tables/virtual function tables work in fact terrible, and switch would be faster?

Re: clamp operator

<f99f7e3b-8310-43e4-bd00-e4d00bc74a4fn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=26074&group=comp.lang.c++#26074

  copy link   Newsgroups: comp.lang.c
 by: fir - Thu, 6 Jul 2023 13:50 UTC

poniedziałek, 3 lipca 2023 o 15:50:29 UTC+2 David Brown napisał(a):
> On 02/07/2023 17:31, Bart wrote:
> > On 02/07/2023 15:57, fir wrote:
> > >
> > >
> > > noy much good
> > > the question is hovever what operator to use
> > >
> > > maybe x]b for min and a[x for max , a[x]b for clamp..thsi is
> > possibly the shortest
> >
> > The shortest isn't much good if nobody understands what it means, or
> > can't remember.
> >
> > I use 'min' and 'max' since everybody will know what they mean,
> > especially me!
> >
> > And while I can write them as `a max b`, I nearly always use `max(a, b)`
> > instead, as it is clearer.
> >
> > Now we come to your [, ] operators. One of them means min, the other
> > max, but which is which, and WHY?
> >
> > If a syntax also uses [] for indexing, those mismatched braces also
> > introduce ambiguity:
> >
> > A[i] ...
> >
> > If this indexing with `i`, or is that `]` part of a min or max operation
> > (whichever one it is)?
> >
> > For that matter, is that /first/ `[` part of a max or min operation? Are
> > /both/ operators min/max ones?
> >
> > Have a look at what symbols other languages, or mathematics, use for
> > minimum and maximum.
> In mathematics, "a ∨ b" is sometimes used for the maximum, and "a ∧ b"
> for the minimum. But these are rare, partly because people get them
> mixed up (think about what they would mean for Boolean logic, or
> analogous to set union and intersection, to see why they are written
> this way). It's more common to use "max" and "min", outside of specific
> fields like lattice theory.
>
> gcc used to have "a <? b" and "a >? b" as extensions for minimum and
> maximum, but these were dropped long ago.
> >
> > I'm not aware of any except for APL that uses ⌊ and ⌈, funnily enough
> > not much different from [ and ], but now you can make a good stab at
> > which is which.
> >

note in fact some of such thinks shouldnt go in c standard ass aditions (i mean thsi clamp /min/max operators) though tpes float3 and arithmetic should but imo they should go in gcc additions imo. .soem other can be applied only as a new language


devel / comp.lang.c / Re: clamp operator

Pages:12
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor