Thread View: comp.lang.c
104 messages
104 total messages
Page 3 of 3
Started by fir
Fri, 23 Oct 2026 05:44
Page 3 of 3 • 104 total messages
Re: variadic min and max, (any some and none, cat and more)
Author: David Brown
Date: Wed, 02 Dec 2026 08:02
Date: Wed, 02 Dec 2026 08:02
111 lines
6512 bytes
6512 bytes
On 01/12/2026 22:09, bartc wrote:
> On 01/12/2026 20:25, David Brown wrote:
>> On 01/12/2026 18:48, bartc wrote:
>>> On 01/12/2026 13:53, David Brown wrote:
>>>> On 01/12/2026 13:11, bartc wrote:
>>>
>>>>> Some more:
>>>>>
>>>>> v := (100, 0)
>>>>> m := rotatematrix(15°, "Z")
>>>>> println v * (m * m) # rotate 30 deg ac/w
>>>
>>>>> I found it was simplest just to build-in whatever is needed (this
>>>>> language was used with my 3D graphics app). No imports or libraries
>>>>> are needed, and every user had the same abilities out-of-the-box.
>>>>>
>>>>>
>>>>
>>>> When you have a domain-specific language, and full control of the
>>>> language, then built-ins can be easier. The point of techniques
>>>> like this, whether in Python or C++, is that the language and
>>>> toolchain only needs to support the technique. Then the individual
>>>> users or library vendors can use it for any operations and any
>>>> types. If someone wants to make a type that supports 8 x 8 matrices
>>>> over complex doubles with a binary exponential operator, and write
>>>> "matrix A = B *pow* C;", then they can do that with pretty much
>>>> identical code as this. Well, they'd have to write code for the
>>>> exponential operator in question, and good luck doing that
>>>> efficiently manner while keeping numeric stability. But the
>>>> wrapping code to support the syntax is straightforward.
>>>>
>>>> Some other languages, like Haskell, make it easier to write your own
>>>> operators and also let you use symbols for them. C++ lets you write
>>>> "rotatematrix(15_deg, "Z")", but not "15°". (You'd have to find a
>>>> Unicode letter that looks like a degree symbol.)
>>>
>>> I had the option to use either ° or 'deg':
>>
>> When you are making the language, you make the rules :-)
>>
>>>
>>> 15 deg
>>>
>>> There were a number of such suffixes, which acted as scale factors
>>> when they followed a constant number. Mostly they were used for
>>> lengths, such as "m mm ft ins" (these converted the number to a
>>> standard unit, and in my app that was millimetres).
>>>
>>
>> Was it just a scaling factor, or did you distinguish types? In C++,
>> when you do this sort of thing (called "user-defined literals") you
>> generally attach a specific type to it. Thus if you have "auto x =
>> 10_m;", "auto y = 5_s;", trying to add x and y (adding a distance to a
>> time) would be a compile-time error - but you could divide them to get
>> a speed.
>>
>
> It's just a scale factor. The result is a generic FP type.
>
> I have looked into properly implementing units, where multiplying tgwo
> length types for example gives you an area type, and where area + length
> is not allowed, but it very quickly gets out of hand and turns into a
> can of works.
>
> In any case it requires a much stricter, and hierarchical, type system,
> such as you might find in Ada, and may be possible in C++.
>
It does not require hierarchical types (there are no type hierarchies
and inheritance needed), but it does require users to be able to make
their own types and define overloaded operators on them between
different types, and their own conversion functions, to be user-friendly.
C can provide the strong and strict types (using structs), but not the
overloads. So in C, you can end up with :
Acceleration a = Acceleration_C(9.81);
Time t = read_clock();
Distance d = mult(mult(0.5, a), mult(t, t));
("mult" here would be a _Generic macro.)
while in C++ you can have :
Acceleration a { 9.81 }; // or auto a = 9.81_m_p_ss
Time t = read_clock();
Distance d = 0.5 * a * t * t;
So this /can/ be done in C, but it is much more cumbersome to define,
less scalable (you will want to generate the _Generic macros in some
way, rather than writing them by hand!) and the result is ugly.
Most importantly, however, you need the language to support defining
these types and operators yourself. Adding support for lengths and
times might be okay in the language itself, but imagine trying to add
/all/ SI units to the compiler. And even then, someone will come along
that wants to measure speed in feet per nanosecond. (Award yourself a
pat on the back if you know who that might be!)
> I mentioned Frink (https://frinklang.org/) which supports thousands of
> physical units. That is so comprehensive that I decided it would be
> pointless to emulate a tiny fraction of it within my own PL.
>
> That's fine; my Casio works perfectly will with unitless numbers. So can
> a PL!
Sure. But sometimes it can work better with units - it can eliminate a
class of programming errors, and programmers need all the help they can get.
Typed variables and values (was Re: variadic min and max [...])
Author: Janis Papanagnou
Date: Wed, 02 Dec 2026 11:33
Date: Wed, 02 Dec 2026 11:33
26 lines
2885 bytes
2885 bytes
On 2026-12-02 09:02, David Brown wrote: > On 01/12/2026 22:09, bartc wrote: >> [...] >> That's fine; my Casio works perfectly will with unitless numbers. So >> can a PL! > > Sure. But sometimes it can work better with units - it can eliminate a > class of programming errors, and programmers need all the help they can > get. One of the more well known and popular macro-events (from the quite many existing): https://en.wikipedia.org/wiki/Mars_Climate_Orbiter (Quite hilarious if it weren't that costly, and if people/companies didn't learn from it.) Concerning the units more inherent to language items; Kornshell's 'typeset' somewhat goes in that direction - at least technically. As one can define a variable to have certain technical properties (concerning the representation of their values) that could also have been extended for a semantic property, which unit (or unit system) is associated to those variables; unit conversions could then be performed implicitly (or, in a poor man's variant, just throw an error on unit mismatches, or supporting both). Janis
Re: variadic min and max, (any some and none, cat and more)
Author: bartc
Date: Wed, 02 Dec 2026 16:41
Date: Wed, 02 Dec 2026 16:41
79 lines
4992 bytes
4992 bytes
On 02/12/2026 08:02, David Brown wrote:
> On 01/12/2026 22:09, bartc wrote:
>> On 01/12/2026 20:25, David Brown wrote:
>> It's just a scale factor. The result is a generic FP type.
>>
>> I have looked into properly implementing units, where multiplying tgwo
>> length types for example gives you an area type, and where area +
>> length is not allowed, but it very quickly gets out of hand and turns
>> into a can of works.
>>
>> In any case it requires a much stricter, and hierarchical, type
>> system, such as you might find in Ada, and may be possible in C++.
>>
>
> It does not require hierarchical types (there are no type hierarchies
> and inheritance needed), but it does require users to be able to make
> their own types and define overloaded operators on them between
> different types, and their own conversion functions, to be user-friendly.
If you look at the Ada type chart, there is a kind of hierarchy (eg.
real (float) types are a subset of scalars).
But what I was thinking of is that a Length type for example would be
based on top of a generic Float type, and would inherit some of its
characteristics.
(And, somebody might define new types on top of Length which represent
different length applications, that they don't want to mix.)
> C can provide the strong and strict types (using structs), but not the
> overloads. So in C, you can end up with :
>
> Acceleration a = Acceleration_C(9.81);
> Time t = read_clock();
> Distance d = mult(mult(0.5, a), mult(t, t));
>
> ("mult" here would be a _Generic macro.)
>
> while in C++ you can have :
>
> Acceleration a { 9.81 }; // or auto a = 9.81_m_p_ss
(It used to be 9.82 when I was at school; gravity must have changed!)
> Time t = read_clock();
> Distance d = 0.5 * a * t * t;
>
> So this /can/ be done in C, but it is much more cumbersome to define,
> less scalable (you will want to generate the _Generic macros in some
> way, rather than writing them by hand!) and the result is ugly.
>
> Most importantly, however, you need the language to support defining
> these types and operators yourself. Adding support for lengths and
> times might be okay in the language itself, but imagine trying to add /
> all/ SI units to the compiler.
My point about Frink is that it is futile to add them to a language no
matter how it's done, unless you are implementing Frink.
> And even then, someone will come along
> that wants to measure speed in feet per nanosecond. (Award yourself a
> pat on the back if you know who that might be!)
I can't think who that might be, however I can see the attraction of a
'normalised' measure of velocity that is always within 0 and 1.
But, AFAIK that's not how such units would work. With my Length example,
the quantity stored would used a fixed unit (probably metres for SI).
So you could add two lengths knowing they used the same unit. If one
length uses cm and another furlongs, then it would get a LOT more
complicated.
To keep it simple, velocity would always be metres/second say, and any
other denotations for UI purposes would be done via conversions.
Page 3 of 3 • 104 total messages
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads