2

The running example is really simple to understand:

program Project1;

uses
SysUtils, Math;

var
  fValue: double;
  fValueMax: double;
begin
  fValue := 7.0207503445953527;
  fValueMax := Max(0, fValue);
  writeln(fValue);
  writeln(fValueMax);
  readln;
end.   

However the result is completely unexpected. For some reason, the Max function does not only return the larger number from the two arguments but also changes it's value.

In the example code above, the expected value of fValueMax is exactly fValue, but instead fValueMax is bigger. The difference is approximately E-7, so small, but still unexpected and crashes my following code (which is not published here to keep the question clear and simple).

5
  • Depending on the compiler and optimizations, the variable might actually be held in a floating point register which has higher precision than a floating point variable type. When calling Max, the results might be through such a type and thus loose precision. Aug 25, 2018 at 18:40
  • @LasseVågsætherKarlsen So this means the problem (code above) is not even necesserily reproducable? Hmmm... Any ideas on what optimization will work as expected?
    – skrat
    Aug 25, 2018 at 18:44
  • 2
    For some reason, FreePascal is choosing Max(single,single) instead of Max(double,double). This doesn't make sense to me, but that's what it's doing. You can force it to use Max(double,double) by casting zero to double: Max(double(0),fValue). Or by using floating point value of zero instead of integer zero: Max(0.0,fValue); Aug 25, 2018 at 20:32
  • 1
    @Lasse No. If it uses a higher precision register then precision cannot be lost. Aug 25, 2018 at 21:27
  • Yeah, my comment was more about it using local variable placed into the floating point cpu registers, which has a higher precision than the types still. Meaning that any transition between the cpu(fpu) and memory is prone to loosing precision. But that wasn't the problem here so never mind me. Aug 26, 2018 at 10:14

1 Answer 1

10

I should state upfront that the last time I used Pascal was close to 25 years ago. But I pulled down Free Pascal out of curiosity and tried this:

program Project1;

uses
SysUtils, Math;

var
  fValue: double;
  fValueMax: double;

  fSingle: single;

  fValue2: double;
  fValue2b: double;
  fValueMax2: double;

begin
  fValue := 7.0207503445953527; 
  fSingle := 7.0207503445953527;
  fValueMax := Max(0, fValue);

  writeln(fValue);       // prints 7.0207503445953527E+000
  writeln(fValueMax);    // prints 7.0207505226135254E+000

  writeln(fSingle);      // prints 7.020750523E+00

  fValue2 := 7.0207503445953527;
  fValue2b := 0.0;
  fValueMax2 := Max(fValue2b, fValue2);

  writeln(fValue2);      // prints 7.0207503445953527E+000
  writeln(fValueMax2);   // prints 7.0207503445953527E+000
  readln;
end.

My first two writeln commands show the same result that you reported seeing. I suspected that perhaps Max was returning a value with less precision that the double you expected to get back, so I created fSingle and assigned it the same literal as you assigned to fValue, and sure enough, its value looks very close to what you're getting back in fValueMax.

So finally, instead of invoking Max with fValue and the literal 0, I called it with two variables of type double, one of which I had set to 0.0. In this case you can see that the input (fValue2) and the output (fValueMax2) have exactly the same value. So while I don't know exactly what Pascal's rules are for determining which overload to call, I wonder if your original call to Max was somehow resolving to the version that takes two single values and returns the same.

While you may be aware of this, I feel compelled to throw in the usual caution about how floating-point types like single and double won't always be able to exactly represent the values you want them to. Here's a good overview.

2
  • 2
    Looks like a bug in FreePascal indeed (it seems to choose the single precision overload, even though the second parameter is a double). It does choose the correct overload when 0.0 is specified instead of 0 for the first parameter. Aug 26, 2018 at 10:48
  • 3
    FWIW: kudos. From someone who hasn't touched Pascal for 25 years, your code still looks good. <g> Aug 26, 2018 at 10:52

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.