lboasso / oberonc

An Oberon-07 compiler for the JVM
MIT License
147 stars 17 forks source link

Oakwood's Math module #17

Closed ruanjiaxing closed 4 years ago

ruanjiaxing commented 4 years ago

Currently oberonc lacks the Math module. I have implemented a Math module compatible with obnc's using Apache's Common Math library. I use this library because Java Math doesn't offer asinh, acosh, atanh and implementing them myself is erroneous and inefficient, so I use a well known and widely used library. Please consider the possibility of adding them to the stardard distribution of oberonc. Thanks.

lboasso commented 4 years ago

oberonc is just a compiler. I purposefully avoided adding a standard library to keep the dependencies of this project to a minimum. Adding your Math module would require the compiler binary distribution to depend on Apache's Common Math for all the users of this project. After a quick look at the implementation of FastMath.java, it seems like you can simply use the regular math formulas for asinh, acosh and atanh. (albeit this may not give you the best performance).

For example asinh can be implemented as:

  public static float asinh(float x) {
    return (float) Math.log(x + Math.sqrt(x * x + 1));
  }

If change your Math module to depend only on Java's standard library, I will be happy to add it to oberonc.

ruanjiaxing commented 4 years ago

Yes, I originally implement my Math module only using java.lang.Math. But the real trouble is atanh. I implemented atanh like this:

public static float arctanh(float x) { double absAtanh = 0.5*ln((1 + x)/(1 - x)); if(x < 0) return (float) -absAtanh; else return (float) absAtanh; }

But FastMath.java implement atanh very different. I'm not confident with my programming skill so I choose to use their version.

ruanjiaxing commented 4 years ago

If you think my implementation is acceptable I will replace the FastMath version with the pure java.lang.Math version.

lboasso commented 4 years ago

Please be careful not to copy-paste code, although the Apache license of Common Math is permissive, I'll rather include only new code to oberonc. I just tried out these two functions below for some values of x and they produce the same results:

  public static float arctanh_FastMath(float x) {
    return (float) FastMath.atanh(x);
  }

  public static float arctanh(float x) {
    return (float) (0.5 * Math.log((1.0 + x) / (1.0 - x)));
  }

Please update your code and submit a pull request so that I can merge your contribution. Thanks!

ruanjiaxing commented 4 years ago

Nope. Not copy and paste. Originally I named the variable as tmp not absAtanh, but just changed to follow Apache to make it looks more professional. I got the atanh formula from Wiki.

I updated my Math module to use only java.lang.Math.

Sorry. I don't know how to use github. I didn't lie. You see, all of my repos are added by upload! I don't know what a pull request is so the only thing I could do is to point you to my repo where the Math module is already updated. I'm just an amateur/hobbyist. I know some Java with Eclipse and nothing else :grin:

lboasso commented 4 years ago

Thanks, I'll take a look at your Math module in the next days and incorporate it in oberonc's distribution

lboasso commented 4 years ago

Sorry for the late reply, I just committed Add Math.Mod and remove redundant * in DEFINITION modules.