plasma-umass / doppio

Breaks the browser language barrier (includes a plugin-free JVM).
http://plasma-umass.github.io/doppio-demo
MIT License
2.16k stars 175 forks source link

implementing sinh.h test problem #412

Closed ghost closed 8 years ago

ghost commented 8 years ago

I have just tried to implement Math.sinh but when i ran tests it's giving me problem

public static 'sinh(D)D'(thread: JVMThread, d_val: number): number {
var exp = Math.exp(d_val);
return (exp - 1 / exp) / 2;
}
Doppio                        JAVA
01. 4.120155658429      4.120155658429
02. -12.95350242677    -12.95350242677
03. 0.000000000000      0.000000000000
04. 10.01787492741      10.01787492741
05. 27.28991719713      27.28991719713
06. Infinity                      Infinity 
07. 0.000000000000     4.900000000000e-324 
08. 0.000000000000      2.225073858507e-308 
09. Infinity                      Infinity 
10. -Infinity                     -Infinity
11. Infinity                     Infinity
12. -Infinity                  -Infinity

point 7,8 value mismatch in doppio and java i have not much experience with these precision type details but if any one can suggest how to solve this problem or from where to get help or any clue how to solve this. Thanks

jvilk commented 8 years ago

This sort of thing happens frequently; I've backpedaled on this unit test a number of times due to precision errors.

Does using the first polyfill of Math.sinh here work, which makes two Math.expcalls?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sinh

If not, then we can look into relaxing this unit test, or modifying it to only look at a certain number of sig figs.

Thanks for looking into this!

ghost commented 8 years ago

I have tried both polyfils and they both have same issue. so what should we do ?

ghost commented 8 years ago

when i tried taylor method its working in browser for maximum value and passing tests. but at end test are not going to finish. console looks like that it is freezed public static 'sinh(D)D'(thread: JVMThread, d_val: number): number { var returning = d_val, xToN = dval, factorial = 1, index = 1, nextTerm = 1; while ( nextTerm != 0 ) { index++; factorial = index; index++; factorial = index; xToN = d_val_d_val; nextTerm = xToN/factorial; returning += nextTerm; }

          return returning;
        }
jvilk commented 8 years ago

Yeah, definitely don't do anything crazy. Let me look into it to see if I can restrict Java to printing only a specific number of sigfigs.

ghost commented 8 years ago

Thanks :) Actually these two sig figs tests are problem for sinh, tanh, expm1, log1p because there polyfill uses Math.exp which eventualy results to 1 ...

jvilk commented 8 years ago

@owaishanif786 I looked into it. You should just add checks in your loop to continue when the value is Double.MAX_EXPONENT or Double.MIN_EXPONENT. It looks like those are causing your issues, which makes sense given the use of Math.exp in the polyfill. :) (We already do this for a few functions in StrictMathTest.java)

Also, you should implement a polyfill for Math.sinh in src/util.ts, just after Math.imul. Then, define the native using Math.sinh. We want to use the browser's version of this function if available, since it will be faster (and probably more accurate on these corner cases).

Once you've done that and the tests pass, please open a PR with your change!

jvilk commented 8 years ago

Your code is merged, so this is all set. Thanks for your contribution!