907 Commits

Author SHA1 Message Date
Yusuke Endoh
3a7b9ca93b Fix Integer.sqrt to never exceed actual value
`Integer.sqrt` uses `sqrt(3)` from libm for small values.
This method must return a value less than or equal to the actual integer
square root, but libm's sqrt does not always guarantee that.

This change corrects that by decrementing the result if necessary.

Fixes [Bug #21217]
2025-04-07 11:08:10 +09:00
Nobuyoshi Nakada
9394d2084c
[DOC] Fix indentation
Not to make preceding paragraphs a code block.
2025-02-27 13:00:34 +09:00
Nobuyoshi Nakada
e433e6515e
[DOC] Exclude 'Class' and 'Module' from RDoc's autolinking 2025-01-02 12:36:06 +09:00
Nobuyoshi Nakada
b4ec22fe6c
[DOC] Exclude 'Method' from RDoc's autolinking 2025-01-02 12:23:49 +09:00
BurdetteLamar
9715131c32 [DOC] Fix example in Numeric#to_int 2024-12-17 13:01:39 -05:00
BurdetteLamar
d588a1c880 Correct order of listed methods 2024-12-02 15:28:53 -05:00
Takashi Kokubun
0f3723c644
Rewrite Numeric#dup and Numeric#+@ in Ruby (#11933) 2024-10-22 11:01:29 -07:00
Nobuyoshi Nakada
66b69d4ea5
[DOC] Move each Float#to_s example item by item
Like as +half+ option in Float#round.

If the body of the preceding list items and the following code block
are indented at the same position, the code block is parsed as a list
continuation.

[Fixes GH-11683]
2024-10-07 18:22:19 +09:00
BurdetteLamar
5df574d26e [DOC] Fix links 2024-09-10 14:04:41 -04:00
Peter Zhu
a7167d0cee Fix ceil when ndigits is large
[Bug #20654]

This commit fixes Integer#ceil and Float#ceil when the number is
negative and ndigits is large such that 10**ndigits is a bignum.

Previously, it would return 0 in such cases. However, this would cause
unexpected behaviour such as:

    puts 1.ceil(-5) # => 100000
    puts 1.ceil(-10) # => 10000000000
    puts 1.ceil(-20) # => 0

This commit changes the last result so that it will return
100000000000000000000.
2024-07-30 08:21:28 -04:00
Peter Zhu
3af2a7fbe1 Fix floor when ndigits is large
[Bug #20654]

This commit fixes Integer#floor and Float#floor when the number is
negative and ndigits is large such that 10**ndigits is a bignum.

Previously, it would return 0 in such cases. However, this would cause
unexpected behaviour such as:

    puts -1.floor(-5) # => -100000
    puts -1.floor(-10) # => -10000000000
    puts -1.floor(-20) # => 0

This commit changes the last result so that it will return
-100000000000000000000.
2024-07-30 08:21:28 -04:00
Aaron Patterson
2c1655314a Revert moving things to Ruby
This is slowing down benchmarks on x86, so lets revert it for now.
2024-07-29 14:18:11 -07:00
BurdetteLamar
aaa542d894 Doc for some #ceil and #floor 2024-07-26 11:22:18 -04:00
BurdetteLamar
78f1b835a0 Doc for some #ceil and #floor 2024-07-26 11:22:18 -04:00
Burdette Lamar
30b9912bb7
[DOC] Doc for Float#ceil (#11125) 2024-07-09 09:43:07 -04:00
Peter Zhu
ab3fa8dece [DOC] Use backticks instead of HTML tags 2024-07-09 08:55:58 -04:00
Peter Zhu
5de6d0b35f [DOC] Fix granularity calculation
The granularity is calculated as `10 ** ndigits.abs` rather than
`ndigits.abs * 10`. For example, if `ndigits` is `-2`, it should be
`10 ** 2 == 100` rather than `2 * 10 == 20`.
2024-07-09 08:55:58 -04:00
BurdetteLamar
a57b4340d0 Doc fixes 2024-07-08 14:35:29 -04:00
Burdette Lamar
f5dfadf38b
[DOC] Doc for Integer#floor (#11077) 2024-07-03 16:00:00 -04:00
Aaron Patterson
f4b313f733 move Integer#downto to Ruby
Speeds up ChunkyPNG.

The interpreter is about 70% faster:

```
before: ruby 3.4.0dev (2024-07-03T15:16:17Z master 786cf9db48) [arm64-darwin23]
after: ruby 3.4.0dev (2024-07-03T15:32:25Z ruby-downto 0b8b744ce2) [arm64-darwin23]

----------  -----------  ----------  ----------  ----------  -------------  ------------
bench       before (ms)  stddev (%)  after (ms)  stddev (%)  after 1st itr  before/after
chunky-png  892.2        0.1         526.3       1.0         1.65           1.70
----------  -----------  ----------  ----------  ----------  -------------  ------------
Legend:
- after 1st itr: ratio of before/after time for the first benchmarking iteration.
- before/after: ratio of before/after time. Higher is better for after. Above 1 represents a speedup.
```

YJIT is 2.5x faster:

```
before: ruby 3.4.0dev (2024-07-03T15:16:17Z master 786cf9db48) +YJIT [arm64-darwin23]
after: ruby 3.4.0dev (2024-07-03T15:32:25Z ruby-downto 0b8b744ce2) +YJIT [arm64-darwin23]

----------  -----------  ----------  ----------  ----------  -------------  ------------
bench       before (ms)  stddev (%)  after (ms)  stddev (%)  after 1st itr  before/after
chunky-png  709.4        0.1         278.8       0.3         2.35           2.54
----------  -----------  ----------  ----------  ----------  -------------  ------------
Legend:
- after 1st itr: ratio of before/after time for the first benchmarking iteration.
- before/after: ratio of before/after time. Higher is better for after. Above 1 represents a speedup.
```
2024-07-03 10:01:41 -07:00
BurdetteLamar
9930647134 Doc for Integer#ceil 2024-06-29 10:33:54 -04:00
Artur
dd578cf2f1
[DOC] Enhance Numeric#nonzero? doc
Add `zero?` as a related method
2024-04-26 12:33:41 +00:00
Reznov
19f4b06b9d
Reducing the number of divisions in rb_fix_digits 2024-04-08 12:51:32 +09:00
Peter Zhu
9ad175c1ee Register rb_fix_to_s_static as global right after creating
If a GC runs right during creating a rb_fix_to_s_static, it may cause
the previous ones to become swept by the GC because they have not been
registered by rb_vm_register_global_object.
2024-03-27 09:39:23 -04:00
tompng
0ff2c7fe6f Faster Integer.sqrt for large bignum
Integer.sqrt uses Newton's method.
This pull request reduces the precision which was unnecessarily high in each calculation step.
2024-03-18 13:52:27 +09:00
Jean Boussier
d4f3dcf4df Refactor VM root modules
This `st_table` is used to both mark and pin classes
defined from the C API. But `vm->mark_object_ary` already
does both much more efficiently.

Currently a Ruby process starts with 252 rooted classes,
which uses `7224B` in an `st_table` or `2016B` in an `RArray`.

So a baseline of 5kB saved, but since `mark_object_ary` is
preallocated with `1024` slots but only use `405` of them,
it's a net `7kB` save.

`vm->mark_object_ary` is also being refactored.

Prior to this changes, `mark_object_ary` was a regular `RArray`, but
since this allows for references to be moved, it was marked a second
time from `rb_vm_mark()` to pin these objects.

This has the detrimental effect of marking these references on every
minors even though it's a mostly append only list.

But using a custom TypedData we can save from having to mark
all the references on minor GC runs.

Addtionally, immediate values are now ignored and not appended
to `vm->mark_object_ary` as it's just wasted space.
2024-03-06 15:33:43 -05:00
Nobuyoshi Nakada
3dccb716da
Use defined?(yield) and SIZED_ENUMERATOR
Prefer built-in features over method calls that may be overridden.
2024-02-17 23:28:00 +09:00
Yusuke Endoh
25d74b9527 Do not include a backtick in error messages and backtraces
[Feature #16495]
2024-02-15 18:42:31 +09:00
Peter Zhu
fd87259a26 Replace assert with RUBY_ASSERT in numeric.c
assert does not print the bug report, only the file and line number of
the assertion that failed. RUBY_ASSERT prints the full bug report, which
makes it much easier to debug.
2024-02-12 15:07:47 -05:00
BurdetteLamar
8cad6fc90a Tweak to Integer What's Here 2024-01-06 13:25:17 -05:00
BurdetteLamar
4a44bf8c34 Tweak to Float What's Here 2024-01-06 13:24:48 -05:00
BurdetteLamar
7b615bfdf3 Tweak to Float What's Here 2024-01-06 13:24:48 -05:00
Peter Zhu
d96fe5e4f8 [DOC] Fix indentation in Numeric#step 2024-01-06 11:43:57 -05:00
Peter Zhu
0831d960bf [DOC] Fix typo in call-seq for Float#round 2024-01-06 09:34:54 -05:00
Peter Zhu
6934a60ab1 [DOC] Fix alignment of values for Integer#div 2024-01-01 11:14:42 -05:00
Peter Zhu
c0481e5818 [DOC] Fix indentation for Integer#div
The line was indented, which caused it to be treated as a code block.
2024-01-01 11:12:54 -05:00
Peter Zhu
606c01727a [DOC] Fix indentation for Numeric#step
The documentation was indented one level too deep, which causes RDoc to
generate it all as a code block.
2023-12-29 13:13:17 -05:00
Peter Zhu
550a49c913 [DOC] Fix documentation for Numeric#eql?
+==+ does not format properly, so we should use <tt>==</tt> instead.
2023-12-26 10:48:48 -05:00
John Hawthorn
0c3593b657 Use free with ruby_dtoa
In ae0ceafb0c0d05cc80623b525070255e3abb34ef ruby_dtoa was switched to
use malloc instead of xmalloc, which means that consumers should be
using free instead of xfree. Otherwise we will artificially shrink
oldmalloc_increase_bytes.
2023-12-07 09:23:02 -08:00
Nobuyoshi Nakada
8e93bf8e1f
[Bug #17037] Improve accuracy of division near precision limits
When dividing near the precision limit of `double`, use Bignum
division to get rid of rounding errors.
2023-11-29 20:16:36 +09:00
Kouhei Yanagita
5cb76754ab [DOC] Fix the argument name in the Numeric#step doc 2023-10-15 13:47:58 +09:00
Kouhei Yanagita
b28c1d2c56 [Bug #19926] Fix Range#size for ranges with a Rational endpoint 2023-10-14 11:22:42 +09:00
Nobuyoshi Nakada
50520cc193
[DOC] Missing comment markers 2023-09-27 16:18:05 +09:00
Takashi Kokubun
5b5ae3d9e0
Rewrite Integer#times in Ruby (#8388) 2023-09-07 10:57:52 -07:00
BurdetteLamar
3080cf3dec [DOC] Don't suppress autolinks 2023-08-13 15:47:12 -04:00
Burdette Lamar
6528cf9fcf
[DOC] Fixes for link fragments (#7981) 2023-06-28 09:05:43 -04:00
Peter Zhu
b7a26dfa16 Unify error messages of rb_num2ulong and rb_num2ull
The error messages were slightly different due, which causes different
behaviour on 32-bit and 64-bit systems.
2023-05-29 11:55:39 -04:00
Matt Valentine-House
026321c5b9 [Feature #19474] Refactor NEWOBJ macros
NEWOBJ_OF is now our canonical newobj macro. It takes an optional ec
2023-04-06 11:07:16 +01:00
Takashi Kokubun
233ddfac54 Stop exporting symbols for MJIT 2023-03-06 21:59:23 -08:00
BurdetteLamar
3b239d2480 Remove (newly unneeded) remarks about aliases 2023-02-19 14:26:34 -08:00