ZetCode

Pascal numeric functions

last modified November 18, 2026 Free Pascal 3.2.2

Pascal provides a small set of numeric functions in the System unit and a much larger set in the Math unit. The System functions cover absolute values, squares, square roots, rounding, and parity; the Math unit adds powers, logarithms, trigonometry, and minimum and maximum. This article goes through the functions a numerical program uses most and points out where Pascal differs from C or Python.

Roots and powers

Abs, Sqr, and Sqrt are part of the System unit and need no uses clause. Power, Exp, Ln, Log10, and Log2 come from Math. Power returns a real even when both arguments are integers, and the logarithms are natural unless the name says otherwise.

roots.pas
program roots;
{$mode objfpc}{$H+}

uses Math;

begin
  WriteLn('Abs(-7)      = ', Abs(-7));
  WriteLn('Sqr(5)       = ', Sqr(5));
  WriteLn('Sqrt(2)      = ', Sqrt(2):0:6);
  WriteLn('Power(2, 10) = ', Power(2, 10):0:0);
  WriteLn('Exp(1)       = ', Exp(1):0:6);
  WriteLn('Ln(Exp(1))   = ', Ln(Exp(1)):0:6);
  WriteLn('Log10(1000)  = ', Log10(1000):0:0);
  WriteLn('Log2(8)      = ', Log2(8):0:0);
end.

Abs removes the sign, Sqr multiplies a value by itself, and Sqrt returns the square root. Power(2, 10) is 1024, and Exp(1) is e, the base of the natural logarithm. Ln is the inverse of Exp, Log10 is the base-10 logarithm, and Log2 is the base-2 logarithm. The result of Power is a real, so it is formatted with :0:0 to hide the decimal part.

$ fpc -gl roots.pas
$ ./roots
Abs(-7)      = 7
Sqr(5)       = 25
Sqrt(2)      = 1.414214
Power(2, 10) = 1024
Exp(1)       = 2.718282
Ln(Exp(1))   = 1.000000
Log10(1000)  = 3
Log2(8)      = 3

Trigonometry

The trigonometric functions work in radians, not degrees. Sin, Cos, and Tan take an angle and return the ratio; ArcTan, ArcSin, and ArcCos go the other way and return an angle in radians. DegToRad and RadToDeg convert between the two units, and Hypot returns the length of the hypotenuse of a right triangle.

trig.pas
program trig;
{$mode objfpc}{$H+}

uses Math;

var
  angle: Double;

begin
  angle := Pi / 6;
  WriteLn('Sin(pi/6) = ', Sin(angle):0:4);
  WriteLn('Cos(pi/6) = ', Cos(angle):0:4);
  WriteLn('Tan(pi/4) = ', Tan(Pi / 4):0:4);
  WriteLn('ArcTan(1) = ', ArcTan(1):0:4);
  WriteLn('ArcSin(1) = ', ArcSin(1):0:4);
  WriteLn('DegToRad(180) = ', DegToRad(180):0:4);
  WriteLn('RadToDeg(Pi)  = ', RadToDeg(Pi):0:1);
  WriteLn('Hypot(3, 4)   = ', Hypot(3, 4):0:1);
end.

Pi is a constant from Math. The sine of pi/6 is 0.5 and the cosine is about 0.8660. ArcTan(1) returns pi/4, and ArcSin(1) returns pi/2. The conversion functions confirm that 180 degrees is pi radians. Hypot(3, 4) returns 5, the length of the 3-4-5 triangle.

$ fpc -gl trig.pas
$ ./trig
Sin(pi/6) = 0.5000
Cos(pi/6) = 0.8660
Tan(pi/4) = 1.0000
ArcTan(1) = 0.7854
ArcSin(1) = 1.5708
DegToRad(180) = 3.1416
RadToDeg(Pi)  = 180.0
Hypot(3, 4)   = 5.0

Rounding and truncation

Pascal distinguishes four operations that turn a real into a whole number. Round returns the nearest integer, Trunc drops the fractional part toward zero, Int returns the integer part as a real and also truncates toward zero, and Frac returns the signed fractional part. Ceil and Floor return integers and always move toward positive or negative infinity.

rounding.pas
program rounding;
{$mode objfpc}{$H+}

uses Math;

begin
  WriteLn('Round(2.5)  = ', Round(2.5));
  WriteLn('Round(3.5)  = ', Round(3.5));
  WriteLn('Round(-2.5) = ', Round(-2.5));
  WriteLn('Trunc(2.9)  = ', Trunc(2.9));
  WriteLn('Trunc(-2.9) = ', Trunc(-2.9));
  WriteLn('Int(2.9)    = ', Int(2.9):0:1);
  WriteLn('Frac(2.9)   = ', Frac(2.9):0:1);
  WriteLn('Ceil(2.1)   = ', Ceil(2.1));
  WriteLn('Floor(2.9)  = ', Floor(2.9));
  WriteLn('Ceil(-2.7)  = ', Ceil(-2.7));
  WriteLn('Floor(-2.7) = ', Floor(-2.7));
end.

Round uses banker's rounding, also called round-half-to-even: 2.5 becomes 2 and 3.5 becomes 4, so the result is the nearest even integer when the value is exactly halfway. This is the default of the floating-point unit, not a Pascal-specific choice. Trunc(2.9) is 2 and Trunc(-2.9) is -2, because truncation moves toward zero. Int(2.9) is 2.0, Frac(2.9) is 0.9, and the negative examples show the difference between Ceil and Floor.

$ fpc -gl rounding.pas
$ ./rounding
Round(2.5)  = 2
Round(3.5)  = 4
Round(-2.5) = -2
Trunc(2.9)  = 2
Trunc(-2.9) = -2
Int(2.9)    = 2.0
Frac(2.9)   = 0.9
Ceil(2.1)   = 3
Floor(2.9)  = 2
Ceil(-2.7)  = -2
Floor(-2.7) = -3

Minimum, maximum, and sign

Min and Max return the smaller and larger of two values. They are overloaded for integers and reals, so the result has the type of the arguments. Sign returns -1, 0, or 1 according to the sign of its argument, and Odd tests whether an integer is odd.

minmax2.pas
program minmax2;
{$mode objfpc}{$H+}

uses Math;

begin
  WriteLn('Min(3, 7)     = ', Min(3, 7));
  WriteLn('Max(3, 7)     = ', Max(3, 7));
  WriteLn('Min(3.5, 2.5) = ', Min(3.5, 2.5):0:1);
  WriteLn('Max(3.5, 2.5) = ', Max(3.5, 2.5):0:1);
  WriteLn('Sign(-5)      = ', Sign(-5));
  WriteLn('Sign(0)       = ', Sign(0));
  WriteLn('Sign(5)       = ', Sign(5));
  WriteLn('Odd(7)        = ', Odd(7));
  WriteLn('Odd(8)        = ', Odd(8));
end.

The first four lines show that Min and Max work for both integer and real arguments. Sign is useful when only the direction of a difference matters, and Odd avoids a manual mod 2 test. Inc and Dec, which also belong to the numeric toolbox, were covered with the operators.

$ fpc -gl minmax2.pas
$ ./minmax2
Min(3, 7)     = 3
Max(3, 7)     = 7
Min(3.5, 2.5) = 2.5
Max(3.5, 2.5) = 3.5
Sign(-5)      = -1
Sign(0)       = 0
Sign(5)       = 1
Odd(7)        = TRUE
Odd(8)        = FALSE

Angles and degrees

A program that reads angles in degrees should convert them before calling the trigonometric functions. DegToRad multiplies by pi and divides by 180; RadToDeg does the opposite. The two functions are cheap and make the intent clear, so there is no reason to repeat the constant in every call.

The inverse functions have restricted domains. ArcSin and ArcCos accept values from -1 to 1, and ArcTan accepts any real. Passing a value outside the domain may raise a floating-point exception or return NaN, depending on the FPU exception mask. A later article explains the mask.

Next steps

The procedures article shows how to package a calculation in a function. Later articles in the series apply these functions to whole arrays, add the functions that produce values for simulations, and explain the precision behind the results.

Source

Free Pascal Math unit

Free Pascal Reference guide

Pascal on Wikipedia

In this article we have used the Pascal roots, powers, logarithms, trigonometric functions, rounding and truncation routines, minimum and maximum, sign, and parity tests. The series continues with Pascal procedures and functions.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Pascal tutorials.