Math
Attributes
- Graph
-
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
Math.type
Members list
Value members
Concrete methods
Calculate the absolute value of an integer.
Calculate the absolute value of an integer.
Attributes
- Returns
-
The absolute value.
- Example
-
abs(-3) == 3 abs(5) == 5
Restrict the value of an integer between two min and max bounds
Restrict the value of an integer between two min and max bounds
Value parameters
- max
-
highest allowed value.
- min
-
lowest allowed value.
- self
-
an integer to restrict
Attributes
- Returns
-
selfclamped to[min, max]. - Example
-
clamp(self = 14, min = 0, max = 10) == 10 clamp(self = -4, min = 0, max = 10) == 0 clamp(self = 7, min = 0, max = 10) == 7
Calculates the power of 2 for a given exponent.
Calculates the power of 2 for a given exponent.
Value parameters
- exp
-
The power's exponent to raise.
Attributes
- Returns
-
2raised to the power ofexp. - Note
-
Much cheaper than using
pow(base = 2, _)for small exponents0 < exp < 256. - Example
-
exp2(-2) == 0 exp2(0) == 1 exp2(1) == 2 exp2(4) == 16 exp2(42) == 4398046511104
The greatest common divisor of this and another integer.
The greatest common divisor of this and another integer.
Value parameters
- other
-
The other number to compute GCD with.
- self
-
A number to compute GCD with.
Attributes
- Returns
-
The greatest common divisor.
- Example
-
gcd(42, 14) == 14 gcd(14, 42) == 14 gcd(0, 0) == 0
Checks if an integer has a given integer square root x.
Checks if an integer has a given integer square root x.
The check has constant time complexity O(1).
Attributes
- Returns
-
True if it is a perfect square, false otherwise.
- Example
-
isSqrt(0, 0) == true isSqrt(25, 5) == true isSqrt(25, -5) == false isSqrt(44203, 210) == true
Computes a logarithm in a given base using integer divisions.
Computes a logarithm in a given base using integer divisions.
Value parameters
- base
-
Base of logarithm.
- self
-
logarithm argument
Attributes
- Returns
-
Greatest integer
ksuch thatbase^k <= self. - Example
-
log(10, base = 2) == 3 log(42, base = 2) == 5 log(42, base = 3) == 3 log(5, base = 0) == 0 log(4, base = 4) == 1 log(4, base = 42) == 0
The integer logarithm in base 2.
The integer logarithm in base 2.
Value parameters
- self
-
logarithm argument
Attributes
- Returns
-
Greatest integer
ksuch that2^k <= self. - Note
-
Faster than
log(_, base = 2)in this particular case. - Example
-
log2(1) == 0 log2(2) == 1 log2(3) == 1 log2(4) == 2 log2(256) == 8 log2(257) == 8 log2(511) == 8 log2(1025) == 10
Returns the larger of two integers.
Returns the larger of two integers.
Attributes
- Returns
-
The maximum.
- Example
-
man(1, 2) == 2 man(-1, -5) == -1
Returns the smaller of two integers.
Returns the smaller of two integers.
Attributes
- Returns
-
The minimum.
- Example
-
min(1, 2) == 1 min(-1, -5) == -5
Raises an integer to a power using the exponentiation by squaring method.
Raises an integer to a power using the exponentiation by squaring method.
Value parameters
- base
-
The base to be raised to a power.
- exp
-
The power's exponent to raise.
Attributes
- Returns
-
baseraised to the power ofexp. - Example
-
pow(3, 5) == 243 pow(7, 2) == 49 pow(3, -4) == 0 pow(0, 0) == 1 pow(513, 3) == 135005697
Calculates the square root of an integer using the babylonian method.
Calculates the square root of an integer using the babylonian method.
Value parameters
- radicand
-
The number to take the square root of
Attributes
- Returns
-
The exact result or the smallest integer nearest to the square root.
Nonefor negative values. - See also
- Note
-
This function can be quite expensive to perform on-chain. Prefer using scalus.prelude.isSqrt whenever possible.
- Example
-
sqrt(0) == Some(0) sqrt(25) == Some(5) sqrt(44203) == Some(210) sqrt(-42) == None