kuroko-lang / kuroko

Dialect of Python with explicit variable declaration and block scoping, with a lightweight and easy-to-embed bytecode compiler and interpreter.
https://kuroko-lang.github.io/
MIT License
427 stars 25 forks source link

math functions log1p and expm1 #17

Closed sanjayjain159 closed 2 years ago

sanjayjain159 commented 2 years ago
  1. In kuroko documentation, log1p(x) is defined as log(x) +1. At the same time it is said that this libc math function. This is inconsistent. I would like to point out that in libc log1p(x) is ln(1+x) where ln is the natural logarithm and log1p is used when x <1. When x<<1, there are truncation/roundoff errors in adding a large number 1 to a small floating point number x, and to avoid these a separate function log1p has been provided in libc which is used for small x. There is corresponding assembly instruction in x87 for this function also. So if you are computing log1p as log(1+x) it is inaccurate, and if you are computing log1p as log(x) +1 it is wrong.
  2. There is a corresponding function expm1(x) in libc, which is defined as (e^x -1), which is as necessary as log1p for the same reason for small x<1. The reason is for x<<1 e^x is close to 1 while the difference e^x -1 causes truncation/roundoff errors as before. So the expm1 function has been provided in libc to avoid these errors and there also a instruction to compute it x87 assembly. I request the libc function expm1(x) be added to kuroko.
  3. Having great fun with kuroko! Congrats!
klange commented 2 years ago

log1p was incorrectly bound I think because I misread something in Python documentation. Fixed that and added the binding for expm1.