Unit 'System' Package
[Overview][Constants][Types][Classes][Procedures and functions][Variables][Index] [#rtl]

Trunc

Truncate a floating point value.

Declaration

Source position: mathh.inc line 134

function Trunc(

  d: ValReal

):Int64;

Description

Trunc returns the integer part of X, which is always smaller than (or equal to) X in absolute value.

Errors

An invalid floating point operation error may occur in one of several conditions:

See also

Frac

  

Return fractional part of floating point value.

Int

  

Calculate integer part of floating point value.

Round

  

Round floating point value to nearest integer number.

Example

Program Example70;

{ Program to demonstrate the Trunc function. }

begin
  Writeln (Trunc(123.456));  { Prints 123  }
  Writeln (Trunc(-123.456)); { Prints -123 }
  Writeln (Trunc(12.3456));  { Prints 12   }
  Writeln (Trunc(-12.3456)); { Prints -12  }
end.

Example

program ex124;

{
  Example to show various error conditions for the Trunc() function.
  SysUtils is used to convert the runtime error to an exception that can be caught
  Math is used to have access to Nan and (Neg)Infinity
 
}

{$mode objfpc}
{$h+}
uses

  sysutils, math;
var
  D: Double;
  Q: QWord;
  L: Int64;
begin
  D := 2.0 * High(QWord);
  try
    Q := Trunc(D);
  except
    on E: Exception do writeln(E.ClassName,': ',E.Message);  //EInvalidOp: Invalid floating point operation
  end;
  D := NaN;
  try
    Q := Trunc(D);
  except
    on E: Exception do writeln(E.ClassName,': ',E.Message);  //EInvalidOp: Invalid floating point operation
  end;
  D := Infinity;
  try
    Q := Trunc(D);
  except
    on E: Exception do writeln(E.ClassName,': ',E.Message);  //EInvalidOp: Invalid floating point operation
  end;
  D := NegInfinity;
  try
    Q := Trunc(D);
  except
    on E: Exception do writeln(E.ClassName,': ',E.Message);  //EInvalidOp: Invalid floating point operation
  end;
  D := -2.0 * High(QWord);
  try
    L := Trunc(D);
  except
    on E: Exception do writeln(E.ClassName,': ',E.Message);  //EInvalidOp: Invalid floating point operation
  end;
end.

Documentation generated on: Oct 27 2024