Function trunc ()

See the following code, why is the result of the Trunc function different?

procedure TForm1.Button1Click(Sender: TObject);
var
  D: Double;
  E: Extended;
  I: Int64;
begin
  D := Frac(101 / 100) * 100;
  E := Frac(101 / 100) * 100;
  I := Trunc(D);
  ShowMessage('Trunc(Double): ' + IntToStr(I));  // Trunc(Double): 1
  I := Trunc(E);
  ShowMessage('Trunc(Extended): ' + IntToStr(I)); // Trunc(Extended): 0
end;
+5
source share
2 answers

Formatting functions do not always display actual numbers (data).
 Real numbers and accuracy can be complex.

Take a look at this code, where I use more information about what I want to see on the screen:

  D := Frac(101 / 100);
  E := Frac(101 / 100);
  ShowMessage(FloatToStrF(D, ffFixed, 15, 20));
  ShowMessage(FloatToStrF(E, ffFixed, 18, 20));

It seems that D- this is something like 0.010000000000, but E- how 0.00999999999.

Edit: a Extended type has better precision than a type Double. If we try to display the values ​​of D and E using FloatToString (), we are likely to get the same result, although the actual values ​​do not match.

+9

. , ,

, D - - 0.010000000000, E - 0,00999999999.

, , . . ( 0 1 , ), Delphi ( ) ( ).

: Fixed-point

+6

All Articles