Hi all
I´m using gpc as a cross compiler (from sparc to ppc) and I have many old files from an irix/mips pascal compiler and in one file I got some problem. It seemes that the new gpc (20010201) does not manage to handle a calculation in a type casting statement. The code doesn´t get through the compiler.
Example: Program bug; Var m:real; a:integer; begin m :=10.0; a := integer(m*1000); (* line 7 *) writeln; end.
->gpc -c bug.p bug.p: In main program: bug.p:7: warning: cast to type of different size bug.p:7: reference expected, value given ->
/Björn Persson
bpn wrote:
I´m using gpc as a cross compiler (from sparc to ppc) and I have many old files from an irix/mips pascal compiler and in one file I got some problem. It seemes that the new gpc (20010201) does not manage to handle a calculation in a type casting statement.
No, as you can try for yourself if you like, the problem is not with the multiplication -- it's with casting a real to an integer.
Type-casting as understood by GPC is "BP style", i.e. it's either (a) forcing a value conversion that can also be done automatically, e.g. Integer->Real, or Integer->LongInt, (that is the same as assigning the value to a temporary variable of the destination type and using that variable in place of the type-cast) or (b) interpreting a references as the same bit pattern using another type of the same size.
Both do not apply here: (a) There is no automatical Real->Integer conversion in Pascal, and (b) the sizes of Integer and Real types differ (at least on some platforms, apparently including yours), and even if not, re-interpreting the bit pattern would not be what you want, anyway.
So, why is there is no automatical Real->Integer conversion in Pascal? Because there's no unique way to define this and you have to specify what to do with fractions. Pascal supports two predefined functions, Trunc (convert Real to Integer, truncating fractions) and Round (rouding towards the nearest integer). Both are part of Standard Pascal, and supported by any Pascal dialect and compiler, AFAIK. So I suggest you use one of these functions (which most likely also works with your old Pascal compiler if that's an issue).
While we usually try to implement as many compatibility features to other compilers as possible, this one is not a good candidate. Overloading type-casting with that meaning would be redundant, unclear (which of the truncation/rounding modes to use) and even more confusing than type-casting already is...
Frank