psx.wiki

Math Library (libmath)

Provides a floating-point operation package and the standard C math functions.

Math Library (libmath)
Librarylibmath.lib
Header Filelibmath.h, limits.h

This is an old revision of this page, as edited by RemmyLee at 02:58, 23 October 2026. Edit this revision | Latest revision

Overview

The Math Library (libmath) provides a floating-point operation package and the K & R based math functions of the standard C library. Its functions comply with ANSI and IEEE754.12

A program that uses libmath links with libmath.lib and includes libmath.h and limits.h.2

Floating-Point Numbers

The PlayStation hardware does not support float and double operations directly, because the CPU has no floating-point coprocessor. libmath contains an internal floating-point arithmetic package, so a program linked with it can use the float and double types. It supports IEEE754 single-precision (float) and double-precision (double) numbers:2

Table 1: Floating-point formats
Item Float Double
Size 4 bytes 8 bytes
Significant digits 6 decimal digits 15 decimal digits
Overflow limit value 2.0**128 = 3.4e38 2.0**1024 = 1.8e308
Underflow limit value 0.5**126 = 2.2e-38 0.5**1022 = 2.2e-308

Error Processing

Errors in floating-point operations are reported by events, and error status is also recorded in an external variable in the style of standard C. Functions whose specifications cover a range of arguments test the range. An inappropriate argument gives an area error (EDOM), and a result beyond the range that can be expressed gives a range error (ERANGE).2

For either error the library sets an error code in an event and in the external variable. The result of the operation is an infinite value, so the calculation can continue where possible. Division by zero returns NaN or -NaN, which is a bit pattern reserved by the operation subroutine to report an error. A normal variable never holds this pattern, and further operations on NaN do not give correct results. The bit patterns are the following:3

Table 2: Infinite and NaN bit patterns
Value Float Double
Positive infinity 0x7F800000 0x7FF0000000000000
Negative infinity 0xFF800000 0xFFF0000000000000
NaN 0x7FFFFFFF 0x7FFFFFFFFFFFFFFF
-NaN 0xFFFFFFFF 0xFFFFFFFFFFFFFFFF

An error in a libmath function causes an event with the cause descriptor SwMATH, so overflow and division by zero can be detected. The variable math_errno, defined in libmath.lib and declared in libmath.h, starts at zero and receives EDOM or ERANGE from sys/errno.h on an error. It is not reset automatically, and the program clears it after processing the error.3

Table 3: Error notification
Error math_errno Cause descriptor Event type
Area error EDOM SwMATH EvSpEDOM
Range error ERANGE SwMATH EvSpERANGE

Functions

In the tables below, Introduced is the run-time library version that added the item.4

libmath has the following functions:4

Table 4: libmath functions
Function Introduced Purpose
acos 3.0 Arccosine.
asin 3.0 Arcsine.
atan 3.0 Arctangent.
atan2 3.0 Arctangent.
atof 3.0 Convert a string to a floating-point equivalent.
ceil 3.0 Minimum integer not less than x (ceiling function).
cos 3.0 Cosine.
cosh 3.0 Hyperbolic cosine.
exp 3.0 Exponent.
fabs 3.0 Absolute value (macro).
floor 3.0 Maximum integer not more than x (lower function).
fmod 3.0 Floating-point remainder resulting from x/y.
frexp 3.0 Resolve into a normalized fraction and a power of 2.
hypot 3.0 Absolute value of a complex number.
ldexp 3.0 Calculate a real number from a mantissa and an exponent.
log 3.0 Natural logarithm.
log10 3.0 Base 10 logarithm.
modf 3.0 Separate a double into integral and fractional parts.
pow 3.0 Raise a double to a power.
printf2 3.0 Convert formatted output as standard output (with floating-point and double-precision arguments).
sin 3.0 Sine.
sinh 3.0 Hyperbolic sine.
sprintf2 3.0 Format output to a string (with floating-point and double-precision arguments).
strtod 3.0 Convert a string to a floating-point equivalent.
tan 3.0 Tangent.
tanh 3.0 Hyperbolic tangent.

References


  1. ^ Sony Computer Entertainment Inc. Run-Time Library Overview. Run-Time Library 4.4. January 1999. Page x.

  2. ^ a b c d Sony Computer Entertainment Inc. Run-Time Library Overview. Run-Time Library 4.4. January 1999. Page 4-3.

  3. ^ a b Sony Computer Entertainment Inc. Run-Time Library Overview. Run-Time Library 4.4. January 1999. Page 4-4.

  4. ^ a b Sony Computer Entertainment Inc. Run-Time Library Reference. Run-Time Library 4.4. January 1999. Pages 3-3 to 3-29.

Categories: Libraries