Wrong cast? lazarus / 21-General ---------------------------------------------------------------------- Ivo · 2023-10-13 17:19 Hi, my first post here. While trying to convert some Delphi code to FreePascal/Lazarus, I ran into numerical problems not getting the same (or very similar) results. I traced the issue to what seems to be a wrong cast of a numerical expression: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i WriteLn('-2.0/3.0: ', -2.0/3.0); // gives -6.666666865E-01  (single precision)WriteLn('-2/3: ', -2/3); // gives -6.6666666666666663E-001  (double precision) It happens even when I send the value as a parameter: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure Test(d: double);begin  WriteLn(d); end; begin  Test(-2.0/3.0);  // prints -6.6666668653488159E-001  Test(-2/3);      // prints -6.6666666666666663E-001end. FreePascal 3.2.2, Win10 64bit (target Win64, CPU family x86_64), in both objfpc and delphi modes. It seems that FPC compiler is treating -2.0/3.0 as single precision, while -2/3 as double. Maybe I'm doing something wrong. I know, it is an easy fix, but it should not behave like that, right? ---------------------------------------------------------------------- WooBean · 2023-10-13 17:39 Try this ... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;begin  writeln('sizeof(2)=',sizeof(2));  writeln('sizeof(2/3)=',sizeof(2/3));  writeln('sizeof(2.0)=',sizeof(2.0));  writeln('sizeof(2.0/3.0)=',sizeof(2.0/3.0));  readln; end. //output: sizeof(2)=1 sizeof(2/3)=8 sizeof(2.0)=4 sizeof(2.0/3.0)=4 Generally, FPC tries to use the less memory consuming type to represemt a constant (a real one too). 2/3 is treated in a bit different way. ---------------------------------------------------------------------- Ivo · 2023-10-13 18:14 QuoteTry this ...I got the same output as you: sizeof(2)=1 sizeof(2/3)=8 sizeof(2.0)=4 sizeof(2.0/3.0)=4 QuoteGenerally, FPC tries to use the less memory consuming type to represemt a constant (a real one too).But, is that good behavior? Is there a compiler option to turn that behavior off (I looked but couldn't find any) or to set default precision for floating point numbers? Why it bothers me is because I have exponential and power raising functions dealing with double precision variables and this slight difference between single/double precision gets magnified. And I expect 2/3 to be the same as 2.0/3.0. In Delphi 10.4, it works as expected, i.e. all float values from your code are 8 bytes long, not 8/4/4 as in fpc. ---------------------------------------------------------------------- Ivo · 2023-10-13 19:22 QuoteIs there a compiler option to turn that behavior off (I looked but couldn't find any) or to set default precision for floating point numbers? Found it!  $MINFPCONSTPREC or equivalent -CF command line switch. Apologies for not finding it earlier. https://www.freepascal.org/docs- html/prog/progsu52.html#x59-580001.2.52