Ph0enixKM / Amber

💎 Amber the programming language compiled to bash
https://amber-lang.com
GNU General Public License v3.0
3.53k stars 70 forks source link

Add - Math module for std lib #180

Open EvanPerreau opened 2 weeks ago

EvanPerreau commented 2 weeks ago

Rounding

  • ceil(x: Num): Num - rounds up to the nearest integer
  • floor(x: Num): Num - rounds down to the nearest
  • round(x: Num): Num - rounds to the nearest integer

Absolute and sign

  • abs(x: Num): Num - absolute value
  • sign(x: Num): Num - sign function, returns -1, 0, or 1 depending on the sign of x

Min / Max

  • min(a: Num, b: Num): Num
  • max(a: Num, b: Num): Num

Power, root, exponential, logarithms and trigonometric

These can be easily evaluated using bc with flag -l

  • pow(base: Num, exponent: Num): Num - power function
  • sqrt(x: Num): Num - square root
  • exp(x: Num): Num - exponential function
  • log(x: Num): Num - natural logarithm
  • log10(x: Num): Num - base-10 logarithm
  • sin(x: Num): Num - sine
  • cos(x: Num): Num - cosine
  • tan(x: Num): Num - tangent
EvanPerreau commented 2 weeks ago

It's because actually, the pull request Split stdlib into submodules isn't merge, for unit test, I need to put all functions in main.ab, but for help us to split stdlib in future, I also put all functions in math.ab

b1ek commented 2 weeks ago

then its probably better to wait for a decicion on #152 and then merge this one

EvanPerreau commented 2 weeks ago

yes, ok !

UrbanCoffee commented 2 weeks ago

Hello. I just wanted to add that the implementations of floor and ceil are not entirely correct. They return the wrong value for negative inputs. Additionally, if the input x is an integer, ceil returns x+1 rather than just x.

Ph0enixKM commented 2 weeks ago

We'll have to write a separate file for testing the math library. We need a lot of good tests to keep this working

b1ek commented 2 weeks ago

We'll have to write a separate file for testing the math library. We need a lot of good tests to keep this working

that should probably be as a submod to stlib.rs, like stdlib/math.rs ig

EvanPerreau commented 2 weeks ago

Hello. I just wanted to add that the implementations of floor and ceil are not entirely correct. They return the wrong value for negative inputs. Additionally, if the input x is an integer, ceil returns x+1 rather than just x.

Hello, I fix this bug :)