natu.mathΒΆ

Python math, adapted for use with physical quantities

Many of the functions only accept dimensionless quantities, and they operate on the underlying values—not the values as represented in a particular display unit. To operate on values in a display unit would be to favor a particular unit, which is against the design of the package.

The constants (pi, e) are exactly as they are in Python math.

These functions accept floats, integers, and dimensionless quantities:

  • Number-theoretic and representation functions: factorial(), frexp(), modf(), and trunc()
  • Power and logarithmic functions: exp(), expm1(), log10(), log1p(), and log()
  • Hyperbolic functions: acosh(), asinh(), atanh(), cosh(), sinh(), and tanh()
  • Special functions: erf(), erfc(), gamma(), and lgamma()

They are the same as those from Python math, except the documentation of acosh(), asinh(), and atanh() has been corrected (see http://bugs.python.org/issue21902). The functions cast quantities cast as floats and return floats, except for factorial(), which casts quantities as integers and returns integers.

All of the other functions (below) are different than those in Python math.

These functions accept angle as a quantity:

  • cos(), sin(), and tan()

They return floats (as in Python math).

These functions accept floats, integers, and dimensionless quantities:

  • acos(), asin(), atan(), and atan2()

They return angles as quantities.

atan2() accepts accept floats, integers, and quantities of the same dimension. It returns angle as a quantity.

These functions are no longer applicable and have been deleted since angle is a quantity:

  • degrees() and radians()

These functions accept floats, integers, and dimensionless quantities:

  • ceil() and floor()

If the input is a float or an integer, the output is a float. If the input is a Quantity or a ScalarUnit, the result is the same and has the same dimension, display, etc.

These functions accept floats, integers, and quantities:

  • fabs() and copysign()

If the input is a float or an integer, the output is a float. If the input is a Quantity or a ScalarUnit, the result is of the same type and has the same dimension, display, etc. (of the first argument in the case of copysign()).

These functions also accept floats, integers, and quantities:

  • ldexp(), pow(), and sqrt()

If the input is an float or an integer, the output is a float. If the input is a Quantity or a ScalarUnit, the result is the same. The dimensions and display units are handled according to the power.

fmod() accepts floats, integers, and quantities of the same dimension. The output is always a float.

These functions also accept floats, integers, and quantities of the same dimension:

  • fsum() and hypot()

The display unit (and prefixable attribute, if applicable) of the first argument or entry is propagated to the output.

These functions accept floats or quantities:

  • isinf(), isfinite() (only available in Python >= 3.2), and isnan()

Only the value of a quantity is used; dimension and display unit are ignored.