Skip to content

Commit

Permalink
math lib (#217)
Browse files Browse the repository at this point in the history
Co-authored-by: Akuli <[email protected]>
  • Loading branch information
littlewhitecloud and Akuli authored Feb 12, 2023
1 parent ef849db commit 6128470
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
40 changes: 40 additions & 0 deletions stdlib/math.jou
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
declare abs(x: int) -> int
declare llabs(x: long) -> long
declare fabs(x: double) -> double

declare ceil(x: double) -> double
declare floor(x: double) -> double
declare round(x: double) -> double

declare acos(x: double) -> double
declare asin(x: double) -> double
declare atan(x: double) -> double
declare atan2(y: double, x: double) -> double
declare cos(x: double) -> double
declare sin(x: double) -> double
declare tan(x: double) -> double

declare fmod(x: double, y: double) -> double
declare sqrt(x: double) -> double
declare cbrt(x: double) -> double

declare log(x: double) -> double
declare log10(x: double) -> double
declare log2(x: double) -> double

declare exp(x: double) -> double
declare exp2(x: double) -> double
declare pow(x: double, y: double) -> double
declare ldexp(x: double, exp: int) -> double

declare fmax(x: double, y: double) -> double
declare fmin(x: double, y: double) -> double

declare hypot(x: double, y: double) -> double

# These function are not support yet...
# syntax error
# declare modf(value: double, *iptr: double) -> double
# declare frexp(value: double, *exp: int) -> double
# declare poly(x: double, degree: int, coeffs[]: double) -> double
# declare matherr(struct exception *e)
34 changes: 34 additions & 0 deletions tests/should_succeed/mathlibtest.jou
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from "stdlib/math.jou" import (
abs, llabs, fabs, ceil, floor, round, acos, asin, atan, atan2, cos, sin, tan,
fmod, sqrt, cbrt, log, log10, log2, exp, exp2, pow, ldexp, fmax, fmin, hypot,
)
from "stdlib/io.jou" import printf

def main() -> int:
printf("%d\n", abs(-1)) # Output: 1
printf("%lld\n", llabs(-1145141919180L)) # Output: 1145141919180
printf("%f\n", fabs(-114.514)) # Output: 114.514000
printf("%f\n", ceil(3.14159265358979)) # Output: 4.000000
printf("%f\n", floor(3.14159265358979)) # Output: 3.000000
printf("%f\n", round(3.14159265358979)) # Output: 3.000000
printf("%f\n", acos(-1)) # Output: 3.141593
printf("%f\n", asin(-1)) # Output: -1.570796
printf("%f\n", atan(-1)) # Output: -0.785398
printf("%f\n", atan2(-1, -1)) # Output: -2.356194
printf("%f\n", cos(1.04719753333333)) # Output: 0.500000
printf("%f\n", sin(0.785398149999999)) # Output: 0.707107
printf("%f\n", tan(2.45)) # Output: -0.828017
printf("%f\n", fmod(9.3, 3.1)) # Output: 0.000000
printf("%f\n", sqrt(5)) # Output: 2.236068
printf("%f\n", cbrt(27)) # Output: 3.000000
printf("%f\n", log(114)) # Output: 4.736198
printf("%f\n", log10(114)) # Output: 2.056905
printf("%f\n", log2(114)) # Output: 6.832890
printf("%f\n", exp(3)) # Output: 20.085537
printf("%f\n", exp2(3)) # Output: 8.000000
printf("%f\n", pow(2, 2)) # Output: 4.000000
printf("%f\n", ldexp(10, 2)) # Output: 40.000000
printf("%f\n", fmax(1.14, 5.14)) # Output: 5.140000
printf("%f\n", fmin(1.14, 5.14)) # Output: 1.140000
printf("%f\n", hypot(3, 4)) # Output: 5.000000
return 0

0 comments on commit 6128470

Please sign in to comment.