Skip to content

Commit

Permalink
Showing 3 changed files with 17 additions and 16 deletions.
24 changes: 16 additions & 8 deletions opal/corelib/number.rb
Original file line number Diff line number Diff line change
@@ -503,24 +503,32 @@ def round(ndigits = undefined)
return self
end

if Float === ndigits && ndigits.infinite?
raise RangeError, "Infinity"
end

ndigits = Opal.coerce_to!(ndigits, Integer, :to_int)

if ndigits < Integer::MIN
raise RangeError, "out of bounds"
end

if `ndigits >= 0`
return self
end

ndigits = -ndigits;

if `0.415241 * ndigits - 0.125 > #{size}`
return 0
end
%x{
if (0.415241 * ndigits - 0.125 > #{size}) {
return 0;
}
f = 10 ** ndigits
x = self < 0 ? -self : self
x = (x + f / 2) / f * f
x = -x if self < 0
var f = Math.pow(10, ndigits),
x = Math.floor((Math.abs(x) + f / 2) / f) * f;
x
return self < 0 ? -x : x;
}
else
if nan? && `ndigits == null`
raise FloatDomainError, "NaN"
8 changes: 0 additions & 8 deletions spec/filters/bugs/integer.rb
Original file line number Diff line number Diff line change
@@ -32,12 +32,4 @@
fails "Integer#gcdlcm returns an Array"
fails "Integer#gcdlcm returns the greatest common divisor of self and argument as the first element"
fails "Integer#gcdlcm returns the least common multiple of self and argument as the last element"
fails "Integer#round calls #to_int on the argument to convert it to an Integer"
fails "Integer#round raises a RangeError when passed Float::INFINITY"
fails "Integer#round raises a RangeError when passed a beyond signed int"
fails "Integer#round raises a TypeError when #to_int does not return an Integer"
fails "Integer#round raises a TypeError when its argument cannot be converted to an Integer"
fails "Integer#round raises a TypeError when passed a String"
fails "Integer#round returns itself rounded if passed a negative value"
fails "Integer#round rounds itself as a float if passed a positive precision"
end
1 change: 1 addition & 0 deletions spec/filters/unsupported/bignum.rb
Original file line number Diff line number Diff line change
@@ -51,4 +51,5 @@

fails "Float#fdiv performs floating-point division between self and a Bignum"
fails "Float#round returns rounded values for big values"
fails "Integer#round returns itself rounded if passed a negative value"
end

0 comments on commit 349cc51

Please sign in to comment.