Skip to content

Commit

Permalink
More mathlib (#387)
Browse files Browse the repository at this point in the history
Co-authored-by: Akuli <[email protected]>
  • Loading branch information
littlewhitecloud and Akuli authored Apr 22, 2023
1 parent 8b6454b commit e0d9568
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 53 deletions.
1 change: 1 addition & 0 deletions self_hosted/main.jou
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ class Compiler:
sprintf(command, "\"%s\" -o \"%s\"", c_compiler, exe)
for i = 0; object_files[i] != NULL; i++:
sprintf(&command[strlen(command)], " \"%s\"", object_files[i])
strcat(command, " -lm")

if is_windows():
# windows strips outermost quotes for some reason, so let's quote it all one more time...
Expand Down
29 changes: 14 additions & 15 deletions self_hosted/runs_wrong.txt
Original file line number Diff line number Diff line change
@@ -1,45 +1,44 @@
# This is a list of files that don't behave correctly when ran with the self-hosted compiler.
stdlib/errno.jou
tests/404/indirect_import_symbol.jou
tests/crash/null_deref.jou
tests/other_errors/address_of_minusminus.jou
tests/other_errors/array_literal_as_a_pointer.jou
tests/other_errors/array0.jou
tests/other_errors/assert_fail.jou
tests/other_errors/assert_fail_multiline.jou
tests/other_errors/instantiation_address_of_field.jou
tests/other_errors/missing_return.jou
tests/other_errors/missing_value_in_return.jou
tests/other_errors/noreturn_but_return_with_value.jou
tests/other_errors/noreturn_but_return_without_value.jou
tests/other_errors/runtime_return_1.jou
tests/other_errors/var_shadow.jou
tests/should_succeed/and_or_not.jou
tests/should_succeed/as.jou
tests/should_succeed/compiler_cli.jou
tests/should_succeed/errno_test.jou
tests/should_succeed/file.jou
tests/should_succeed/global_bug.jou
tests/should_succeed/global.jou
tests/should_succeed/global_bug.jou
tests/should_succeed/if_elif_else.jou
tests/should_succeed/implicit_conversions.jou
tests/should_succeed/imported/point_factory.jou
tests/should_succeed/indirect_method_import.jou
tests/should_succeed/linked_list.jou
tests/should_succeed/local_import.jou
tests/should_succeed/loops.jou
tests/should_succeed/mathlibtest.jou
tests/should_succeed/plusplus_minusminus.jou
tests/should_succeed/pointer.jou
tests/should_succeed/printf.jou
tests/should_succeed/return_void.jou
tests/should_succeed/sizeof.jou
tests/should_succeed/stderr.jou
tests/should_succeed/undefined_value_warning.jou
tests/should_succeed/union.jou
tests/should_succeed/unreachable_warning.jou
tests/should_succeed/unused_import.jou
tests/syntax_error/bad_addressof.jou
tests/wrong_type/assert.jou
tests/wrong_type/cannot_be_indexed.jou
tests/wrong_type/index.jou
tests/should_succeed/imported/point_factory.jou
tests/should_succeed/indirect_method_import.jou
tests/404/indirect_import_symbol.jou
tests/other_errors/noreturn_but_return_without_value.jou
tests/other_errors/noreturn_but_return_with_value.jou
tests/other_errors/assert_fail.jou
tests/wrong_type/assert.jou
tests/should_succeed/union.jou
tests/other_errors/instantiation_address_of_field.jou
tests/other_errors/array_literal_as_a_pointer.jou
tests/other_errors/assert_fail_multiline.jou
stdlib/errno.jou
tests/should_succeed/errno_test.jou
75 changes: 54 additions & 21 deletions stdlib/math.jou
Original file line number Diff line number Diff line change
@@ -1,40 +1,73 @@
# Math lib
# Some math functions

# Other functions
# Returns the absolute value of x: |x|.
declare abs(x: int) -> int
declare llabs(x: long) -> long
declare fabs(x: double) -> double
declare llabs(x: long) -> long

# Rounding and remainder functions
# Rounds x upward, returning the smallest integral value that is not less than x.
declare ceil(x: double) -> double
# Rounds x downward, returning the largest integral value that is not greater than x.
declare floor(x: double) -> double
# Returns the integral value that is nearest to x, with halfway cases rounded away from zero.
declare round(x: double) -> double

# Trigonometric functions
# These functions use radians as the angle unit.
# One radian is the angle of a circle slice with equal radius and arc length, about 57 degrees.
# A full turn (360 degrees) is 2pi (about 6.28) radians.
declare cos(x: double) -> double
declare sin(x: double) -> double
declare tan(x: double) -> double
# The 'a' prefixed functions are inverse functions.
# For example, asin is also known as arcsin and sin^-1.
declare acos(x: double) -> double
declare asin(x: double) -> double
declare atan(x: double) -> double
# Returns the angle of a point (x, y). Note the reversed order of the arguments.
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
# Use nan("") to get a quiet NaN (Not-A-Number) value.
# The argument must be an empty string.
declare nan(tagp: byte*) -> double

declare log(x: double) -> double
declare log10(x: double) -> double
declare log2(x: double) -> double
# Hyperbolic versions of the trig functions
declare cosh(x: double) -> double
declare sinh(x: double) -> double
declare tanh(x: double) -> double
declare acosh(x: double) -> double
declare asinh(x: double) -> double
declare atanh(x: double) -> double

# Exponential and logarithmic functions
# returns e^x
declare exp(x: double) -> double
# returns 2^x
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
# Returns the natural logarithm of x.
declare log(x: double) -> double
declare log2(x: double) -> double
declare log10(x: double) -> double

# Power functions
# Raise to power.
declare pow(x: double, y: double) -> double
# Returns the square root of x.
declare sqrt(x: double) -> double
# Returns the cubic root of x.
declare cbrt(x: double) -> double
# Returns the hypotenuse of a right-angled triangle whose legs are x and y.
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)
# Error and gamma functions
# Returns the error function value for x
declare erf(x: double) -> double
# Returns the complementary error function value for x.
declare erfc(x: double) -> double
# Returns the gamma function of x.
declare tgamma(x: double) -> double
# Returns the natural logarithm of the absolute value of the gamma.
declare lgamma(x: double) -> double
49 changes: 32 additions & 17 deletions tests/should_succeed/mathlibtest.jou
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,42 @@ 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
printf("%f\n", acos(-1.0)) # Output: 3.141593
printf("%f\n", asin(-1.0)) # Output: -1.570796
printf("%f\n", atan(-1.0)) # Output: -0.785398
printf("%f\n", atan2(-1.0, -1.0)) # Output: -2.356194

printf("%f\n", cosh(3.0)) # Output: 10.067662
printf("%f\n", sinh(4.0)) # Output: 27.289917
printf("%f\n", tanh(0.59)) # Output: 0.529896
printf("%f\n", acosh(13.21)) # Output: 3.272686
printf("%f\n", asinh(0.5)) # Output: 0.481212
printf("%f\n", atanh(-0.50)) # Output: -0.549306

printf("%f\n", exp(3.0)) # Output: 20.085537
printf("%f\n", exp2(3.0)) # Output: 8.000000

printf("%f\n", log(114.0)) # Output: 4.736198
printf("%f\n", log2(114.0)) # Output: 6.832890
printf("%f\n", log10(114.0)) # Output: 2.056905

printf("%f\n", pow(2.0, 2.0)) # Output: 4.000000
printf("%f\n", sqrt(5.0)) # Output: 2.236068
printf("%f\n", cbrt(27.0)) # Output: 3.000000
printf("%f\n", hypot(3.0, 4.0)) # Output: 5.000000

printf("%f\n", erf(1.0)) # Output: 0.842701
printf("%f\n", erfc(1.0)) # Output: 0.157299
printf("%f\n", tgamma(6.0)) # Output: 120.000000
printf("%f\n", lgamma(6.0)) # Output: 4.787492

return 0

0 comments on commit e0d9568

Please sign in to comment.