Math

scalus.prelude.Math
object Math

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any
Self type
Math.type

Members list

Value members

Concrete methods

inline def abs(x: BigInt): BigInt

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
def clamp(self: BigInt, min: BigInt, max: BigInt): BigInt

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

self clamped 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
def exp2(exp: BigInt): BigInt

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

2 raised to the power of exp.

Note

Much cheaper than using pow(base = 2, _) for small exponents 0 < exp < 256.

Example
 exp2(-2) == 0
 exp2(0) == 1
 exp2(1) == 2
 exp2(4) == 16
 exp2(42) == 4398046511104
def gcd(x: BigInt, y: BigInt): BigInt

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
def isSqrt(self: BigInt, x: BigInt): Boolean

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
def log(self: BigInt, base: BigInt): BigInt

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 k such that base^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
def log2(self: BigInt): BigInt

The integer logarithm in base 2.

The integer logarithm in base 2.

Value parameters

self

logarithm argument

Attributes

Returns

Greatest integer k such that 2^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
inline def max(x: BigInt, y: BigInt): BigInt

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
inline def min(x: BigInt, y: BigInt): BigInt

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
def pow(base: BigInt, exp: BigInt): BigInt

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

base raised to the power of exp.

Example
 pow(3, 5) == 243
 pow(7, 2) == 49
 pow(3, -4) == 0
 pow(0, 0) == 1
 pow(513, 3) == 135005697
def sqrt(radicand: BigInt): Option[BigInt]

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. None for 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

Concrete fields

lazy val sirDeps: List[SIRModuleWithDeps]
lazy val sirModule: Module