Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/workflows/tarball-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,18 @@ jobs:
BRANCH: master
outputs:
branch: ${{ env.BRANCH }}
skip: ${{ steps.skipping.outputs.skip }}
steps:
- id: skipping
run:
echo 'skip=true' >> $GITHUB_OUTPUT
if: >-
${{(false
|| contains(github.event.head_commit.message, '[DOC]')
|| contains(github.event.pull_request.title, '[DOC]')
|| contains(github.event.pull_request.labels.*.name, 'Documentation')
|| (github.event.pull_request.user.login == 'dependabot[bot]')
)}}
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
fetch-depth: 0
Expand All @@ -55,6 +66,7 @@ jobs:

ubuntu:
needs: tarball
if: ${{ ! needs.tarball.outputs.skip }}
uses: ./.github/workflows/tarball-ubuntu.yml
with:
archname: snapshot-${{ needs.tarball.outputs.branch }}
Expand All @@ -63,6 +75,7 @@ jobs:

macos:
needs: tarball
if: ${{ ! needs.tarball.outputs.skip }}
uses: ./.github/workflows/tarball-macos.yml
with:
archname: snapshot-${{ needs.tarball.outputs.branch }}
Expand All @@ -71,6 +84,7 @@ jobs:

windows:
needs: tarball
if: ${{ ! needs.tarball.outputs.skip }}
uses: ./.github/workflows/tarball-windows.yml
with:
archname: snapshot-${{ needs.tarball.outputs.branch }}
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/tarball-ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ jobs:
[
Dir.home,
].each do |dir|
Pathname(dir).each_child do |pn|
st = pn.stat
Dir.each_child(dir) do |pn|
st = File.stat(pn)
if st.file?
content = Digest::SHA1.hexdigest(pn.read)
content = Digest::SHA1.file(pn).hexdigest
elsif st.directory? && st.nlink <= 10
content = pn.children.map(&:basename).map(&:to_s).sort
content = Dir.children(pn).sort
end
out << [pn.to_s, "%o"%st.mode, st.nlink, st.uid, st.gid, st.size, content].to_s
out << [pn, "%o"%st.mode, st.nlink, st.uid, st.gid, st.size, content].to_s
rescue
out << [pn.to_s, $!.inspect].to_s
out << [pn, $!.inspect].to_s
end
end
File.open(ARGV.shift, "w") do |io|
Expand Down
4 changes: 3 additions & 1 deletion internal/rational.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ VALUE rb_rational_cmp(VALUE self, VALUE other);
VALUE rb_rational_pow(VALUE self, VALUE other);
VALUE rb_rational_floor(VALUE self, int ndigits);
VALUE rb_numeric_quo(VALUE x, VALUE y);
VALUE rb_flo_round_by_rational(int argc, VALUE *argv, VALUE num);
VALUE rb_flo_round_by_rational(VALUE num, int ndigits, enum ruby_num_rounding_mode mode);
VALUE rb_flo_ceil_by_rational(VALUE num, int ndigits);
VALUE rb_flo_floor_by_rational(VALUE num, int ndigits);
VALUE rb_float_numerator(VALUE x);
VALUE rb_float_denominator(VALUE x);

Expand Down
13 changes: 10 additions & 3 deletions numeric.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
#define DBL_EPSILON 2.2204460492503131e-16
#endif

#define ACCURATE_POW10(ndigits) ((ndigits) < DBL_DIG)

#ifndef USE_RB_INFINITY
#elif !defined(WORDS_BIGENDIAN) /* BYTE_ORDER == LITTLE_ENDIAN */
const union bytesequence4_or_float rb_infinity = {{0x00, 0x00, 0x80, 0x7f}};
Expand Down Expand Up @@ -2018,6 +2020,9 @@ rb_float_floor(VALUE num, int ndigits)
if (float_round_overflow(ndigits, binexp)) return num;
if (number > 0.0 && float_round_underflow(ndigits, binexp))
return DBL2NUM(0.0);
if (!ACCURATE_POW10(ndigits)) {
return rb_flo_floor_by_rational(num, ndigits);
}
f = pow(10, ndigits);
mul = floor(number * f);
res = (mul + 1) / f;
Expand Down Expand Up @@ -2226,6 +2231,9 @@ rb_float_ceil(VALUE num, int ndigits)
if (float_round_overflow(ndigits, binexp)) return num;
if (number < 0.0 && float_round_underflow(ndigits, binexp))
return DBL2NUM(0.0);
if (!ACCURATE_POW10(ndigits)) {
return rb_flo_ceil_by_rational(num, ndigits);
}
f = pow(10, ndigits);
f = ceil(number * f) / f;
return DBL2NUM(f);
Expand Down Expand Up @@ -2490,9 +2498,8 @@ flo_round(int argc, VALUE *argv, VALUE num)
frexp(number, &binexp);
if (float_round_overflow(ndigits, binexp)) return num;
if (float_round_underflow(ndigits, binexp)) return DBL2NUM(0);
if (ndigits > 14) {
/* In this case, pow(10, ndigits) may not be accurate. */
return rb_flo_round_by_rational(argc, argv, num);
if (!ACCURATE_POW10(ndigits)) {
return rb_flo_round_by_rational(num, ndigits, mode);
}
f = pow(10, ndigits);
x = ROUND_CALL(mode, round, (number, f));
Expand Down
1 change: 0 additions & 1 deletion process.c
Original file line number Diff line number Diff line change
Expand Up @@ -2888,7 +2888,6 @@ void
rb_execarg_parent_end(VALUE execarg_obj)
{
execarg_parent_end(execarg_obj);
RB_GC_GUARD(execarg_obj);
}

static void
Expand Down
32 changes: 27 additions & 5 deletions rational.c
Original file line number Diff line number Diff line change
Expand Up @@ -1374,10 +1374,12 @@ nurat_round_half_even(VALUE self)
return num;
}

static VALUE f_round_n(VALUE self, VALUE n, VALUE (*func)(VALUE)) ;

static VALUE
f_round_common(int argc, VALUE *argv, VALUE self, VALUE (*func)(VALUE))
{
VALUE n, b, s;
VALUE n;

if (rb_check_arity(argc, 0, 1) == 0)
return (*func)(self);
Expand All @@ -1387,6 +1389,14 @@ f_round_common(int argc, VALUE *argv, VALUE self, VALUE (*func)(VALUE))
if (!k_integer_p(n))
rb_raise(rb_eTypeError, "not an integer");

return f_round_n(self, n, func);
}

static VALUE
f_round_n(VALUE self, VALUE n, VALUE (*func)(VALUE))
{
VALUE b, s;

b = f_expt10(n);
s = rb_rational_mul(self, b);

Expand Down Expand Up @@ -1417,8 +1427,7 @@ rb_rational_floor(VALUE self, int ndigits)
return nurat_floor(self);
}
else {
VALUE n = INT2NUM(ndigits);
return f_round_common(1, &n, self, nurat_floor);
return f_round_n(self, INT2NUM(ndigits), nurat_floor);
}
}

Expand Down Expand Up @@ -1561,9 +1570,22 @@ nurat_round_n(int argc, VALUE *argv, VALUE self)
}

VALUE
rb_flo_round_by_rational(int argc, VALUE *argv, VALUE num)
rb_flo_round_by_rational(VALUE num, int ndigits, enum ruby_num_rounding_mode mode)
{
VALUE (*round_func)(VALUE) = ROUND_FUNC(mode, nurat_round);
return nurat_to_f(f_round_n(float_to_r(num), INT2NUM(ndigits), round_func));
}

VALUE
rb_flo_ceil_by_rational(VALUE num, int ndigits)
{
return nurat_to_f(f_round_n(float_to_r(num), INT2NUM(ndigits), nurat_ceil));
}

VALUE
rb_flo_floor_by_rational(VALUE num, int ndigits)
{
return nurat_to_f(nurat_round_n(argc, argv, float_to_r(num)));
return nurat_to_f(f_round_n(float_to_r(num), INT2NUM(ndigits), nurat_floor));
}

static double
Expand Down
39 changes: 39 additions & 0 deletions test/ruby/test_float.rb
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,22 @@ def (prec = Object.new).to_int; 2; end
assert_equal(-1.26, -1.255.round(2))
end

def test_round_ndigits
bug14635 = "[ruby-core:86323]"
f = 0.5
31.times do |i|
assert_equal(0.5, f.round(i+1), bug14635 + " (argument: #{i+1})")
end
end

def test_round_with_precision_min
(0..3).each do |n|
n -= Float::MIN_10_EXP
f = Float::MIN.round(n)
assert_include([Float::MIN.floor(n), Float::MIN.ceil(n)], f, "round(#{n})")
end
end

def test_round_half_even_with_precision
assert_equal(767573.18759, 767573.1875850001.round(5, half: :even))
assert_equal(767573.18758, 767573.187585.round(5, half: :even))
Expand Down Expand Up @@ -536,6 +552,16 @@ def (prec = Object.new).to_int; 2; end
assert_equal(-100000000000000000000000000000000000000000000000000, -1.0.floor(-50), "[Bug #20654]")
end

def test_floor_with_precision_min
min = Float::MIN
(0..3).each do |n|
n -= Float::MIN_10_EXP
f = min.floor(n)
assert_operator(f, :<=, Float::MIN, "floor(#{n})")
assert_operator(f, :>=, Float::MIN.floor(n-1), "ceil(#{n})")
end
end

def test_ceil_with_precision
assert_equal(+0.1, +0.001.ceil(1))
assert_equal(-0.0, -0.001.ceil(1))
Expand Down Expand Up @@ -567,6 +593,19 @@ def (prec = Object.new).to_int; 2; end
assert_equal(100000000000000000000000000000000000000000000000000, 1.0.ceil(-50), "[Bug #20654]")
end

def test_ceil_with_precision_min
min = Float::MIN
(-Float::MIN_10_EXP).times do |n|
assert_equal(10.pow(-n), min.ceil(n))
end
(0..3).each do |n|
n -= Float::MIN_10_EXP
f = min.ceil(n)
assert_operator(f, :>=, Float::MIN, "ceil(#{n})")
assert_operator(f, :<=, Float::MIN.ceil(n-1), "ceil(#{n})")
end
end

def test_truncate_with_precision
assert_equal(1.100, 1.111.truncate(1))
assert_equal(1.110, 1.111.truncate(2))
Expand Down
8 changes: 0 additions & 8 deletions test/ruby/test_numeric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,6 @@ def test_cmp
assert_nil(a <=> :foo)
end

def test_float_round_ndigits
bug14635 = "[ruby-core:86323]"
f = 0.5
31.times do |i|
assert_equal(0.5, f.round(i+1), bug14635 + " (argument: #{i+1})")
end
end

def test_floor_ceil_round_truncate
a = Class.new(Numeric) do
def to_f; 1.5; end
Expand Down