Next: Conversion of Scalar Types Up: Types of Numeric Data Previous: Numeric Operators   Contents

## Functions for Numeric Data

A few basic mathematical functions are part of python's core; many more can be found in the math module.

The abs function returns the absolute value of its argument, that is, the value itself if it is greater than or equal to 0, and the negative of the value otherwise. The round function performs rounding on its argument. Unlike integer arithmetic, which simply drops any fractional portion of a number, round returns a floating point representation of the nearest integer of its argument, truncating when the fraction is less than 0.5, and advancing to the next integer when the fraction is greater than 0.5.

The divmod function accepts two numeric arguments, and returns a tuple (Section 4.5) containing two floating point values. The first argument is divided by the second argument, and the function returns (in the first element of the tuple) the number of times the second element goes into the first and (in the second element of the tuple) the remainder. The divmod function accepts either integer or floating point arguments:

```>>> divmod(21,4)
(5, 1)
>>> divmod(25.8,.7)
(36.0, 0.6)
```
Note that, in the first example, 21 = 5 * 4 + 1, and in the second example 25.8 = 36 * 0.7 + 0.6. The modulus function found in many other languages returns the second element of the tuple returned by divmod.

The pow function is an alternative to the exponentiation operator (**). It accepts two arguments; the first is the numerical expression to be raised to a power, and the second is the power to which the number should be raised. Provided with two integer arguments, pow returns an integer; if either of the arguments is a floating point number, it will return a floating point number. Similarly, if a long integer is provided as the first argument, it will return a long integer, unless the second argument is a floating point number.

Related modules: array, cmath, math, random

Related exceptions: ZeroDivisionError, OverflowError, FloatingPointError

Next: Conversion of Scalar Types Up: Types of Numeric Data Previous: Numeric Operators   Contents
Phil Spector 2003-11-12