[go: up one dir, main page]

0% found this document useful (0 votes)
9 views2 pages

Public Documents 2

The document provides an overview of various mathematical functions in Python's math module, including math.ceil, math.fabs, math.floor, math.fma, math.fmod, math.modf, math.remainder, and math.trunc. Each function is described with its purpose, behavior, and any special considerations, particularly regarding floating-point arithmetic and adherence to the IEEE 754 standard. The document emphasizes the differences between certain functions and their Python equivalents, particularly in handling floating-point numbers.

Uploaded by

tamoyskitv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Public Documents 2

The document provides an overview of various mathematical functions in Python's math module, including math.ceil, math.fabs, math.floor, math.fma, math.fmod, math.modf, math.remainder, and math.trunc. Each function is described with its purpose, behavior, and any special considerations, particularly regarding floating-point arithmetic and adherence to the IEEE 754 standard. The document emphasizes the differences between certain functions and their Python equivalents, particularly in handling floating-point numbers.

Uploaded by

tamoyskitv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Floating point arithmetic

math.ceil(x)

Return the ceiling of x, the smallest integer greater than or equal to x.


If x is not a float, delegates to x.__ceil__, which should return
an Integral value.
math.fabs(x)

Return the absolute value of x.


math.floor(x)

Return the floor of x, the largest integer less than or equal to x. If x is


not a float, delegates to x.__floor__, which should return
an Integral value.
math.fma(x, y, z)

Fused multiply-add operation. Return (x * y) + z, computed as


though with infinite precision and range followed by a single round to
the float format. This operation often provides better accuracy than
the direct expression (x * y) + z.

This function follows the specification of the fusedMultiplyAdd


operation described in the IEEE 754 standard. The standard leaves one
case implementation-defined, namely the result
of fma(0, inf, nan) and fma(inf, 0, nan). In these
cases, math.fma returns a NaN, and does not raise any exception.

Added in version 3.13.

math.fmod(x, y)

Return the floating-point remainder of x / y, as defined by the


platform C library function fmod(x, y). Note that the Python
expression x % y may not return the same result. The intent of the C
standard is that fmod(x, y) be exactly (mathematically; to infinite
precision) equal to x - n*y for some integer n such that the result has
the same sign as x and magnitude less than abs(y).
Python’s x % y returns a result with the sign of y instead, and may not
be exactly computable for float arguments. For example, fmod(-1e-
100, 1e100) is -1e-100, but the result of Python’s -1e-
100 % 1e100 is 1e100-1e-100, which cannot be represented exactly as
a float, and rounds to the surprising 1e100. For this reason,
function fmod() is generally preferred when working with floats, while
Python’s x % y is preferred when working with integers.
math.modf(x)
Return the fractional and integer parts of x. Both results carry the sign
of x and are floats.

Note that modf() has a different call/return pattern than its C


equivalents: it takes a single argument and return a pair of values,
rather than returning its second return value through an ‘output
parameter’ (there is no such thing in Python).
math.remainder(x, y)

Return the IEEE 754-style remainder of x with respect to y. For


finite x and finite nonzero y, this is the difference x - n*y, where n is
the closest integer to the exact value of the quotient x / y. If x / y is
exactly halfway between two consecutive integers, the
nearest even integer is used for n. The
remainder r = remainder(x, y) thus always
satisfies abs(r) <= 0.5 * abs(y).

Special cases follow IEEE 754: in


particular, remainder(x, math.inf) is x for any finite x,
and remainder(x, 0) and remainder(math.inf, x) raise ValueError f
or any non-NaN x. If the result of the remainder operation is zero, that
zero will have the same sign as x.

On platforms using IEEE 754 binary floating point, the result of this
operation is always exactly representable: no rounding error is
introduced.

Added in version 3.7.

math.trunc(x)

Return x with the fractional part removed, leaving the integer part.
This rounds toward 0: trunc() is equivalent to floor() for positive x,
and equivalent to ceil() for negative x. If x is not a float, delegates
to x.__trunc__, which should return an Integral value.

For the ceil(), floor(), and modf() functions,


note that all floating-point numbers of
sufficiently large magnitude are exact integers.
Python floats typically carry no more than 53
bits of precision (the same as the platform C
double type), in which case any
float x with abs(x) >= 2**52 necessarily has
no fractional bits.

You might also like