RE: CONST arithmetic

From: CFB Software <"CFB>
Date: Fri, 3 Dec 2004 09:36:32 +1030

----boundary-LibPST-iamunique-1013063478_-_-
Content-type: text/plain

> -----Original Message-----
> From: Campbell, Robert D (UK Edinburgh)
> [mailto:robert.d.campbell{([at]})nowhere.xy
> Sent: Thursday, 2 December 2004 9:38 PM
> To: blackbox{([at]})nowhere.xy
> Subject: CONST arithmetic
>
>
>
> I have some code:
>
> CONST
> arrayLen = 5;
> arrayMax = arrayLen - 1;
>
> VAR
> xyx : REAL;
>
> BEGIN
> xyz := 100. / arrayMax;
>
>
> Fine.
> But for a simple test case I set arraylen = 1.
> I get a compiler error 'division by zero'.
>
> This is not an error; my algorithm will work fine with
> xyz = INF.
>

I would have expected this to be handled OK by the compiler. It sounds
like a deficiency in its constant expression evaluation process.


> Even if I add
>
> IF arraylen > 1 THEN xyz := 100. / arrayMax END
>
> I still get a compiler error.
>
> Is this a restriction in the language, or the compiler, or ...?
>
> I thought that the compiler removed 'dead' code of the form:
>
> IF FALSE THEN ... END anyway.
>

I wouldn't have expected this to make any difference in this situation.
The compiler can't simply ignore what follows the 'IF FALSE THEN' It
actually has to parse what follows so that it knows when it has passed
all of the dead code. It is the parsing process which does the constant
calculations etc. Thus it will hit the same problem. Similarly if you
had any syntax errors in the dead code, these would be reported as well.


If the compiler does remove 'dead' code, (which is not a requirement of
the language), it is quite possible that it does it at a later stage,
once parsing has been completed and the code is being generated.
 
In the meantime, a workaround to this problem is to use a variable to
prevent the compiler attempting to evaluate the division calculation at
compile-time. e.g.

CONST
  arrayLen = 1;
  arrayMax = arrayLen - 1;

VAR
  xyx: REAL;
  aMax: INTEGER;
 
BEGIN
  aMax := arrayMax;
  xyz := 100.0 / aMax;
 
Regards,
Chris Burrows

CFB Software
http://www.cfbsoftware.com
info{([at]})nowhere.xy

--------------------------------------------

To unsubscribe from this mailing list, send a message containing the word "unsubscribe" to:
   blackbox-request{([at]})nowhere.xy

To get a list of valid e-mail commands and instructions on their usage, send a message containing the word "help" to the above address.

Send any problem reports or questions related to this email list to the list owner at
   owner-blackbox{([at]})nowhere.xy

Current posting policy:

a) To post you should use the same address by which you are subscribed to the mailing list. That way, the list server will recognize you as subscriber and forward your posting immediately, without creating any overhead.

b) If, for some reason, you cannot post from the address, by which you are subscribed, your message will be moderated to avoid spam. Please understand that moderation will often cause some delay, in particular over weekends or holydays.


----boundary-LibPST-iamunique-1013063478_-_-
Content-type: application/rtf
Content-transfer-encoding: base64
Content-Disposition: attachment; filename="rtf-body.rtf"

e1xydGYxXGFuc2lcYW5zaWNwZzEyNTJcZnJvbXRleHQgXGRlZmYwe1xmb250dGJsDQp7XGYwXGZz
d2lzcyBBcmlhbDt9DQp7XGYxXGZtb2Rlcm4gQ291cmllciBOZXc7fQ0Ke1xmMlxmbmlsXGZjaGFy
c2V0MiBTeW1ib2w7fQ0Ke1xmM1xmbW9kZXJuXGZjaGFyc2V0MCBDb3VyaWVyIE5ldzt9fQ0Ke1xj
b2xvcnRibFxyZWQwXGdyZWVuMFxibHVlMDtccmVkMFxncmVlbjBcYmx1ZTI1NTt9DQpcdWMxXHBh
cmRccGxhaW5cZGVmdGFiMzYwIFxmMFxmczIwID4gLS0tLS1PcmlnaW5hbCBNZXNzYWdlLS0tLS1c
cGFyDQo+IEZyb206IENhbXBiZWxsLCBSb2JlcnQgRCAoVUsgRWRpbmJ1cmdoKSBccGFyDQo+IFtt
YWlsdG86cm9iZXJ0LmQuY2FtcGJlbGxAYmFlc3lzdGVtcy5jb21dIFxwYXINCj4gU2VudDogVGh1
cnNkYXksIDIgRGVjZW1iZXIgMjAwNCA5OjM4IFBNXHBhcg0KPiBUbzogYmxhY2tib3hAb2Jlcm9u
LmNoXHBhcg0KPiBTdWJqZWN0OiBDT05TVCBhcml0aG1ldGljXHBhcg0KPiBccGFyDQo+IFxwYXIN
Cj4gXHBhcg0KPiBJIGhhdmUgc29tZSBjb2RlOlxwYXINCj4gXHBhcg0KPiBDT05TVFxwYXINCj4g
ICBhcnJheUx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