Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions manifest/pkg_math.lily
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,24 @@ define abs(x: Integer): Integer
### Calculates the arc cosine of a double in radians.
define acos(x: Double): Double

### Calculates the hyperbolic arc cosine of a double in radians.
define acosh(x: Double): Double

### Calculates the arc sine of a double in radians.
define asin(x: Double): Double

### Calculates the hyperbolic arc sine of a double in radians.
define asinh(x: Double): Double

### Calculates the arc tangent of a double in radians.
define atan(x: Double): Double

### Calculates the hyperbolic arc tangent of a double in radians.
define atanh(x: Double): Double

### Calculate the cube root of a double.
define cbrt(x: Double): Double

### Round a double up to the nearest integer.
define ceil(x: Double): Double

Expand All @@ -28,6 +40,9 @@ define cosh(x: Double): Double
### Calculate e^x.
define exp(x: Double): Double

### Calculate 2^x.
define exp2(x: Double): Double

### Calculates the absolute value of a double.
define fabs(x: Double): Double

Expand All @@ -49,15 +64,24 @@ define ldexp(x: Double, y: Integer): Double
### Calculate the log of a double with base e.
define log(x: Double): Double

### Calculate the log of a double with base 2.
define log2(x: Double): Double

### Calculate the log of a double with base 10.
define log10(x: Double): Double

### Split a double into an integer and a fractional part <[ipart, fpart]>.
define modf(x: Double): Tuple[Double, Double]

### Calculate the square roots of x and y then the square root of them added together.
define hypot(x: Double, y: Double): Double

### Calculate x^y.
define pow(x: Double, y: Double): Double

### Round a double to the nearest integer.
define round(x: Double): Double

### Calculate the sine of a double in radians.
define sin(x: Double): Double

Expand Down
57 changes: 57 additions & 0 deletions src/lily_pkg_math.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,48 @@ void lily_math__acos(lily_state *s)
lily_return_double(s, acos(x));
}

void lily_math__acosh(lily_state *s)
{
double x = lily_arg_double(s, 0);

lily_return_double(s, acosh(x));
}

void lily_math__asin(lily_state *s)
{
double x = lily_arg_double(s, 0);

lily_return_double(s, asin(x));
}

void lily_math__asinh(lily_state *s)
{
double x = lily_arg_double(s, 0);

lily_return_double(s, asinh(x));
}

void lily_math__atan(lily_state *s)
{
double x = lily_arg_double(s, 0);

lily_return_double(s, atan(x));
}

void lily_math__atanh(lily_state *s)
{
double x = lily_arg_double(s, 0);

lily_return_double(s, atanh(x));
}

void lily_math__cbrt(lily_state *s)
{
double x = lily_arg_double(s, 0);

lily_return_double(s, cbrt(x));
}

void lily_math__ceil(lily_state *s)
{
double x = lily_arg_double(s, 0);
Expand Down Expand Up @@ -66,6 +94,13 @@ void lily_math__exp(lily_state *s)
lily_return_double(s, exp(x));
}

void lily_math__exp2(lily_state *s)
{
double x = lily_arg_double(s, 0);

lily_return_double(s, exp2(x));
}

void lily_math__fabs(lily_state *s)
{
double x = lily_arg_double(s, 0);
Expand All @@ -88,6 +123,14 @@ void lily_math__fmod(lily_state *s)
lily_return_double(s, fmod(x, y));
}

void lily_math__hypot(lily_state *s)
{
double x = lily_arg_double(s, 0);
double y = lily_arg_double(s, 1);

lily_return_double(s, hypot(x, y));
}

void lily_math__is_infinity(lily_state *s)
{
double x = lily_arg_double(s, 0);
Expand Down Expand Up @@ -117,6 +160,13 @@ void lily_math__log(lily_state *s)
lily_return_double(s, log(x));
}

void lily_math__log2(lily_state *s)
{
double x = lily_arg_double(s, 0);

lily_return_double(s, log2(x));
}

void lily_math__log10(lily_state *s)
{
double x = lily_arg_double(s, 0);
Expand Down Expand Up @@ -148,6 +198,13 @@ void lily_math__pow(lily_state *s)
lily_return_double(s, pow(x, y));
}

void lily_math__round(lily_state *s)
{
double x = lily_arg_double(s, 0);

lily_return_double(s, round(x));
}

void lily_math__sin(lily_state *s)
{
double x = lily_arg_double(s, 0);
Expand Down
8 changes: 8 additions & 0 deletions src/lily_pkg_math_bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,30 @@ lily_call_entry_func lily_math_call_table[] = { \
NULL, \
lily_math__abs, \
lily_math__acos, \
lily_math__acosh, \
lily_math__asin, \
lily_math__asinh, \
lily_math__atan, \
lily_math__atanh, \
lily_math__cbrt, \
lily_math__ceil, \
lily_math__cos, \
lily_math__cosh, \
lily_math__exp, \
lily_math__exp2, \
lily_math__fabs, \
lily_math__floor, \
lily_math__fmod, \
lily_math__hypot, \
lily_math__is_infinity, \
lily_math__is_nan, \
lily_math__ldexp, \
lily_math__log, \
lily_math__log2, \
lily_math__log10, \
lily_math__modf, \
lily_math__pow, \
lily_math__round, \
lily_math__sin, \
lily_math__sinh, \
lily_math__sqrt, \
Expand Down
16 changes: 16 additions & 0 deletions test/prelude/test_pkg_math.lily
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class TestPkgMath < TestCase
assert_near_equal(math.atan(math.pi / 4.0), 0.6657737500283538)
}

public define test_cbrt
{
assert_near_equal(math.cbrt(27.0), 3.0)
}

public define test_ceil
{
assert_near_equal(math.ceil(23.4), 24.0)
Expand Down Expand Up @@ -76,6 +81,11 @@ class TestPkgMath < TestCase
assert_near_equal(math.log(math.exp(1.0)), 1.0)
}

public define test_log2
{
assert_near_equal(math.log2(2.0), 1.0)
}

public define test_log10
{
assert_near_equal(math.log10(10.0), 1.0)
Expand All @@ -98,6 +108,12 @@ class TestPkgMath < TestCase
|> assert_true
}

public define test_round
{
assert_near_equal(math.round(23.4), 23.0)
assert_near_equal(math.round(23.5), 24.0)
}

public define test_sin
{
assert_near_equal(math.sin(math.pi / 2.0), 1.0)
Expand Down