Skip to content

Fct

Spice
import "std/math/fct";

Functions

abs

Spice
public inline f<Numeric> abs<Numeric>(Numeric input)

Calculate absolute value of the input

Parameters

Name Type Description
input Numeric Input number

Returns: Numeric — Absulute value of input

max

Spice
public inline f<Numeric> max<Numeric>(Numeric input1, Numeric input2)

Calculate the maximum of two inputs

Parameters

Name Type Description
input1 Numeric First input
input2 Numeric Second input

Returns: Numeric — Maximum of inputs

min

Spice
public inline f<Numeric> min<Numeric>(Numeric input1, Numeric input2)

Calculate the minimum of two inputs

Parameters

Name Type Description
input1 Numeric First input
input2 Numeric Second input

Returns: Numeric — Minimum of inputs

trunc

Spice
public inline f<int> trunc(double input)

Truncate the number to a whole number

Parameters

Name Type Description
input double Intput number

Returns: int — Rounding result

floor

Spice
public inline f<int> floor(double input)

Round the input number down

Parameters

Name Type Description
input double Intput number

Returns: int — Rounding result

ceil

Spice
public inline f<int> ceil(double input)

Round the input number up

Parameters

Name Type Description
input double Intput number

Returns: int — Rounding result

round

Spice
public inline f<int> round(double input)

Round the input number to a whole number Note: 0.499999 is rounded down to 0, 0.5 is rounded up to 1.

Parameters

Name Type Description
input double Input number

Returns: int — Rounding result

round

Spice
public inline f<double> round(double input, unsigned int places)

Round the input number to the given number of decimal places Note: For 2 places 0.444 is rounded down to 0.44, 0.445 is rounded up to 0.45.

Parameters

Name Type Description
input double Input number
places unsigned int Number of decimal places

Returns: double

degToRad

Spice
public inline f<double> degToRad(double degrees)

Convert degrees to radians

Parameters

Name Type Description
degrees double Input in degrees

Returns: double — Input in radians

radToDeg

Spice
public inline f<double> radToDeg(double radians)

Convert radians to degrees

Parameters

Name Type Description
radians double

Returns: double — Input in degrees

sin

Spice
public f<double> sin<Numeric>(Numeric x)

Calculate the sine of the input, using the taylor series:
sin x = x - x^3/3! + x^5/5! - x^7/7! ...

Parameters

Name Type Description
x Numeric Input number

Returns: double — Sine of input

cos

Spice
public f<double> cos<Numeric>(Numeric x)

Calculate the cosine of the input, using the taylor series:
cos x = 1 - x^2/2! + x^4/4! - x^6/6! ...

Parameters

Name Type Description
x Numeric Input number

Returns: double — Cosine of input

factorial

Spice
public inline f<WholeNumber> factorial<WholeNumber>(WholeNumber input)

Calculate the factorial of the input

Parameters

Name Type Description
input WholeNumber Input number

Returns: WholeNumber — Factorial of input

sqrt

Spice
public f<double> sqrt(double x)

Calculate the square root of the input, using Heron's method. Note: For negative inputs 0.0 is returned, as the result is not a real number.

Parameters

Name Type Description
x double Input number

Returns: double — Square root of input

cbrt

Spice
public f<double> cbrt(double x)

Calculate the cube root of the input, using Newton's method.

Parameters

Name Type Description
x double Input number

Returns: double — Cube root of input

hypot

Spice
public inline f<double> hypot(double a, double b)

Calculate the length of the hypotenuse of a right-angled triangle from its legs.

Parameters

Name Type Description
a double Length of the first leg
b double Length of the second leg

Returns: double — Length of the hypotenuse

exp

Spice
public f<double> exp(double x)

Calculate e raised to the power of the input, using the taylor series:
exp x = 1 + x + x^2/2! + x^3/3! ...

Parameters

Name Type Description
x double Input number

Returns: double — e raised to the power of input

ln

Spice
public f<double> ln(double x)

Calculate the natural logarithm (base e) of the input.
The input is range reduced via e, before applying the series:
ln x = 2 * (t + t^3/3 + t^5/5 ...) with t = (x-1)/(x+1)
Note: For inputs <= 0 the result 0.0 is returned, as the result is not a real number.

Parameters

Name Type Description
x double Input number

Returns: double — Natural logarithm of input

log2

Spice
public inline f<double> log2(double x)

Calculate the binary logarithm (base 2) of the input.

Parameters

Name Type Description
x double Input number

Returns: double — Binary logarithm of input

log10

Spice
public inline f<double> log10(double x)

Calculate the common logarithm (base 10) of the input.

Parameters

Name Type Description
x double Input number

Returns: double — Common logarithm of input

log

Spice
public inline f<double> log(double x, double base)

Calculate the logarithm of the input to an arbitrary base.

Parameters

Name Type Description
x double Input number
base double Logarithm base

Returns: double — Logarithm of input to the given base

pow

Spice
public f<double> pow(double base, int exponent)

Raise the base to an integer power, using exponentiation by squaring.

Parameters

Name Type Description
base double Base number
exponent int Integer exponent

Returns: double — base raised to the power of exponent

pow

Spice
public f<double> pow(double base, double exponent)

Raise the base to an arbitrary power, computed as exp(exponent * ln(base)). Note: For a negative base only integer exponents are defined, otherwise 0.0 is returned.

Parameters

Name Type Description
base double Base number
exponent double Exponent

Returns: double — base raised to the power of exponent

tan

Spice
public inline f<double> tan<Numeric>(Numeric x)

Calculate the tangent of the input (in degrees).

Parameters

Name Type Description
x Numeric Input number in degrees

Returns: double — Tangent of input

atan

Spice
public f<double> atan(double x)

Calculate the arc tangent of the input, returning the result in degrees.
The input is range reduced using the half-angle identity, before applying the series:
atan x = x - x^3/3 + x^5/5 - x^7/7 ...

Parameters

Name Type Description
x double Input number

Returns: double — Arc tangent of input in degrees

atan2

Spice
public f<double> atan2(double y, double x)

Calculate the arc tangent of y/x, returning the result in degrees. Unlike atan, the signs of both arguments are used to determine the correct quadrant.

Parameters

Name Type Description
y double Ordinate (y-coordinate)
x double Abscissa (x-coordinate)

Returns: double — Arc tangent of y/x in degrees

asin

Spice
public f<double> asin(double x)

Calculate the arc sine of the input, returning the result in degrees. Note: For inputs outside of [-1, 1] the result is clamped to +/- 90 degrees.

Parameters

Name Type Description
x double Input number in range [-1, 1]

Returns: double — Arc sine of input in degrees

acos

Spice
public inline f<double> acos(double x)

Calculate the arc cosine of the input, returning the result in degrees. Note: For inputs outside of [-1, 1] the result is clamped to [0, 180] degrees.

Parameters

Name Type Description
x double Input number in range [-1, 1]

Returns: double — Arc cosine of input in degrees

sinh

Spice
public inline f<double> sinh(double x)

Calculate the hyperbolic sine of the input.

Parameters

Name Type Description
x double Input number

Returns: double — Hyperbolic sine of input

cosh

Spice
public inline f<double> cosh(double x)

Calculate the hyperbolic cosine of the input.

Parameters

Name Type Description
x double Input number

Returns: double — Hyperbolic cosine of input

tanh

Spice
public f<double> tanh(double x)

Calculate the hyperbolic tangent of the input.

Parameters

Name Type Description
x double Input number

Returns: double — Hyperbolic tangent of input

gcd

Spice
public f<WholeNumber> gcd<WholeNumber>(WholeNumber input1, WholeNumber input2)

Calculate the greatest common divisor of two inputs, using the Euclidean algorithm.

Parameters

Name Type Description
input1 WholeNumber First input
input2 WholeNumber Second input

Returns: WholeNumber — Greatest common divisor of the inputs

lcm

Spice
public f<WholeNumber> lcm<WholeNumber>(WholeNumber input1, WholeNumber input2)

Calculate the least common multiple of two inputs.

Parameters

Name Type Description
input1 WholeNumber First input
input2 WholeNumber Second input

Returns: WholeNumber — Least common multiple of the inputs

clamp

Spice
public inline f<Numeric> clamp<Numeric>(Numeric value, Numeric minValue, Numeric maxValue)

Clamp the value to the inclusive range [minValue, maxValue].

Parameters

Name Type Description
value Numeric Input value
minValue Numeric Lower bound
maxValue Numeric Upper bound

Returns: Numeric — Clamped value

almostEqual

Spice
public inline f<bool> almostEqual(double input1, double input2)

Check if two floating point numbers are approximately equal within a default epsilon.

Parameters

Name Type Description
input1 double First input
input2 double Second input

Returns: bool — true if the inputs are approximately equal

almostEqual

Spice
public inline f<bool> almostEqual(double input1, double input2, double epsilon)

Check if two floating point numbers are approximately equal within the given epsilon.

Parameters

Name Type Description
input1 double First input
input2 double Second input
epsilon double Maximum allowed absolute difference

Returns: bool — true if the inputs are approximately equal