Skip to content

Commit

Permalink
Fixes #313 and other cases in various Math.* functions where NaN valu…
Browse files Browse the repository at this point in the history
…es were handled differently.
  • Loading branch information
BanzaiMan committed Oct 19, 2012
1 parent 64279e6 commit 2fc5adf
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/org/jruby/RubyMath.java
Expand Up @@ -163,8 +163,10 @@ public static RubyFloat asin(IRubyObject recv, IRubyObject x) {
@JRubyMethod(name = "asin", required = 1, module = true, visibility = Visibility.PRIVATE, compat = CompatVersion.RUBY1_9)
public static RubyFloat asin19(IRubyObject recv, IRubyObject x) {
double value = needFloat(x).getDoubleValue();
if (value < -1.0 || value > 1.0) {
throw recv.getRuntime().newMathDomainError("asin");
}
double result = Math.asin(value);
domainCheck19(recv, result, "asin");
return RubyFloat.newFloat(recv.getRuntime(),result);
}

Expand All @@ -179,8 +181,10 @@ public static RubyFloat acos(IRubyObject recv, IRubyObject x) {
@JRubyMethod(name = "acos", required = 1, module = true, visibility = Visibility.PRIVATE, compat = CompatVersion.RUBY1_9)
public static RubyFloat acos19(IRubyObject recv, IRubyObject x) {
double value = needFloat(x).getDoubleValue();
if (value < -1.0 || value > 1.0) {
throw recv.getRuntime().newMathDomainError("acos");
}
double result = Math.acos(value);
domainCheck19(recv, result, "acos");
return RubyFloat.newFloat(recv.getRuntime(), result);
}

Expand All @@ -193,7 +197,6 @@ public static RubyFloat atan(IRubyObject recv, IRubyObject x) {
@JRubyMethod(name = "atan", required = 1, module = true, visibility = Visibility.PRIVATE, compat = CompatVersion.RUBY1_9)
public static RubyFloat atan19(IRubyObject recv, IRubyObject x) {
double value = needFloat(x).getDoubleValue();
domainCheck19(recv, value, "atan");
return RubyFloat.newFloat(recv.getRuntime(),Math.atan(value));
}

Expand Down Expand Up @@ -254,16 +257,16 @@ public static RubyFloat acosh(IRubyObject recv, IRubyObject x) {
public static RubyFloat acosh19(IRubyObject recv, IRubyObject x) {
double value = needFloat(x).getDoubleValue();
double result;
if (Double.isNaN(value) || value < 1) {
if (Double.isNaN(value)) {
result = Double.NaN;
} else if (value < 1) {
throw recv.getRuntime().newMathDomainError("acosh");
} else if (value < 94906265.62) {
result = Math.log(value + Math.sqrt(value * value - 1.0));
} else{
result = 0.69314718055994530941723212145818 + Math.log(value);
}

domainCheck19(recv, result, "acosh");

return RubyFloat.newFloat(recv.getRuntime(),result);
}

Expand Down Expand Up @@ -418,7 +421,6 @@ private static RubyFloat log_common19(IRubyObject recv, double value, double bas
throw recv.getRuntime().newMathDomainError(msg);
}
double result = Math.log(value)/Math.log(base);
domainCheck(recv, result, msg);
return RubyFloat.newFloat(recv.getRuntime(),result);
}

Expand Down Expand Up @@ -526,6 +528,8 @@ public static RubyFloat hypot(IRubyObject recv, IRubyObject x, IRubyObject y) {
} else if (valueb != 0) {
result = valuea / valueb;
result = Math.abs(valueb) * Math.sqrt(1 + result * result);
} else if (Double.isNaN(valuea) || Double.isNaN(valueb)) {
result = Double.NaN;
} else {
result = 0;
}
Expand All @@ -544,6 +548,8 @@ public static RubyFloat hypot19(IRubyObject recv, IRubyObject x, IRubyObject y)
} else if (valueb != 0) {
result = valuea / valueb;
result = Math.abs(valueb) * Math.sqrt(1 + result * result);
} else if (Double.isNaN(valuea) || Double.isNaN(valueb)) {
result = Double.NaN;
} else {
result = 0;
}
Expand Down Expand Up @@ -652,6 +658,8 @@ public static RubyFloat erf(IRubyObject recv, IRubyObject x) {
result = value * (1 + chebylevSerie(2 * value * value - 1, ERFC_COEF));
} else if (y < 6.013687357) {
result = sign(1 - erfc(recv, RubyFloat.newFloat(recv.getRuntime(),y)).getDoubleValue(), value);
} else if (Double.isNaN(y)) {
result = Double.NaN;
} else {
result = sign(1, value);
}
Expand All @@ -671,6 +679,8 @@ public static RubyFloat erf19(IRubyObject recv, IRubyObject x) {
result = value * (1 + chebylevSerie(2 * value * value - 1, ERFC_COEF));
} else if (y < 6.013687357) {
result = sign(1 - erfc(recv, RubyFloat.newFloat(recv.getRuntime(),y)).getDoubleValue(), value);
} else if (Double.isNaN(y)) {
result = Double.NaN;
} else {
result = sign(1, value);
}
Expand Down

0 comments on commit 2fc5adf

Please sign in to comment.