Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2013-04

Array assignment, and not-a-number

8 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Mark Morgan Lloyd

I was having a bit of fun earlier while waiting for a call. Am I correct in believing that something like

type    t1= array of integer;
var     a1: t1;
	x: integer;
a1 := [1,2,3,4,5];
can't be done at present, irrespective of any custom definition of the
:= operator?

Is there an accessible not-a-number constant, to allow me to do something like

var     reduce: double= NAN;
   x := reduce + a1;

with an appropriate definition of the + operator without risk that an error would try to evaluate it as an ordinary arithmetic expression?

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Jonas Maebe
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote 10 lines
I was having a bit of fun earlier while waiting for a call. Am I correct in believing that something like type t1= array of integer; var a1: t1; x: integer; a1 := [1,2,3,4,5]; can't be done at present, irrespective of any custom definition of the := operator?

Maybe if you overload the assignment operator for "t2=set of byte" to "t1" assignments, but I'm not certain. It's definitely not something we explicitly support.

Show quoted text 7 lines
Is there an accessible not-a-number constant, to allow me to do something like var reduce: double= NAN; x := reduce + a1; with an appropriate definition of the + operator without risk that an error would try to evaluate it as an ordinary arithmetic expression?

We don't have constant to identify a qNaN. I don't even know whether all architectures support them. Currently, the only way to achieve the above is by masking exceptions for invalid floating point operations: math.SetExceptionMask(math.GetExceptionMask + [exInvalidOp]).

You cannot overload/override operators that have a built-in meaning in the language (such as double+double).

Jonas

Reply by email

Marco van de Voort
to Jonas Maebe

In our previous episode, Jonas Maebe said:

Show quoted text 5 lines
> with an appropriate definition of the + operator without risk that an error would try to evaluate it as an ordinary arithmetic expression? We don't have constant to identify a qNaN. From math:
       NaN = 0.0/0.0;
       Infinity = 1.0/0.0;
       NegInfinity = -1.0/0.0;

but of course that is just notation/convention (that 0.0/0.0 as literal evaluates to NaN), it doesn't mean all archs do.

Reply by email

Mark Morgan Lloyd
to Jonas Maebe: Maybe if you overload the assignment operator for "t2=set of byte" to "t1" assignments,…
Jonas Maebe wrote 14 lines
On 15 Apr 2013, at 16:48, Mark Morgan Lloyd wrote: > I was having a bit of fun earlier while waiting for a call. Am I correct in believing that something like > > type t1= array of integer; > > var a1: t1; > x: integer; > > a1 := [1,2,3,4,5]; > > can't be done at present, irrespective of any custom definition of the := operator? Maybe if you overload the assignment operator for "t2=set of byte" to "t1" assignments, but I'm not certain. It's definitely not something we explicitly support.

That's a good point, although obviously ordering would be lost. After I'd tinkered with it for a while I concluded that it was probably related to the discussion of tuples a few weeks ago.

Show quoted text 9 lines
> Is there an accessible not-a-number constant, to allow me to do something like > > var reduce: double= NAN; > > x := reduce + a1; > > with an appropriate definition of the + operator without risk that an error would try to evaluate it as an ordinary arithmetic expression? We don't have constant to identify a qNaN. I don't even know whether all architectures support them. Currently, the only way to achieve the above is by masking exceptions for invalid floating point operations: math.SetExceptionMask(math.GetExceptionMask + [exInvalidOp]).

Thanks, noted. In practical terms the value isn't important, and I could obviously use something like a null variant or for that matter a nil pointer as a marker.

You cannot overload/override operators that have a built-in meaning in the language (such as double+double).

Understood, but what I'm doing here is using "reduce +" as an experimental alternative to APL's +/ or Vector Pascal's \+ to reduce the dimension of the right operand by applying the operator:

c1:
7 9 11 13 5
+/ c1:
45

I've not investigated SSE or spreading over multiple cores yet, and probably won't due to time issues. My only incentive was looking for some alternative to Vector Pascal's higher-level functions, since obviously something like \<operator> <array> couldn't be achieved without major compiler surgery.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Mark Morgan Lloyd
to Marco van de Voort: In our previous episode, Jonas Maebe said: NaN = 0.0/0.0; Infinity = 1.0/0.0; NegInfinity…
Marco van de Voort wrote 13 lines
In our previous episode, Jonas Maebe said: >> with an appropriate definition of the + operator without risk that an error would try to evaluate it as an ordinary arithmetic expression? > We don't have constant to identify a qNaN. >From math: NaN = 0.0/0.0; Infinity = 1.0/0.0; NegInfinity = -1.0/0.0; but of course that is just notation/convention (that 0.0/0.0 as literal evaluates to NaN), it doesn't mean all archs do.

Thanks for that. Initial experiment using a NaN real or nil pointer throws an exception as soon as it's passed as the parameter which I suppose is not entirely surprising. Using varnull as a placeholder seems safest, although it looks as though I can't initialise a variant at declaration... WTH, it's just a toy for the
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Sven Barth
to Mark Morgan Lloyd: That's a good point, although obviously ordering would be lost. After I'd tinkered with…
Mark Morgan Lloyd wrote 23 lines
Jonas Maebe wrote: > On 15 Apr 2013, at 16:48, Mark Morgan Lloyd wrote: > >> I was having a bit of fun earlier while waiting for a call. Am I >> correct in believing that something like >> >> type t1= array of integer; >> >> var a1: t1; >> x: integer; >> >> a1 := [1,2,3,4,5]; >> >> can't be done at present, irrespective of any custom definition of >> the := operator? > > Maybe if you overload the assignment operator for "t2=set of byte" to > "t1" assignments, but I'm not certain. It's definitely not something > we explicitly support. That's a good point, although obviously ordering would be lost. After I'd tinkered with it for a while I concluded that it was probably related to the discussion of tuples a few weeks ago.

Not really.

Also the principial components are already available in the compiler so in theory (!) one could implement this. Array constructors (the "[...]" notation) is already used for open array parameters and array initialization is already possible for named dynamic arrays in the form of the delphi compatible "TMyDynamicArrayType.Create(1, 2, 3, 4, 5);" (yes, I think this is a bad choice of implementing it and comes probably from Delphi.NET :( )

Regards,
Sven

Reply by email

Mark Morgan Lloyd
to Sven Barth
Sven Barth wrote 9 lines
> That's a good point, although obviously ordering would be lost. After > I'd tinkered with it for a while I concluded that it was probably > related to the discussion of tuples a few weeks ago. Not really. Also the principial components are already available in the compiler so in theory (!) one could implement this. Array constructors (the "[...]" notation) is already used for open array parameters

Although if I understand things correctly that's for the specific case of a parameter passed to a procedure of function, not for the operand of a redefined operator such as := even though the definition looks like a function.

Show quoted text 5 lines
and array initialization is already possible for named dynamic arrays in the form of the delphi compatible "TMyDynamicArrayType.Create(1, 2, 3, 4, 5);" (yes, I think this is a bad choice of implementing it and comes probably from Delphi.NET :( )

So it might be able to do it with a type helper, at the cost of looking like a function rather than like an ordinary assignment.

Of course one place where what I'm playing with gets hairy is in overlaid operator definition: I'm having to define e.g. + for a pair of 1-D dynamic arrays, a pair of 2-D dynamic arrays and so on. If I understand things correctly it's not possible to define a generalised dynamic array parameter where the dimension (number of axes/indexes) as well as the rank (range of each index) is undefined.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Sven Barth
to Mark Morgan Lloyd

Am 15.04.2013 22:32 schrieb "Mark Morgan Lloyd" <

Show quoted text 12 lines
: Sven Barth wrote: >> That's a good point, although obviously ordering would be lost. After >> I'd tinkered with it for a while I concluded that it was probably >> related to the discussion of tuples a few weeks ago. > > > Not really. > > Also the principial components are already available in the compiler so

in theory (!) one could implement this. Array constructors (the "[...]" notation) is already used for open array parameters

Although if I understand things correctly that's for the specific case of

a parameter passed to a procedure of function, not for the operand of a redefined operator such as := even though the definition looks like a function.

Both are parsed using the expression parser... I would even go that far to say that it's "just" the type checking in the assignment code that doesn't handle such array assignments yet (and yes, this is a bit simplified compared to the real situation ^^)

Regards,
Sven

Reply by email