Sign in Join

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

Mailing lists / fpc-pascal / 2024-01

Quiz

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Adriaan van Os

Quiz.

Which of below constant declarations are accepted by the compiler and what is their value ?

   const
     ki1                               = + 2;
     ki2                               = +2;
     ki3                               = 2+;
     kr1                               = 1.;
     kr2                               = 1e-2;
     kr3                               = 1e+2;
     kr4                               = 1.e+2;
     kr5                               = 1 . e + 2;
     kr6                               = 1 . 0 e + 2;
     kr7                               = 1.0e+2;
     kr8                               = 1.0 e + 2;

The more difficult question is, why ?

Regards,

Adriaan van Os

Reply by email

Michael Van Canneyt
to Adriaan van Os
Adriaan van Os wrote 9 lines
Quiz. Which of below constant declarations are accepted by the compiler and what is their value ? const ki1 = + 2; ki2 = +2;

+x is a unary expression.

ki3 = 2+;

Does not compile, the binary expression is not complete.

kr1 = 1.;

There is some special handling for 1.
(ending with a dot) due to Delphi/TP compatibility, they accept it.

kr2 = 1e-2; kr3 = 1e+2; kr4 = 1.e+2;

kr4: Same as kr1 in FPC, but Delphi does not accept it.

kr5 = 1 . e + 2;

Same as kr1, but not accepted by Delphi.

kr6 = 1 . 0 e + 2;

kr6 does not compile, although I don't see why if kr5 is accepted..

Delph does not accept it.

kr7 = 1.0e+2; kr8 = 1.0 e + 2;

kr8 Does not compile, probably same reason as kr6. Delphi does not accept it.

Michael.

Reply by email

Tomas Hajny
to Michael Van Canneyt
Michael Van Canneyt wrote
On Sun, 7 Jan 2024, Adriaan van Os via fpc-pascal wrote:

.
.

There is some special handling for 1. (ending with a dot) due to Delphi/TP compatibility, they accept it.

.
.

> kr5 = 1 . e + 2; Same as kr1, but not accepted by Delphi.

Personally, I don't think that a dot directly following a number is the same as a dot separated from the number by a space. TP/BP doesn't accept the latter (i.e. kr5).

> kr6 = 1 . 0 e + 2; kr6 does not compile, although I don't see why if kr5 is accepted..

Well, I'm not sure whether kr5 should be accepted, but anyway, there are IMHO two problems here - the second one being the space before the exponent marker.

> kr8 = 1.0 e + 2; kr8 Does not compile, probably same reason as kr6. Delphi does not accept it.

TP/BP requires the exponent marker (i.e. "e") to follow the number directly. Spaces in the exponent part are allowed, but not before "e".

Tomas

Reply by email