Math

Usage

use Math;

or

import Math;

This module provides less frequently used mathematical constants and functions.

Note

Automatically-included Math symbols can be found here. These symbols can also be accessed using Math. as their qualified access prefix.

It includes wrappers for many of the constants and functions in the C Math library, which is part of the C Language Standard (ISO/IEC 9899) as described in Section 7.12. Please consult that standard for an authoritative description of the expected properties of those constants and routines.

In general, where the C math library provides a double and a float version of a function, the float version has a suffix ‘f’. In the Chapel interface, the suffix is dropped, and the type of the operand determines which version is called – according to the usual function overloading and resolution rules. Normally, the result has the same precision as the argument(s). Please consult the C standard for specifics.

Error Handling – At present, Chapel does not provide control over error handling in the Math module. The default behavior is as if the macro math_errhandling is set to 0: Given erroneous input at run-time, all math functions will return an implementation-defined value; no exception will be generated.

Trigonometric Functions

acos acosh asin asinh atan atan2 atanh cos cosh sin sinh tan tanh

Exponential Functions

exp exp2 expm1 ldExp

Gamma Functions

gamma lnGamma

Error Functions

erf erfc

Algorithms

gcd

Bessel Functions

j0 j1 jn y0 y1 yn

Constant and Function Definitions

param e = 2.7182818284590452354

e - exp(1) or the base of the natural logarithm

param log2E = 1.4426950408889634074

log2(e)

param log2_e = 1.4426950408889634074

log2(e)

Warning

‘log2_e’ is deprecated, please use log2E instead

param log10E = 0.43429448190325182765

log10(e)

param log10_e = 0.43429448190325182765

log10(e)

Warning

‘log10_e’ is deprecated, please use log10E instead

param ln2 = 0.69314718055994530942

ln(2) (natural logarithm)

param ln_2 = 0.69314718055994530942

log(2) (natural logarithm)

Warning

‘ln_2’ is deprecated, please use ln2 instead

param ln10 = 2.30258509299404568402

ln(10) (natural logarithm)

param ln_10 = 2.30258509299404568402

log(10) (natural logarithm)

Warning

‘ln_10’ is deprecated, please use ln10 instead

param pi = 3.14159265358979323846

pi - the circumference/the diameter of a circle

param halfPi = 1.57079632679489661923

pi/2

Warning

‘halfPi’ is unstable due to questions about its utility. If you are seeing negative performance impacts from using ‘pi/2’ instead of this ‘param’, please let us know!

param half_pi = 1.57079632679489661923

pi/2

Warning

‘half_pi’ is deprecated, please use halfPi or ‘pi/2’ instead

param quarterPi = 0.78539816339744830962

pi/4

Warning

‘quarterPi’ is unstable due to questions about its utility. If you are seeing negative performance impacts from using ‘pi/4’ instead of this ‘param’, please let us know!

param quarter_pi = 0.78539816339744830962

pi/4

Warning

‘quarter_pi’ is deprecated, please use quarterPi or ‘pi/4’ instead

param reciprPi = 0.31830988618379067154

1/pi

Warning

‘reciprPi’ is unstable due to questions about its utility. If you are seeing negative performance impacts from using ‘1/pi’ instead of this ‘param’, please let us know!

param recipr_pi = 0.31830988618379067154

1/pi

Warning

‘recipr_pi’ is deprecated, please use reciprPi or ‘1/pi’ instead

param twiceReciprPi = 0.63661977236758134308

2/pi

Warning

‘twiceReciprPi’ is unstable due to questions about its utility. If you are seeing negative performance impacts from using ‘2/pi’ instead of this ‘param’, please let us know!

param twice_recipr_pi = 0.63661977236758134308

2/pi

Warning

‘twice_recipr_pi’ is deprecated, please use twiceReciprPi or ‘2/pi’ instead

param twiceReciprSqrtPi = 1.12837916709551257390

2/sqrt(pi)

Warning

‘twiceReciprSqrtPi’ is unstable due to questions about its utility. If you are using this symbol, please let us know!

param twice_recipr_sqrt_pi = 1.12837916709551257390

2/sqrt(pi)

Warning

‘twice_recipr_sqrt_pi’ is deprecated, please use twiceReciprSqrtPi or ‘2/sqrt(pi)’ instead

param sqrt2 = 1.41421356237309504880

sqrt(2)

Warning

‘sqrt2’ is unstable due to questions about its utility. If you are using this symbol, please let us know!

param sqrt_2 = 1.41421356237309504880

sqrt(2)

Warning

‘sqrt_2’ is deprecated, please use sqrt2 or ‘sqrt(2)’ instead

param reciprSqrt2 = 0.70710678118654752440

1/sqrt(2)

Warning

‘reciprSqrt2’ is unstable due to questions about its utility. If you are using this symbol, please let us know!

param recipr_sqrt_2 = 0.70710678118654752440

1/sqrt(2)

Warning

‘recipr_sqrt_2’ is deprecated, please use reciprSqrt2 or ‘1/sqrt(2)’ instead

proc acos(x: real(64)): real(64)

Returns the arc cosine of the argument x.

It is an error if x is less than -1 or greater than 1.

proc acos(x: real(32)): real(32)

Returns the arc cosine of the argument x.

It is an error if x is less than -1 or greater than 1.

proc acos(x: complex(64)): complex(64)

Returns the arc cosine of the argument x.

proc acos(x: complex(128)): complex(128)

Returns the arc cosine of the argument x.

proc acosh(x: real(64)): real(64)

Returns the inverse hyperbolic cosine of the argument x.

It is an error if x is less than 1.

proc acosh(x: real(32)): real(32)

Returns the inverse hyperbolic cosine of the argument x.

It is an error if x is less than 1.

proc acosh(x: complex(64)): complex(64)

Returns the inverse hyperbolic cosine of the argument x.

proc acosh(x: complex(128)): complex(128)

Returns the inverse hyperbolic cosine of the argument x.

proc asin(x: real(64)): real(64)

Returns the arc sine of the argument x.

It is an error if x is less than -1 or greater than 1.

proc asin(x: real(32)): real(32)

Returns the arc sine of the argument x.

It is an error if x is less than -1 or greater than 1.

proc asin(x: complex(64)): complex(64)

Returns the arc sine of the argument x.

proc asin(x: complex(128)): complex(128)

Returns the arc sine of the argument x.

proc asinh(x: real(64)): real(64)

Returns the inverse hyperbolic sine of the argument x.

proc asinh(x: real(32)): real(32)

Returns the inverse hyperbolic sine of the argument x.

proc asinh(x: complex(64)): complex(64)

Returns the inverse hyperbolic sine of the argument x.

proc asinh(x: complex(128)): complex(128)

Returns the inverse hyperbolic sine of the argument x.

proc atan(x: real(64)): real(64)

Returns the arc tangent of the argument x.

proc atan(x: real(32)): real(32)

Returns the arc tangent of the argument x.

proc atan(x: complex(64)): complex(64)

Returns the arc tangent of the argument x.

proc atan(x: complex(128)): complex(128)

Returns the arc tangent of the argument x.

proc atan2(y: real(64), x: real(64)): real(64)

Returns the arc tangent of the ratio of the two arguments.

This is equivalent to the arc tangent of y / x except that the signs of y and x are used to determine the quadrant of the result.

proc atan2(y: real(32), x: real(32)): real(32)

Returns the arc tangent of the ratio of the two arguments.

This is equivalent to the arc tangent of y / x except that the signs of y and x are used to determine the quadrant of the result.

proc atanh(x: real(64)): real(64)

Returns the inverse hyperbolic tangent of the argument x.

It is an error if x is less than -1 or greater than 1.

proc atanh(x: real(32)): real(32)

Returns the inverse hyperbolic tangent of the argument x.

It is an error if x is less than -1 or greater than 1.

proc atanh(x: complex(64)): complex(64)

Returns the inverse hyperbolic tangent of the argument x.

proc atanh(x: complex(128)): complex(128)

Returns the inverse hyperbolic tangent of the argument x.

proc cos(x: real(64)): real(64)

Returns the cosine of the argument x.

proc cos(x: real(32)): real(32)

Returns the cosine of the argument x.

proc cos(x: complex(64)): complex(64)

Returns the cosine of the argument x.

proc cos(x: complex(128)): complex(128)

Returns the cosine of the argument x.

proc cosh(x: real(64)): real(64)

Returns the hyperbolic cosine of the argument x.

proc cosh(x: real(32)): real(32)

Returns the hyperbolic cosine of the argument x.

proc cosh(x: complex(64)): complex(64)

Returns the hyperbolic cosine of the argument x.

proc cosh(x: complex(128)): complex(128)

Returns the hyperbolic cosine of the argument x.

proc divCeil(param x: integral, param y: integral) param

Returns ceil(x/y), i.e., the fraction x/y rounded up to the nearest integer.

If the arguments are of unsigned type, then fewer conditionals will be evaluated at run time.

proc divCeil(x: integral, y: integral)

Returns ceil(x/y), i.e., the fraction x/y rounded up to the nearest integer.

If the arguments are of unsigned type, then fewer conditionals will be evaluated at run time.

proc divCeilPos(x: integral, y: integral)

A variant of divCeil that performs no runtime checks. The user must ensure that both arguments are strictly positive (not 0) and are of a signed integer type (not uint).

Warning

divCeilPos is unstable due to questions about its utility. If you find this function valuable, please let us know!

proc divFloor(param x: integral, param y: integral) param

Returns floor(x/y), i.e., the fraction x/y rounded down to the nearest integer.

If the arguments are of unsigned type, then fewer conditionals will be evaluated at run time.

proc divFloor(x: integral, y: integral)

Returns floor(x/y), i.e., the fraction x/y rounded down to the nearest integer.

If the arguments are of unsigned type, then fewer conditionals will be evaluated at run time.

proc divFloorPos(x: integral, y: integral)

A variant of divFloor that performs no runtime checks. The user must ensure that both arguments are strictly positive (not 0) and are of a signed integer type (not uint).

Warning

divFloorPos is unstable due to questions about its utility. If you find this function valuable, please let us know!

proc erf(x: real(64)): real(64)

Returns the error function of the argument x. This is equivalent to 2/sqrt(pi) * the integral of exp(-t**2)dt from 0 to x.

Warning

‘erf’ is unstable and may be renamed or moved to a different module in the future

proc erf(x: real(32)): real(32)

Returns the error function of the argument x. This is equivalent to 2/sqrt(pi) * the integral of exp(-t**2)dt from 0 to x.

Warning

‘erf’ is unstable and may be renamed or moved to a different module in the future

proc erfc(x: real(64)): real(64)

Returns the complementary error function of the argument x. This is equivalent to 1.0 - erf(x).

Warning

‘erfc’ is unstable and may be renamed or moved to a different module in the future

proc erfc(x: real(32)): real(32)

Returns the complementary error function of the argument x. This is equivalent to 1.0 - erf(x).

Warning

‘erfc’ is unstable and may be renamed or moved to a different module in the future

proc exp(x: real(64)): real(64)

Returns the value of the Napierian e raised to the power of the argument x.

proc exp(x: real(32)): real(32)

Returns the value of the Napierian e raised to the power of the argument x.

proc exp(x: complex(64)): complex(64)

Returns the value of the Napierian e raised to the power of the argument x.

proc exp(x: complex(128)): complex(128)

Returns the value of the Napierian e raised to the power of the argument x.

proc exp2(x: real(64)): real(64)

Returns the value of 2 raised to the power of the argument x.

proc exp2(x: real(32)): real(32)

Returns the value of 2 raised to the power of the argument x.

proc expm1(x: real(64)): real(64)

Returns one less than the value of the Napierian e raised to the power of the argument x.

proc expm1(x: real(32)): real(32)

Returns one less than the value of the Napierian e raised to the power of the argument x.

proc gamma(x: real(64)): real(64)

Returns the gamma function of the argument x.

proc gamma(x: real(32)): real(32)

Returns the gamma function of the argument x.

proc ldExp(x: real(64), exp: int(32)): real(64)

Returns the value of the argument x multiplied by 2 raised to the argument exp power, i.e., x * 2**exp.

proc ldExp(x: real(32), exp: int(32)): real(32)

Returns the value of the argument x multiplied by 2 raised to the argument exp power, i.e., x * 2**exp.

proc ldexp(x: real(64), n: int(32)): real(64)

Returns the value of the argument x multiplied by 2 raised to the argument n power, i.e., x * 2**n.

Warning

‘ldexp’ with an ‘n’ argument has been deprecated, please use ldExp with an ‘exp’ argument instead

proc ldexp(x: real(32), n: int(32)): real(32)

Returns the value of the argument x multiplied by 2 raised to the argument n power, i.e., x * 2**n.

Warning

‘ldexp’ with an ‘n’ argument has been deprecated, please use ldExp with an ‘exp’ argument instead

proc lnGamma(x: real(64)): real(64)

Returns the natural logarithm of the absolute value of the gamma function of the argument x.

proc lnGamma(x: real(32)): real(32)

Returns the natural logarithm of the absolute value of the gamma function of the argument x.

proc lgamma(x: real(64)): real(64)

Returns the natural logarithm of the absolute value of the gamma function of the argument x.

Warning

‘lgamma’ has been deprecated in favor of lnGamma, please use that instead

proc lgamma(x: real(32)): real(32)

Returns the natural logarithm of the absolute value of the gamma function of the argument x.

Warning

‘lgamma’ has been deprecated in favor of lnGamma, please use that instead

proc ln(x: real(64)): real(64)

Returns the natural logarithm of the argument x.

It is an error if x is less than or equal to zero.

proc ln(x: real(32)): real(32)

Returns the natural logarithm of the argument x.

It is an error if x is less than or equal to zero.

proc ln(x: complex(64)): complex(64)

Returns the natural logarithm of the argument x.

proc ln(x: complex(128)): complex(128)

Returns the natural logarithm of the argument x.

proc log(x: real(64)): real(64)

Returns the natural logarithm of the argument x.

It is an error if x is less than or equal to zero.

proc log(x: real(32)): real(32)

Returns the natural logarithm of the argument x.

It is an error if x is less than or equal to zero.

proc log(x: complex(64)): complex(64)

Returns the natural logarithm of the argument x.

proc log(x: complex(128)): complex(128)

Returns the natural logarithm of the argument x.

proc log10(x: real(64)): real(64)

Returns the base 10 logarithm of the argument x.

It is an error if x is less than or equal to zero.

proc log10(x: real(32)): real(32)

Returns the base 10 logarithm of the argument x.

It is an error if x is less than or equal to zero.

proc log1p(x: real(64)): real(64)

Returns the natural logarithm of x + 1.

It is an error if x is less than or equal to -1.

proc log1p(x: real(32)): real(32)

Returns the natural logarithm of x + 1.

It is an error if x is less than or equal to -1.

proc logBasePow2(x: int(?w), exp)

Returns the log to the base 2**exp of the given x value. If exp is 1, then returns the log to the base 2; if exp is 2, then returns the log to the base 4, etc. Any fractional part is discarded.

Return type

int

Warning

‘logBasePow2’ is unstable due to questions about its utility. If you rely on this function, please let us know!

proc logBasePow2(x: uint(?w), exp)

Returns the log to the base 2**exp of the given x value. If exp is 1, then returns the log to the base 2; if exp is 2, then returns the log to the base 4, etc. Any fractional part is discarded.

Return type

int

Warning

‘logBasePow2’ is unstable due to questions about its utility. If you rely on this function, please let us know!

proc logBasePow2(val: int(?w), baseLog2)

Returns the log to the base 2**baseLog2 of the given in value. If baseLog2 is 1, then returns the log to the base 2; if baseLog2 is 2, then returns the log to the base 4, etc. Any fractional part is discarded.

Return type

int

Warning

The ‘val’ and ‘baseLog2’ argument names are now deprecated, please use ‘x’ and ‘exp’ respectively

proc logBasePow2(val: uint(?w), baseLog2)

Returns the log to the base 2**baseLog2 of the given in value. If baseLog2 is 1, then returns the log to the base 2; if baseLog2 is 2, then returns the log to the base 4, etc. Any fractional part is discarded.

Return type

int

Warning

The ‘val’ and ‘baseLog2’ argument names are now deprecated, please use ‘x’ and ‘exp’ respectively

proc log2(x: real(64)): real(64)

Returns the base 2 logarithm of the argument x.

It is an error if x is less than or equal to zero.

proc log2(x: real(32)): real(32)

Returns the base 2 logarithm of the argument x.

It is an error if x is less than or equal to zero.

proc log2(x: int(?w))

Returns the base 2 logarithm of the argument x, rounded down.

Return type

int

It is an error if x is less than or equal to zero.

Warning

The version of ‘log2’ that takes an int argument is unstable

proc log2(x: uint(?w))

Returns the base 2 logarithm of the argument x, rounded down.

Return type

int

It is an error if x is equal to zero.

Warning

The version of ‘log2’ that takes an uint argument is unstable

proc nearbyint(x: real(64)): real(64)

Returns the rounded integral value of the argument x determined by the current rounding direction. nearbyint will not raise the “inexact” floating-point exception.

Warning

nearbyint is unstable while we design more thorough rounding support

proc nearbyint(x: real(32)): real(32)

Returns the rounded integral value of the argument x determined by the current rounding direction. nearbyint will not raise the “inexact” floating-point exception.

Warning

nearbyint is unstable while we design more thorough rounding support

proc rint(x: real(64)): real(64)

Returns the rounded integral value of the argument x determined by the current rounding direction. rint may raise the “inexact” floating-point exception.

Warning

rint is unstable while we design more thorough rounding support

proc rint(x: real(32)): real(32)

Returns the rounded integral value of the argument x determined by the current rounding direction. rint may raise the “inexact” floating-point exception.

Warning

rint is unstable while we design more thorough rounding support

proc sin(x: real(64)): real(64)

Returns the sine of the argument x.

proc sin(x: real(32)): real(32)

Returns the sine of the argument x.

proc sin(x: complex(64)): complex(64)

Returns the sine of the argument x.

proc sin(x: complex(128)): complex(128)

Returns the sine of the argument x.

proc sinh(x: real(64)): real(64)

Returns the hyperbolic sine of the argument x.

proc sinh(x: real(32)): real(32)

Returns the hyperbolic sine of the argument x.

proc sinh(x: complex(64)): complex(64)

Returns the hyperbolic sine of the argument x.

proc sinh(x: complex(128)): complex(128)

Returns the hyperbolic sine of the argument x.

proc tan(x: real(64)): real(64)

Returns the tangent of the argument x.

proc tan(x: real(32)): real(32)

Returns the tangent of the argument x.

proc tan(x: complex(64)): complex(64)

Returns the tangent of the argument x.

proc tan(x: complex(128)): complex(128)

Returns the tangent of the argument x.

proc tanh(x: real(64)): real(64)

Returns the hyperbolic tangent of the argument x.

proc tanh(x: real(32)): real(32)

Returns the hyperbolic tangent of the argument x.

proc tanh(x: complex(64)): complex(64)

Returns the hyperbolic tangent of the argument x.

proc tanh(x: complex(128)): complex(128)

Returns the hyperbolic tangent of the argument x.

proc tgamma(x: real(64)): real(64)

Returns the gamma function of the argument x.

Warning

‘tgamma’ has been deprecated in favor of gamma, please use that instead

proc tgamma(x: real(32)): real(32)

Returns the gamma function of the argument x.

Warning

‘tgamma’ has been deprecated in favor of gamma, please use that instead

proc gcd(in x: int, in y: int): int

Returns the greatest common divisor of the integer arguments x and y.

proc gcd(in x: int(32), in y: int(32)): int(32)

Returns the greatest common divisor of the integer arguments x and y.

proc gcd(in x: int(16), in y: int(16)): int(16)

Returns the greatest common divisor of the integer arguments x and y.

proc gcd(in x: int(8), in y: int(8)): int(8)

Returns the greatest common divisor of the integer arguments x and y.

proc gcd(in x: uint(64), in y: uint(64)): uint(64)

Returns the greatest common divisor of the unsigned integer arguments x and y.

proc gcd(in x: uint(32), in y: uint(32)): uint(32)

Returns the greatest common divisor of the unsigned integer arguments x and y.

proc gcd(in x: uint(16), in y: uint(16)): uint(16)

Returns the greatest common divisor of the unsigned integer arguments x and y.

proc gcd(in x: uint(8), in y: uint(8)): uint(8)

Returns the greatest common divisor of the unsigned integer arguments x and y.

proc j0(x: real(32)): real(32)

Returns the Bessel function of the first kind of order 0 of x.

Warning

‘j0’ is unstable and may be renamed or moved to a different module in the future

proc j0(x: real(64)): real(64)

Returns the Bessel function of the first kind of order 0 of x.

Warning

‘j0’ is unstable and may be renamed or moved to a different module in the future

proc j1(x: real(32)): real(32)

Returns the Bessel function of the first kind of order 1 of x.

Warning

‘j1’ is unstable and may be renamed or moved to a different module in the future

proc j1(x: real(64)): real(64)

Returns the Bessel function of the first kind of order 1 of x.

Warning

‘j1’ is unstable and may be renamed or moved to a different module in the future

proc jn(n: int, x: real(32)): real(32)

Returns the Bessel function of the first kind of order n of x.

Warning

‘jn’ is unstable and may be renamed or moved to a different module in the future

proc jn(n: int, x: real(64)): real(64)

Returns the Bessel function of the first kind of order n of x.

Warning

‘jn’ is unstable and may be renamed or moved to a different module in the future

proc y0(x: real(32)): real(32)

Returns the Bessel function of the second kind of order 0 of x, where x must be greater than 0.

Warning

‘y0’ is unstable and may be renamed or moved to a different module in the future

proc y0(x: real(64)): real(64)

Returns the Bessel function of the second kind of order 0 of x, where x must be greater than 0.

Warning

‘y0’ is unstable and may be renamed or moved to a different module in the future

proc y1(x: real(32)): real(32)

Returns the Bessel function of the second kind of order 1 of x, where x must be greater than 0.

Warning

‘y1’ is unstable and may be renamed or moved to a different module in the future

proc y1(x: real(64)): real(64)

Returns the Bessel function of the second kind of order 1 of x, where x must be greater than 0.

Warning

‘y1’ is unstable and may be renamed or moved to a different module in the future

proc yn(n: int, x: real(32)): real(32)

Returns the Bessel function of the second kind of order n of x, where x must be greater than 0.

Warning

‘yn’ is unstable and may be renamed or moved to a different module in the future

proc yn(n: int, x: real(64)): real(64)

Returns the Bessel function of the second kind of order n of x, where x must be greater than 0.

Warning

‘yn’ is unstable and may be renamed or moved to a different module in the future