# [Ride v4 and v3] Math functions
⚠️ This is the documentation for the Standard library version 4 and 3. We recommend to use version 6. Go to version 6
| Name | Description | Complexity |
|---|---|---|
| fraction | Multiplies and divides integers to avoid overflow | 1 |
| log | Calculates logarithm of a number with a given base | 100 |
| median | Returns the median of a list of integers | 20 |
| pow | Raises a number to a given power | 100 |
# fraction
Multiplies integers a, b and divides the result by the integer c to avoid overflow.
Fraction a × b / c should not exceed the maximum value of the integer type 9,223,372,036,854,755,807.
fraction(a: Int, b: Int, c: Int): Int
# Parameters
| Parameter | Description |
|---|---|
a: Int | Integer a |
b: Int | Integer b |
c: Int | Integer c |
# Example
Lets assume that:
a = 100,000,000,000,
b = 50,000,000,000,000,
c = 2,500,000.
The following formula, with operators * and /, fails due to overflow:
a * b / c # overflow, because a × b exceeds max integer value
The fraction function with no overflow:
fraction(a, b, c) # Result: 2,000,000,000,000,000,000
# log
Calculates logba.
log(value: Int, vp: Int, base: Int, bp: Int, rp: Int, round: UP|DOWN|CEILING|FLOOR|HALFUP|HALFDOWN|HALFEVEN): Int
In Ride, there is no data type with the floating point. That is why, for example, when you need to calculate log2.716.25 then the number value = 1625, vp = 2 and the base = 27, bp = 1.
More examples:
| a | value | vp |
|---|---|---|
| 16.25 | 1625 | 2 |
| 5 | 5 | 0 |
| 5.00 | 500 | 2 |
If the log function returns, for example, 2807, and the parameter of function rp = 3, then the result is 2.807; in the number 2807 the last 3 digits is a fractional part.
# Parameters
| Parameter | Description |
|---|---|
value: Int | Number a without decimal point |
vp: Int | Number of decimals of a |
base: Int | Logarithm base b without decimal point |
bp: Int | Number of decimals of b |
rp: Int | Number of decimals in the resulting value, from 0 to 8 inclusive. Specifies the accuracy of the calculated result. |
round: UP|DOWN|CEILING|FLOOR|HALFUP|HALFDOWN|HALFEVEN | One of the rounding variables |
# Examples
log2.716.25 = 2.807035421...
log(1625, 2, 27, 1, 2, HALFUP) # Function returns 281, so the result is: 2.81
log(1625, 2, 27, 1, 5, HALFUP) # Function returns 280703542, so the result is: 2.80704
log(0, 0, 2, 0, 0, HALFUP) # Result: -Infinity
# median
Returns the median of the list. The list can't be empty, otherwise, the function fails.
⚠️ The
medianfunction is added in Standard library version 4.
median(arr: List[Int]): Int
# Parameters
| Parameter | Description |
|---|---|
arr: List[Int] | List of integers |
# Examples
median([1, 2, 3]) # Returns 2
median([2, 4, 9, 20]) # Returns 6
median([-2, -4, -9, -20]) # Returns -7
# pow
Calculates ab.
pow(base: Int, bp: Int, exponent: Int, ep: Int, rp: Int, round: UP|DOWN|CEILING|FLOOR|HALFUP|HALFDOWN|HALFEVEN): Int
In Ride, there is no data type with the floating point. That is why, for example, when you need to calculate, 16.252.7, then the number base = 1625, bp = 2, and the exponent = 27, ep = 1.
If the pow function returns, for example, 18591057, and the parameter of function rp = 4, then the result is 1859.1057; in the number 18591057 the last 4 digits is a fractional part.
# Parameters
| Parameter | Description |
|---|---|
base: Int | Number a without decimal point |
bp: Int | Number of decimals of a |
exponent: Int | Exponent b without decimal point |
ep: Int | Number of decimals of b |
rp: Int | Number of decimals in the resulting value. Specifies the accuracy of the calculated result. The value of the variable can be 0 to 8 integer inclusive |
round: UP|DOWN|CEILING|FLOOR|HALFUP|HALFDOWN|HALFEVEN | One of the rounding variables |
# Examples
16.252.7 = 1859,1057168...
pow(1625, 2, 27, 1, 2, HALFUP) # function returns 185911, so the result is: 1859.11
pow(1625, 2, 27, 1, 5, HALFUP) # function returns 185910572, so, the result is: 1859.10572
# Rounding Variables
Below is the list of built-in rounding variables. Every variable corresponds to the rounding method.
The rounding variables are only used as the parameters of functions log and pow.
| Name | Description |
|---|---|
| CEILING | Rounds towards positive infinity |
| DOWN | Rounds towards zero |
| FLOOR | Rounds towards negative infinity |
| HALFDOWN | Rounds towards the nearest integer; if the integers are equidistant - rounds towards zero |
| HALFEVEN | Rounds towards the nearest integer; if the integers are equidistant - rounds towards the nearest even integer |
| HALFUP | Rounds towards the nearest integer; if the integers are equidistant - rounds away from zero |
| UP | Rounding away from zero |
# Examples
| Input number/Rounding method | UP | DOWN | CEILING | FLOOR | HALFUP | HALFDOWN | HALFEVEN |
|---|---|---|---|---|---|---|---|
| 5.5 | 6 | 5 | 6 | 5 | 6 | 5 | 6 |
| 2.5 | 3 | 2 | 3 | 2 | 3 | 2 | 2 |
| 1.6 | 2 | 1 | 2 | 1 | 2 | 2 | 2 |
| 1.1 | 2 | 1 | 2 | 1 | 1 | 1 | 1 |
| 1.0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| -1.0 | -1 | -1 | -1 | -1 | -1 | -1 | -1 |
| -1.1 | -2 | -1 | -1 | -2 | -1 | -1 | -1 |
| -1.6 | -2 | -1 | -1 | -2 | -2 | -2 | -2 |
| -2.5 | -3 | -2 | -2 | -3 | -3 | -2 | -2 |
| -5.5 | -6 | -5 | -5 | -6 | -6 | -5 | -6 |