# RenderScript Mathematical Constants and Functions

## Overview

The mathematical functions below can be applied to scalars and vectors. When applied to vectors, the returned value is a vector of the function applied to each entry of the input.

For example:``` float3 a, b; // The following call sets // a.x to sin(b.x), // a.y to sin(b.y), and // a.z to sin(b.z). a = sin(b); ```

See Vector Math Functions for functions like distance() and length() that interpret instead the input as a single vector in n-dimensional space.

The precision of the mathematical operations on 32 bit floats is affected by the pragmas rs_fp_relaxed and rs_fp_full. Under rs_fp_relaxed, subnormal values may be flushed to zero and rounding may be done towards zero. In comparison, rs_fp_full requires correct handling of subnormal values, i.e. smaller than 1.17549435e-38f. rs_fp_rull also requires round to nearest with ties to even.

Different precision/speed tradeoffs can be achieved by using variants of the common math functions. Functions with a name starting with

• native_: May have custom hardware implementations with weaker precision. Additionally, subnormal values may be flushed to zero, rounding towards zero may be used, and NaN and infinity input may not be handled correctly.
• half_: May perform internal computations using 16 bit floats. Additionally, subnormal values may be flushed to zero, and rounding towards zero may be used.

## Summary

Constants
M_1_PI 1 / pi, as a 32 bit float
M_2_PI 2 / pi, as a 32 bit float
M_2_SQRTPI 2 / sqrt(pi), as a 32 bit float
M_E e, as a 32 bit float
M_LN10 log_e(10), as a 32 bit float
M_LN2 log_e(2), as a 32 bit float
M_LOG10E log_10(e), as a 32 bit float
M_LOG2E log_2(e), as a 32 bit float
M_PI pi, as a 32 bit float
M_PI_2 pi / 2, as a 32 bit float
M_PI_4 pi / 4, as a 32 bit float
M_SQRT1_2 1 / sqrt(2), as a 32 bit float
M_SQRT2 sqrt(2), as a 32 bit float
Functions
abs Absolute value of an integer
acos Inverse cosine
acosh Inverse hyperbolic cosine
acospi Inverse cosine divided by pi
asin Inverse sine
asinh Inverse hyperbolic sine
asinpi Inverse sine divided by pi
atan Inverse tangent
atan2 Inverse tangent of a ratio
atan2pi Inverse tangent of a ratio, divided by pi
atanh Inverse hyperbolic tangent
atanpi Inverse tangent divided by pi
cbrt Cube root
ceil Smallest integer not less than a value
clamp Restrain a value to a range
clz Number of leading 0 bits
copysign Copies the sign of a number to another
cos Cosine
cosh Hypebolic cosine
cospi Cosine of a number multiplied by pi
erf Mathematical error function
erfc Mathematical complementary error function
exp e raised to a number
exp10 10 raised to a number
exp2 2 raised to a number
expm1 e raised to a number minus one
fabs Absolute value of a float
fdim Positive difference between two values
floor Smallest integer not greater than a value
fmax Maximum of two floats
fmin Minimum of two floats
fmod Modulo
fract Positive fractional part
frexp Binary mantissa and exponent
half_recip Reciprocal computed to 16 bit precision
half_rsqrt Reciprocal of a square root computed to 16 bit precision
half_sqrt Square root computed to 16 bit precision
hypot Hypotenuse
ilogb Base two exponent
ldexp Creates a floating point from mantissa and exponent
lgamma Natural logarithm of the gamma function
log Natural logarithm
log10 Base 10 logarithm
log1p Natural logarithm of a value plus 1
log2 Base 2 logarithm
logb Base two exponent
max Maximum
min Minimum
mix Mixes two values
modf Integral and fractional components
nan Not a Number
nan_half Not a Number
native_acos Approximate inverse cosine
native_acosh Approximate inverse hyperbolic cosine
native_acospi Approximate inverse cosine divided by pi
native_asin Approximate inverse sine
native_asinh Approximate inverse hyperbolic sine
native_asinpi Approximate inverse sine divided by pi
native_atan Approximate inverse tangent
native_atan2 Approximate inverse tangent of a ratio
native_atan2pi Approximate inverse tangent of a ratio, divided by pi
native_atanh Approximate inverse hyperbolic tangent
native_atanpi Approximate inverse tangent divided by pi
native_cbrt Approximate cube root
native_cos Approximate cosine
native_cosh Approximate hypebolic cosine
native_cospi Approximate cosine of a number multiplied by pi
native_divide Approximate division
native_exp Approximate e raised to a number
native_exp10 Approximate 10 raised to a number
native_exp2 Approximate 2 raised to a number
native_expm1 Approximate e raised to a number minus one
native_hypot Approximate hypotenuse
native_log Approximate natural logarithm
native_log10 Approximate base 10 logarithm
native_log1p Approximate natural logarithm of a value plus 1
native_log2 Approximate base 2 logarithm
native_powr Approximate positive base raised to an exponent
native_recip Approximate reciprocal
native_rootn Approximate nth root
native_rsqrt Approximate reciprocal of a square root
native_sin Approximate sine
native_sincos Approximate sine and cosine
native_sinh Approximate hyperbolic sine
native_sinpi Approximate sine of a number multiplied by pi
native_sqrt Approximate square root
native_tan Approximate tangent
native_tanh Approximate hyperbolic tangent
native_tanpi Approximate tangent of a number multiplied by pi
nextafter Next floating point number
pow Base raised to an exponent
pown Base raised to an integer exponent
powr Positive base raised to an exponent
remainder Remainder of a division
remquo Remainder and quotient of a division
rint Round to even
rootn Nth root
round Round away from zero
rsRand Pseudo-random number
rsqrt Reciprocal of a square root
sign Sign of a value
sin Sine
sincos Sine and cosine
sinh Hyperbolic sine
sinpi Sine of a number multiplied by pi
sqrt Square root
step 0 if less than a value, 1 otherwise
tan Tangent
tanh Hyperbolic tangent
tanpi Tangent of a number multiplied by pi
tgamma Gamma function
trunc Truncates a floating point
Deprecated Functions
rsClamp Deprecated. Restrain a value to a range
rsFrac Deprecated. Returns the fractional part of a float

## Constants

#### M_1_PI: 1 / pi, as a 32 bit float

 Value: 0.318309886183790671537767526745028724f

The inverse of pi, as a 32 bit float.

#### M_2_PI: 2 / pi, as a 32 bit float

 Value: 0.636619772367581343075535053490057448f

2 divided by pi, as a 32 bit float.

#### M_2_SQRTPI: 2 / sqrt(pi), as a 32 bit float

 Value: 1.128379167095512573896158903121545172f

2 divided by the square root of pi, as a 32 bit float.

#### M_E: e, as a 32 bit float

 Value: 2.718281828459045235360287471352662498f

The number e, the base of the natural logarithm, as a 32 bit float.

#### M_LN10: log_e(10), as a 32 bit float

 Value: 2.302585092994045684017991454684364208f

The natural logarithm of 10, as a 32 bit float.

#### M_LN2: log_e(2), as a 32 bit float

 Value: 0.693147180559945309417232121458176568f

The natural logarithm of 2, as a 32 bit float.

#### M_LOG10E: log_10(e), as a 32 bit float

 Value: 0.434294481903251827651128918916605082f

The logarithm base 10 of e, as a 32 bit float.

#### M_LOG2E: log_2(e), as a 32 bit float

 Value: 1.442695040888963407359924681001892137f

The logarithm base 2 of e, as a 32 bit float.

#### M_PI: pi, as a 32 bit float

 Value: 3.141592653589793238462643383279502884f

The constant pi, as a 32 bit float.

#### M_PI_2: pi / 2, as a 32 bit float

 Value: 1.570796326794896619231321691639751442f

Pi divided by 2, as a 32 bit float.

#### M_PI_4: pi / 4, as a 32 bit float

 Value: 0.785398163397448309615660845819875721f

Pi divided by 4, as a 32 bit float.

#### M_SQRT1_2: 1 / sqrt(2), as a 32 bit float

 Value: 0.707106781186547524400844362104849039f

The inverse of the square root of 2, as a 32 bit float.

#### M_SQRT2: sqrt(2), as a 32 bit float

 Value: 1.414213562373095048801688724209698079f

The square root of 2, as a 32 bit float.

## Functions

#### abs: Absolute value of an integer

 uchar abs(char v); uchar2 abs(char2 v); uchar3 abs(char3 v); uchar4 abs(char4 v); uint abs(int v); uint2 abs(int2 v); uint3 abs(int3 v); uint4 abs(int4 v); ushort abs(short v); ushort2 abs(short2 v); ushort3 abs(short3 v); ushort4 abs(short4 v);

Returns the absolute value of an integer.

For floats, use fabs().

#### acos: Inverse cosine

 float acos(float v); float2 acos(float2 v); float3 acos(float3 v); float4 acos(float4 v); half acos(half v); Added in API level 24 half2 acos(half2 v); Added in API level 24 half3 acos(half3 v); Added in API level 24 half4 acos(half4 v); Added in API level 24

Returns the inverse cosine, in radians.

#### acosh: Inverse hyperbolic cosine

 float acosh(float v); float2 acosh(float2 v); float3 acosh(float3 v); float4 acosh(float4 v); half acosh(half v); Added in API level 24 half2 acosh(half2 v); Added in API level 24 half3 acosh(half3 v); Added in API level 24 half4 acosh(half4 v); Added in API level 24

Returns the inverse hyperbolic cosine, in radians.

#### acospi: Inverse cosine divided by pi

 float acospi(float v); float2 acospi(float2 v); float3 acospi(float3 v); float4 acospi(float4 v); half acospi(half v); Added in API level 24 half2 acospi(half2 v); Added in API level 24 half3 acospi(half3 v); Added in API level 24 half4 acospi(half4 v); Added in API level 24

Returns the inverse cosine in radians, divided by pi.

To get an inverse cosine measured in degrees, use `acospi(a) * 180.f`.

#### asin: Inverse sine

 float asin(float v); float2 asin(float2 v); float3 asin(float3 v); float4 asin(float4 v); half asin(half v); Added in API level 24 half2 asin(half2 v); Added in API level 24 half3 asin(half3 v); Added in API level 24 half4 asin(half4 v); Added in API level 24

Returns the inverse sine, in radians.

#### asinh: Inverse hyperbolic sine

 float asinh(float v); float2 asinh(float2 v); float3 asinh(float3 v); float4 asinh(float4 v); half asinh(half v); Added in API level 24 half2 asinh(half2 v); Added in API level 24 half3 asinh(half3 v); Added in API level 24 half4 asinh(half4 v); Added in API level 24

Returns the inverse hyperbolic sine, in radians.

#### asinpi: Inverse sine divided by pi

 float asinpi(float v); float2 asinpi(float2 v); float3 asinpi(float3 v); float4 asinpi(float4 v); half asinpi(half v); Added in API level 24 half2 asinpi(half2 v); Added in API level 24 half3 asinpi(half3 v); Added in API level 24 half4 asinpi(half4 v); Added in API level 24

Returns the inverse sine in radians, divided by pi.

To get an inverse sine measured in degrees, use `asinpi(a) * 180.f`.

#### atan: Inverse tangent

 float atan(float v); float2 atan(float2 v); float3 atan(float3 v); float4 atan(float4 v); half atan(half v); Added in API level 24 half2 atan(half2 v); Added in API level 24 half3 atan(half3 v); Added in API level 24 half4 atan(half4 v); Added in API level 24

Returns the inverse tangent, in radians.

#### atan2: Inverse tangent of a ratio

 float atan2(float numerator, float denominator); float2 atan2(float2 numerator, float2 denominator); float3 atan2(float3 numerator, float3 denominator); float4 atan2(float4 numerator, float4 denominator); half atan2(half numerator, half denominator); Added in API level 24 half2 atan2(half2 numerator, half2 denominator); Added in API level 24 half3 atan2(half3 numerator, half3 denominator); Added in API level 24 half4 atan2(half4 numerator, half4 denominator); Added in API level 24
##### Parameters
numerator Numerator. Denominator. Can be 0.

Returns the inverse tangent of `(numerator / denominator)`, in radians.

#### atan2pi: Inverse tangent of a ratio, divided by pi

 float atan2pi(float numerator, float denominator); float2 atan2pi(float2 numerator, float2 denominator); float3 atan2pi(float3 numerator, float3 denominator); float4 atan2pi(float4 numerator, float4 denominator); half atan2pi(half numerator, half denominator); Added in API level 24 half2 atan2pi(half2 numerator, half2 denominator); Added in API level 24 half3 atan2pi(half3 numerator, half3 denominator); Added in API level 24 half4 atan2pi(half4 numerator, half4 denominator); Added in API level 24
##### Parameters
numerator Numerator. Denominator. Can be 0.

Returns the inverse tangent of `(numerator / denominator)`, in radians, divided by pi.

To get an inverse tangent measured in degrees, use `atan2pi(n, d) * 180.f`.

#### atanh: Inverse hyperbolic tangent

 float atanh(float v); float2 atanh(float2 v); float3 atanh(float3 v); float4 atanh(float4 v); half atanh(half v); Added in API level 24 half2 atanh(half2 v); Added in API level 24 half3 atanh(half3 v); Added in API level 24 half4 atanh(half4 v); Added in API level 24

Returns the inverse hyperbolic tangent, in radians.

#### atanpi: Inverse tangent divided by pi

 float atanpi(float v); float2 atanpi(float2 v); float3 atanpi(float3 v); float4 atanpi(float4 v); half atanpi(half v); Added in API level 24 half2 atanpi(half2 v); Added in API level 24 half3 atanpi(half3 v); Added in API level 24 half4 atanpi(half4 v); Added in API level 24

Returns the inverse tangent in radians, divided by pi.

To get an inverse tangent measured in degrees, use `atanpi(a) * 180.f`.

#### cbrt: Cube root

 float cbrt(float v); float2 cbrt(float2 v); float3 cbrt(float3 v); float4 cbrt(float4 v); half cbrt(half v); Added in API level 24 half2 cbrt(half2 v); Added in API level 24 half3 cbrt(half3 v); Added in API level 24 half4 cbrt(half4 v); Added in API level 24

Returns the cube root.

#### ceil: Smallest integer not less than a value

 float ceil(float v); float2 ceil(float2 v); float3 ceil(float3 v); float4 ceil(float4 v); half ceil(half v); Added in API level 24 half2 ceil(half2 v); Added in API level 24 half3 ceil(half3 v); Added in API level 24 half4 ceil(half4 v); Added in API level 24

Returns the smallest integer not less than a value.

For example, `ceil(1.2f)` returns 2.f, and `ceil(-1.2f)` returns -1.f.

#### clamp: Restrain a value to a range

##### Parameters
value Value to be clamped. Lower bound, a scalar or matching vector. High bound, must match the type of low.

Clamps a value to a specified high and low bound. clamp() returns min_value if value < min_value, max_value if value > max_value, otherwise value.

There are two variants of clamp: one where the min and max are scalars applied to all entries of the value, the other where the min and max are also vectors.

If min_value is greater than max_value, the results are undefined.

#### clz: Number of leading 0 bits

 char clz(char value); char2 clz(char2 value); char3 clz(char3 value); char4 clz(char4 value); int clz(int value); int2 clz(int2 value); int3 clz(int3 value); int4 clz(int4 value); short clz(short value); short2 clz(short2 value); short3 clz(short3 value); short4 clz(short4 value); uchar clz(uchar value); uchar2 clz(uchar2 value); uchar3 clz(uchar3 value); uchar4 clz(uchar4 value); uint clz(uint value); uint2 clz(uint2 value); uint3 clz(uint3 value); uint4 clz(uint4 value); ushort clz(ushort value); ushort2 clz(ushort2 value); ushort3 clz(ushort3 value); ushort4 clz(ushort4 value);

Returns the number of leading 0-bits in a value.

For example, `clz((char)0x03)` returns 6.

#### copysign: Copies the sign of a number to another

 float copysign(float magnitude_value, float sign_value); float2 copysign(float2 magnitude_value, float2 sign_value); float3 copysign(float3 magnitude_value, float3 sign_value); float4 copysign(float4 magnitude_value, float4 sign_value); half copysign(half magnitude_value, half sign_value); Added in API level 24 half2 copysign(half2 magnitude_value, half2 sign_value); Added in API level 24 half3 copysign(half3 magnitude_value, half3 sign_value); Added in API level 24 half4 copysign(half4 magnitude_value, half4 sign_value); Added in API level 24

Copies the sign from sign_value to magnitude_value.

The value returned is either magnitude_value or -magnitude_value.

For example, `copysign(4.0f, -2.7f)` returns -4.0f and `copysign(-4.0f, 2.7f)` returns 4.0f.

#### cos: Cosine

 float cos(float v); float2 cos(float2 v); float3 cos(float3 v); float4 cos(float4 v); half cos(half v); Added in API level 24 half2 cos(half2 v); Added in API level 24 half3 cos(half3 v); Added in API level 24 half4 cos(half4 v); Added in API level 24

Returns the cosine of an angle measured in radians.

#### cosh: Hypebolic cosine

 float cosh(float v); float2 cosh(float2 v); float3 cosh(float3 v); float4 cosh(float4 v); half cosh(half v); Added in API level 24 half2 cosh(half2 v); Added in API level 24 half3 cosh(half3 v); Added in API level 24 half4 cosh(half4 v); Added in API level 24

Returns the hypebolic cosine of v, where v is measured in radians.

#### cospi: Cosine of a number multiplied by pi

 float cospi(float v); float2 cospi(float2 v); float3 cospi(float3 v); float4 cospi(float4 v); half cospi(half v); Added in API level 24 half2 cospi(half2 v); Added in API level 24 half3 cospi(half3 v); Added in API level 24 half4 cospi(half4 v); Added in API level 24

Returns the cosine of `(v * pi)`, where `(v * pi)` is measured in radians.

To get the cosine of a value measured in degrees, call `cospi(v / 180.f)`.

#### degrees: Converts radians into degrees

 float degrees(float v); float2 degrees(float2 v); float3 degrees(float3 v); float4 degrees(float4 v); half degrees(half v); Added in API level 24 half2 degrees(half2 v); Added in API level 24 half3 degrees(half3 v); Added in API level 24 half4 degrees(half4 v); Added in API level 24

#### erf: Mathematical error function

 float erf(float v); float2 erf(float2 v); float3 erf(float3 v); float4 erf(float4 v); half erf(half v); Added in API level 24 half2 erf(half2 v); Added in API level 24 half3 erf(half3 v); Added in API level 24 half4 erf(half4 v); Added in API level 24

Returns the error function.

#### erfc: Mathematical complementary error function

 float erfc(float v); float2 erfc(float2 v); float3 erfc(float3 v); float4 erfc(float4 v); half erfc(half v); Added in API level 24 half2 erfc(half2 v); Added in API level 24 half3 erfc(half3 v); Added in API level 24 half4 erfc(half4 v); Added in API level 24

Returns the complementary error function.

#### exp: e raised to a number

 float exp(float v); float2 exp(float2 v); float3 exp(float3 v); float4 exp(float4 v); half exp(half v); Added in API level 24 half2 exp(half2 v); Added in API level 24 half3 exp(half3 v); Added in API level 24 half4 exp(half4 v); Added in API level 24

Returns e raised to v, i.e. e ^ v.

#### exp10: 10 raised to a number

 float exp10(float v); float2 exp10(float2 v); float3 exp10(float3 v); float4 exp10(float4 v); half exp10(half v); Added in API level 24 half2 exp10(half2 v); Added in API level 24 half3 exp10(half3 v); Added in API level 24 half4 exp10(half4 v); Added in API level 24

Returns 10 raised to v, i.e. 10.f ^ v.

#### exp2: 2 raised to a number

 float exp2(float v); float2 exp2(float2 v); float3 exp2(float3 v); float4 exp2(float4 v); half exp2(half v); Added in API level 24 half2 exp2(half2 v); Added in API level 24 half3 exp2(half3 v); Added in API level 24 half4 exp2(half4 v); Added in API level 24

Returns 2 raised to v, i.e. 2.f ^ v.

#### expm1: e raised to a number minus one

 float expm1(float v); float2 expm1(float2 v); float3 expm1(float3 v); float4 expm1(float4 v); half expm1(half v); Added in API level 24 half2 expm1(half2 v); Added in API level 24 half3 expm1(half3 v); Added in API level 24 half4 expm1(half4 v); Added in API level 24

Returns e raised to v minus 1, i.e. (e ^ v) - 1.

#### fabs: Absolute value of a float

 float fabs(float v); float2 fabs(float2 v); float3 fabs(float3 v); float4 fabs(float4 v); half fabs(half v); Added in API level 24 half2 fabs(half2 v); Added in API level 24 half3 fabs(half3 v); Added in API level 24 half4 fabs(half4 v); Added in API level 24

Returns the absolute value of the float v.

For integers, use abs().

#### fdim: Positive difference between two values

 float fdim(float a, float b); float2 fdim(float2 a, float2 b); float3 fdim(float3 a, float3 b); float4 fdim(float4 a, float4 b); half fdim(half a, half b); Added in API level 24 half2 fdim(half2 a, half2 b); Added in API level 24 half3 fdim(half3 a, half3 b); Added in API level 24 half4 fdim(half4 a, half4 b); Added in API level 24

Returns the positive difference between two values.

If a > b, returns (a - b) otherwise returns 0f.

#### floor: Smallest integer not greater than a value

 float floor(float v); float2 floor(float2 v); float3 floor(float3 v); float4 floor(float4 v); half floor(half v); Added in API level 24 half2 floor(half2 v); Added in API level 24 half3 floor(half3 v); Added in API level 24 half4 floor(half4 v); Added in API level 24

Returns the smallest integer not greater than a value.

For example, `floor(1.2f)` returns 1.f, and `floor(-1.2f)` returns -2.f.

 float fma(float multiplicand1, float multiplicand2, float offset); float2 fma(float2 multiplicand1, float2 multiplicand2, float2 offset); float3 fma(float3 multiplicand1, float3 multiplicand2, float3 offset); float4 fma(float4 multiplicand1, float4 multiplicand2, float4 offset); half fma(half multiplicand1, half multiplicand2, half offset); Added in API level 24 half2 fma(half2 multiplicand1, half2 multiplicand2, half2 offset); Added in API level 24 half3 fma(half3 multiplicand1, half3 multiplicand2, half3 offset); Added in API level 24 half4 fma(half4 multiplicand1, half4 multiplicand2, half4 offset); Added in API level 24

Multiply and add. Returns `(multiplicand1 * multiplicand2) + offset`.

This function is similar to mad(). fma() retains full precision of the multiplied result and rounds only after the addition. mad() rounds after the multiplication and the addition. This extra precision is not guaranteed in rs_fp_relaxed mode.

#### fmax: Maximum of two floats

 float fmax(float a, float b); float2 fmax(float2 a, float b); float2 fmax(float2 a, float2 b); float3 fmax(float3 a, float b); float3 fmax(float3 a, float3 b); float4 fmax(float4 a, float b); float4 fmax(float4 a, float4 b); half fmax(half a, half b); Added in API level 24 half2 fmax(half2 a, half b); Added in API level 24 half2 fmax(half2 a, half2 b); Added in API level 24 half3 fmax(half3 a, half b); Added in API level 24 half3 fmax(half3 a, half3 b); Added in API level 24 half4 fmax(half4 a, half b); Added in API level 24 half4 fmax(half4 a, half4 b); Added in API level 24

Returns the maximum of a and b, i.e. `(a < b ? b : a)`.

The max() function returns identical results but can be applied to more data types.

#### fmin: Minimum of two floats

 float fmin(float a, float b); float2 fmin(float2 a, float b); float2 fmin(float2 a, float2 b); float3 fmin(float3 a, float b); float3 fmin(float3 a, float3 b); float4 fmin(float4 a, float b); float4 fmin(float4 a, float4 b); half fmin(half a, half b); Added in API level 24 half2 fmin(half2 a, half b); Added in API level 24 half2 fmin(half2 a, half2 b); Added in API level 24 half3 fmin(half3 a, half b); Added in API level 24 half3 fmin(half3 a, half3 b); Added in API level 24 half4 fmin(half4 a, half b); Added in API level 24 half4 fmin(half4 a, half4 b); Added in API level 24

Returns the minimum of a and b, i.e. `(a > b ? b : a)`.

The min() function returns identical results but can be applied to more data types.

#### fmod: Modulo

 float fmod(float numerator, float denominator); float2 fmod(float2 numerator, float2 denominator); float3 fmod(float3 numerator, float3 denominator); float4 fmod(float4 numerator, float4 denominator); half fmod(half numerator, half denominator); Added in API level 24 hal