Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: cb6a0a60b58e
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 64e91a69bcac
Choose a head ref
  • 2 commits
  • 5 files changed
  • 1 contributor

Commits on Aug 19, 2015

  1. Copy the full SHA
    4d6bfdd View commit details
  2. Fix Number#round

    meh committed Aug 19, 2015
    Copy the full SHA
    64e91a6 View commit details
Showing with 65 additions and 26 deletions.
  1. +55 −5 opal/corelib/number.rb
  2. +0 −9 spec/filters/bugs/fixnum.rb
  3. +0 −12 spec/filters/bugs/float.rb
  4. +7 −0 spec/filters/unsupported/bignum.rb
  5. +3 −0 spec/filters/unsupported/float.rb
60 changes: 55 additions & 5 deletions opal/corelib/number.rb
Original file line number Diff line number Diff line change
@@ -497,11 +497,61 @@ def rationalize(eps = undefined)
end
end

def round(ndigits=0)
%x{
var scale = Math.pow(10, ndigits);
return Math.round(self * scale) / scale;
}
def round(ndigits = undefined)
if Integer === self
if `ndigits == null`
return self
end

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

if `ndigits >= 0`
return self
end

ndigits = -ndigits;

if `0.415241 * ndigits - 0.125 > #{size}`
return 0
end

f = 10 ** ndigits
x = self < 0 ? -self : self
x = (x + f / 2) / f * f
x = -x if self < 0

x
else
if nan? && `ndigits == null`
raise FloatDomainError, "NaN"
end

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

if ndigits <= 0
if nan?
raise RangeError, "NaN"
elsif infinite?
raise FloatDomainError, "Infinity"
end
elsif ndigits == 0
return `Math.round(self)`
elsif nan? || infinite?
return self
end

_, exp = Math.frexp(self)

if ndigits >= (Float::DIG + 2) - (exp > 0 ? exp / 4 : exp / 3 - 1)
return self
end

if ndigits < -(exp > 0 ? exp / 3 + 1 : exp / 4)
return 0
end

`Math.round(self * Math.pow(10, ndigits)) / Math.pow(10, ndigits)`
end
end

def step(limit, step = 1, &block)
9 changes: 0 additions & 9 deletions spec/filters/bugs/fixnum.rb

This file was deleted.

12 changes: 0 additions & 12 deletions spec/filters/bugs/float.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
opal_filter "Float" do
fails "Float#fdiv performs floating-point division between self and a Bignum"
fails "Float#fdiv performs floating-point division between self and a Complex"
fails "Float#fdiv performs floating-point division between self and a Rational"
fails "Float#round raises FloatDomainError for exceptional values when passed a non-positive precision"
fails "Float#round raises FloatDomainError for exceptional values"
fails "Float#round raises RangeError for NAN when passed a non-positive precision"
fails "Float#round raises a TypeError when its argument can not be converted to an Integer"
fails "Float#round returns rounded values for big argument"
fails "Float#round returns rounded values for big values"
fails "Float#round returns the nearest Integer"
fails "Float#round rounds self to an optionally given precision"
fails "Float#round works for corner cases"
fails "Float#to_i returns self truncated to an Integer"
fails "Float#to_int returns self truncated to an Integer"
fails "Float#to_s uses e format for a negative value with fractional part having 6 significant figures"
7 changes: 7 additions & 0 deletions spec/filters/unsupported/bignum.rb
Original file line number Diff line number Diff line change
@@ -44,4 +44,11 @@
fails "Fixnum#* overflows to Bignum when the result does not fit in Fixnum"
fails "Fixnum#** can raise -1 to a Bignum safely"
fails "Fixnum#^ returns self bitwise EXCLUSIVE OR a Bignum"
fails "Fixnum#/ coerces fixnum and return self divided by other"
fails "Fixnum#^ returns self bitwise EXCLUSIVE OR other"
fails "Fixnum#| returns self bitwise OR other"
fails "Fixnum#& returns self bitwise AND other"

fails "Float#fdiv performs floating-point division between self and a Bignum"
fails "Float#round returns rounded values for big values"
end
3 changes: 3 additions & 0 deletions spec/filters/unsupported/float.rb
Original file line number Diff line number Diff line change
@@ -42,6 +42,9 @@
fails "Fixnum#coerce when given a String returns an array containing two Floats"
fails "Fixnum#div coerces self and the given argument to Floats and returns self divided by other as Fixnum"
fails "Fixnum#| raises a TypeError when passed a Float"
fails "Fixnum#/ returns self divided by the given argument"
fails "Fixnum#/ raises a ZeroDivisionError if the given argument is zero and not a Float"
fails "Fixnum#/ supports dividing negative numbers"

# precision error
fails "Math.gamma returns approximately (n-1)! given n for n between 24 and 30"