Re: [fpc-pascal] Strange floating-point result

2022-11-11 Thread Thomas Kurz via fpc-pascal
Understood, ok. Thanks for your explanation.

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Strange floating-point result

2022-11-11 Thread Jonas Maebe via fpc-pascal

On 11/11/2022 11:49, Thomas Kurz via fpc-pascal wrote:

Why do I have to explicitly cast Integer to Double here?


Because 1.2E6 can be represented exactly by single precision.

You can use -CF64 to force floating point constants to have at least 64 
bits precision. Or you can use {$excessprecision on} to force the 
compiler to evaluate all floating point expressions always using the 
maximum precision supported by the FPU.



Jonas
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


[fpc-pascal] Strange floating-point result

2022-11-11 Thread Thomas Kurz via fpc-pascal
Hello,

by chance, I stumbled upon a strange behavior in floating-point calculation. 
Here's the example:

program Project1;

var
  f: double;
  n: integer = 1758;
  m: integer = 0;

begin
  f := n * 1.2E6 + (2*m+1) * 50E3; // 2109650048
  f := Double(n) * 1.2E6 + Double(2*m+1) * 50E3; // 210965
end.

Why do I have to explicitly cast Integer to Double here? I never did that 
before and am a bit worried whether I could get wrong results elsewhere, too, 
without having them noticed yet. And how does the first value come about?

FPC is 3.2.2 on Win64, targeting Win64.

Kind regards,
Thomas

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal