[#109095] [Ruby master Misc#18888] Migrate ruby-lang.org mail services to Google Domains and Google Workspace — "shugo (Shugo Maeda)" <noreply@...>
Issue #18888 has been reported by shugo (Shugo Maeda).
16 messages
2022/06/30
[ruby-core:109034] [Ruby master Feature#18809] Add Numeric#ceildiv
From:
"Dan0042 (Daniel DeLorme)" <noreply@...>
Date:
2022-06-21 12:46:00 UTC
List:
ruby-core #109034
Issue #18809 has been updated by Dan0042 (Daniel DeLorme).
Why not simply use `a.fdiv(b).ceil` ?
It expresses the intent of the code clearly, and I doubt there would be a measurable difference in performance except in the tightest of tight loops.
----------------------------------------
Feature #18809: Add Numeric#ceildiv
https://bugs.ruby-lang.org/issues/18809#change-98150
* Author: kyanagi (Kouhei Yanagita)
* Status: Open
* Priority: Normal
----------------------------------------
pull request: https://github.com/ruby/ruby/pull/5965
I have needed to implement "rounding up division" several times.
("rounding up division" means getting a quotient of division which is rounded up to the nearest integer.)
Typically, this is implemented as follows:
``` ruby
# notice that b > 0 is assumed
def rounding_up_division(a, b)
(a + b - 1) / b
end
```
But for me, this is difficult to write without careful consideration.
Every time I implement this, I need to think for a few minutes on paper.
So I propose to add a new method `Numeric#ceildiv`.
Typical examples where this is necessary are counting groups and pagination.
e.g. There are 123 items. If you display 10 items on each page, how many pages are there?
``` ruby
123.ceildiv(10) # => 13
```
We can find several examples of this division also in the Ruby's source code. (Try `grep -r -E -e '([^ ]+) *- *1\) */ *\1' .`)
```
./internal.h:#define roomof(x, y) (((x) + (y) - 1) / (y))
./array.c: len = (len + ustep - 1) / ustep;
./include/ruby/internal/memory.h: const size_t cnt = (total_size + sizeof(VALUE) - 1) / sizeof(VALUE);
./ext/bigdecimal/missing/dtoa.c:#define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
./ext/bigdecimal/bigdecimal.c: nc += (nc + mc - 1) / mc + 1;
./ext/bigdecimal/bigdecimal.c: mx = (mx + BASE_FIG - 1) / BASE_FIG; /* Determine allocation unit. */
./ext/bigdecimal/bigdecimal.c: mf = (mf + BASE_FIG - 1) / BASE_FIG + 2; /* Needs 1 more for div */
./ext/bigdecimal/bigdecimal.c: nalloc = (ni + nf + BASE_FIG - 1) / BASE_FIG + 1; /* set effective allocation */
./ext/bigdecimal/bigdecimal.c: size_t const round_limit = (VpGetPrecLimit() + BASE_FIG - 1) / BASE_FIG;
./ext/bigdecimal/bigdecimal.c: if ((ix + BASE_FIG - 1) / BASE_FIG > ixDigit + 1) return 0;
./ext/bigdecimal/bits.h:#define roomof(x, y) (((x) + (y) - 1) / (y))
./internal/numeric.h: VALUE values[(SIZEOF_DOUBLE + SIZEOF_VALUE - 1) / SIZEOF_VALUE];
./regcomp.c: OnigDistance str_len = (byte_len + mb_len - 1) / mb_len;
./bignum.c: size_t num_bdigits = (num_bits + BITSPERDIG - 1) / BITSPERDIG;
./missing/dtoa.c:#define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
./numeric.c: char buf[float_dig + (decimal_mant + CHAR_BIT - 1) / CHAR_BIT + 10];
./gc.c:#define CEILDIV(i, mod) (((i) + (mod) - 1)/(mod))
```
Naming:
I was not sure whether to name it `ceildiv` or `divceil` because there are both `divmod` and `fdiv`.
Since `divmod` is a method that returns two elements, the quotient and the remainder,
while `fdiv` is a method that performs Float division, I decided to follow `fdiv`.
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>