[fpc-pascal] Order of Precedence: FPC/Delphi vs Java

mailinglists at geldenhuys.co.uk mailinglists at geldenhuys.co.uk
Wed Oct 3 10:40:54 CEST 2018


I have this simple little test. My expected answer for all the 
calculations are 0 (zero), but both FPC and Delphi give different 
results. Java is the only one that seems consistent regarding the 
results.

Can anybody explain this, especially the first 3 very small negative 
numbers that Delphi and FPC produces? I know  not all floating point 
values can be stored exactly (or something like that), and when doing 
calculations, it might do a auto data type conversion. But is any of 
that taking place here?

The only positive I can see, is that FPC and Delphi are consistent. :-)

Here is the Object Pascal test:

===============[ TestOperatorPrecedence.pas ]=================
program TestOperatorPrecedence;

{$IFDEF FPC}
   {$mode objfpc}{$H+}
{$ELSE}
   {$apptype console}
{$ENDIF}

var
   a,b,c,d: double;
   ans: extended;
begin
   a := 1.0;
   b := 51009.9;
   c := 51009.9;
   d := 51009.9;
   writeln(b);
   writeln(c);
   writeln(d);
   writeln('---');

   ans := c - b * c / d;
   writeln(ans);

   ans := c - (b * c) / d;
   writeln(ans);

   ans := c - (b * c / d);
   writeln(ans);

   ans := c - b * (c / d);
   writeln(ans);


   ans := c - (b * (c / d));
   writeln(ans);
end.
==============================================================

And the results are:
graeme.geldenhuys at UKCM-L500737 C:\devel\tests\OperatorPrecedence
> TestOperatorPrecedence.exe
  5.10099000000000E+0004
  5.10099000000000E+0004
  5.10099000000000E+0004
---
-3.55271367880050E-0015
-3.55271367880050E-0015
-3.55271367880050E-0015
  0.00000000000000E+0000
  0.00000000000000E+0000


And here is the Java equivalent...


===============[ TestOperatorPrecedence.java ]================
public class TestOperatorPrecedence {

	public static void main(String[] args) {
		double b = 51009.9;
		double c = 51009.9;
		double d = 51009.9;
		double ans;

		System.out.println(b);
		System.out.println(c);
		System.out.println(d);
		System.out.println("-----");

		ans = c - b * c / d;
		System.out.println(ans);

		ans = c - (b * c) / d;
		System.out.println(ans);

		ans = c - (b * c / d);
		System.out.println(ans);

		ans = c - b * (c / d);
		System.out.println(ans);


		ans = c - (b * (c / d));
		System.out.println(ans);
	}
}

==============================================================

And the results are:
graeme.geldenhuys at UKCM-L500737 C:\devel\tests\OperatorPrecedence
> java TestOperatorPrecedence
51009.9
51009.9
51009.9
-----
0.0
0.0
0.0
0.0
0.0



Regards,
   Graeme






More information about the fpc-pascal mailing list