bahaghari/lib: add math.exp

This commit is contained in:
Gabriel Arazas 2024-06-03 16:14:06 +08:00
parent 5f07c5b0fc
commit a5ad6af3f0
No known key found for this signature in database
GPG Key ID: 62104B43D00AA360
2 changed files with 42 additions and 3 deletions

View File

@ -8,8 +8,15 @@ rec {
# rest of the decimal place changing functions here for consistency.
inherit (builtins) floor ceil;
constants = {
pi = 3.141592653589793238462643383279502884197;
e = 2.7182818284590452353602874713527;
ln10 = 2.302585092994046;
ln2 = 0.6931471805599453;
# The minimum precision for our functions that need them.
epsilon = pow 10 (-13);
};
/* Returns the absolute value of the given number.
@ -48,6 +55,23 @@ rec {
in
if exponent < 0 then (1 / value) else value;
/* Given a number as x, return e^x.
Type: exp :: Number -> Number
Example:
exp 0
=> 1
exp 1
=> 2.7182818284590452353602874713527
exp -1
=> 0.36787944117144233
*/
exp = x:
pow constants.e x;
/* Given a number, find its square root. This method is implemented using
Newton's method.
@ -70,7 +94,7 @@ rec {
# Changing this value can change the result drastically. A value of
# 10^-13 for tolerance seems to be the most balanced so far since we are
# dealing with floats and should be enough for most cases.
tolerance = pow 10 (-13);
tolerance = constants.epsilon;
iter = value:
let

View File

@ -202,4 +202,19 @@ lib.runTests {
expr = self.math.round' (-10) (self.math.sqrt 2);
expected = 1.4142135624;
};
testMathExp = {
expr = self.math.exp 1;
expected = 2.7182818284590452353602874713527;
};
testMathExp2 = {
expr = self.math.exp (-1);
expected = 0.36787944117144233;
};
testMathExp3 = {
expr = self.math.round' (-10) (self.math.exp 2);
expected = 7.3890560989;
};
}