{ Program Title: Free Pascal Math Functions Demonstration }
program MathExample;
uses
Math, SysUtils; { Math = math functions, SysUtils = rounding helpers }
var
piValue, eValue: Double;
x, angle, y: Double;
aFloat, bFloat, value, clamped: Double; { renamed to avoid conflict }
AByte, BByte: Byte; { renamed to avoid conflict }
begin
{ ============================
Constants
============================ }
piValue := Pi; { π constant }
eValue := Exp(1); { Euler's number }
Writeln('Constants:');
Writeln(' pi = ', piValue);
Writeln(' e = ', eValue);
Writeln;
{ ============================
Core Functions
============================ }
x := 2.5; { sample value }
Writeln('Core Functions:');
Writeln(' Sqrt(2.5) = ', Sqrt(x)); { square root }
Writeln(' Exp(2.5) = ', Exp(x)); { e^x }
Writeln(' Ln(2.5) = ', Ln(x)); { natural log }
Writeln(' Log10(2.5)= ', Log10(x)); { base-10 log }
Writeln(' Power(2.5,3)= ', Power(x,3)); { x^3 }
Writeln;
{ ============================
Trigonometric Functions
============================ }
angle := Pi / 4; { 45 degrees in radians }
Writeln('Trigonometric Functions:');
Writeln(' Sin(pi/4) = ', Sin(angle)); { sine }
Writeln(' Cos(pi/4) = ', Cos(angle)); { cosine }
Writeln(' Tan(pi/4) = ', Tan(angle)); { tangent }
Writeln(' ArcSin(0.5) = ', ArcSin(0.5)); { arcsine }
Writeln(' ArcCos(0.5) = ', ArcCos(0.5)); { arccosine }
Writeln(' ArcTan(1.0) = ', ArcTan(1.0)); { arctangent }
Writeln;
{ ============================
Hyperbolic Functions
============================ }
Writeln('Hyperbolic Functions:');
Writeln(' Sinh(1) = ', Sinh(1)); { hyperbolic sine }
Writeln(' Cosh(1) = ', Cosh(1)); { hyperbolic cosine }
Writeln(' Tanh(1) = ', Tanh(1)); { hyperbolic tangent }
Writeln;
{ ============================
Rounding Functions
============================ }
y := -2.7; { negative number for rounding tests }
Writeln('Rounding Functions:');
Writeln(' Floor(-2.7) = ', Floor(y)); { round down }
Writeln(' Ceil(-2.7) = ', Ceil(y)); { round up }
Writeln(' Round(-2.7) = ', Round(y)); { nearest integer }
Writeln(' Trunc(-2.7) = ', Trunc(y)); { remove decimals }
Writeln;
{ ============================
Min / Max / Clamp
============================ }
aFloat := 10;
bFloat := 20;
value := 15;
Writeln('Min/Max/Clamp:');
Writeln(' Min(10,20) = ', Min(aFloat,bFloat)); { smaller of two }
Writeln(' Max(10,20) = ', Max(aFloat,bFloat)); { larger of two }
clamped := EnsureRange(value, 0, 10); { clamp 15 to range 0–10 }
Writeln(' Clamp(15,0,10) = ', clamped);
Writeln;
{ ============================
Bitwise Math (Integers)
============================ }
AByte := $AA; { 10101010 in hex }
BByte := $CC; { 11001100 in hex }
Writeln('Bitwise Math:');
Writeln(' A AND B = $', HexStr(AByte and BByte, 2)); { bitwise AND }
Writeln(' A OR B = $', HexStr(AByte or BByte, 2)); { bitwise OR }
Writeln(' A XOR B = $', HexStr(AByte xor BByte, 2)); { bitwise XOR }
Writeln(' NOT A = $', HexStr(not AByte, 2)); { bitwise NOT }
Writeln(' A << 2 = $', HexStr(AByte shl 2, 2)); { left shift }
Writeln(' B >> 3 = $', HexStr(BByte shr 3, 2)); { right shift }
Writeln;
{ ============================
Additional Useful Math
============================ }
Writeln('Additional Math:');
Writeln(' Abs(-3.14) = ', Abs(-3.14)); { absolute value }
Writeln(' Frac(3.14) = ', Frac(3.14)); { fractional part }
Writeln(' Int(3.14) = ', Int(3.14)); { integer part }
Writeln(' Hypot(3,4) = ', Hypot(3,4)); { sqrt(x²+y²) }
Writeln(' DegToRad(180) = ', DegToRad(180)); { convert degrees to radians }
Writeln(' RadToDeg(pi) = ', RadToDeg(Pi)); { convert radians to degrees }
end.
{
run:
Constants:
pi = 3.1415926535897931E+000
e = 2.7182818284590451E+000
Core Functions:
Sqrt(2.5) = 1.5811388300841898E+000
Exp(2.5) = 1.21824939607034734377E+0001
Ln(2.5) = 9.16290731874155065179E-0001
Log10(2.5)= 3.97940008672037609576E-0001
Power(2.5,3)= 1.56250000000000000000E+0001
Trigonometric Functions:
Sin(pi/4) = 7.07106781186547502752E-0001
Cos(pi/4) = 7.07106781186547546066E-0001
Tan(pi/4) = 9.99999999999999938743E-0001
ArcSin(0.5) = 5.23598775598298873067E-0001
ArcCos(0.5) = 1.04719755119659774613E+0000
ArcTan(1.0) = 7.85398163397448309628E-0001
Hyperbolic Functions:
Sinh(1) = 1.17520119364380145688E+0000
Cosh(1) = 1.54308063481524377855E+0000
Tanh(1) = 7.61594155955764888109E-0001
Rounding Functions:
Floor(-2.7) = -3
Ceil(-2.7) = -2
Round(-2.7) = -3
Trunc(-2.7) = -2
Min/Max/Clamp:
Min(10,20) = 1.0000000000000000E+001
Max(10,20) = 2.0000000000000000E+001
Clamp(15,0,10) = 1.0000000000000000E+001
Bitwise Math:
A AND B = $88
A OR B = $EE
A XOR B = $66
NOT A = $55
A << 2 = $A8
B >> 3 = $19
Additional Math:
Abs(-3.14) = 3.14000000000000000010E+0000
Frac(3.14) = 1.40000000000000000095E-0001
Int(3.14) = 3.00000000000000000000E+0000
Hypot(3,4) = 5.00000000000000000000E+0000
DegToRad(180) = 3.14159265358979323851E+0000
RadToDeg(pi) = 1.80000000000000000000E+0002
}