Doc for some #ceil and #floor

This commit is contained in:
BurdetteLamar 2024-07-25 15:53:07 +01:00 committed by Peter Zhu
parent 7464514ca5
commit 78f1b835a0
Notes: git 2024-07-26 15:22:36 +00:00

View File

@ -2170,36 +2170,82 @@ flo_ndigits(int argc, VALUE *argv)
} }
/* /*
* :markup: markdown
*
* call-seq: * call-seq:
* floor(ndigits = 0) -> float or integer * floor(ndigits = 0) -> float or integer
* *
* Returns the largest number less than or equal to +self+ with * Returns a float or integer that is a "floor" value for `self`,
* a precision of +ndigits+ decimal digits. * as specified by `ndigits`,
* which must be an
* [integer-convertible object](rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects).
* *
* When +ndigits+ is positive, returns a float with +ndigits+ * When `self` is zero,
* returns a zero value:
* a float if `ndigits` is positive,
* an integer otherwise:
*
* ```
* f = 0.0 # => 0.0
* f.floor(20) # => 0.0
* f.floor(0) # => 0
* f.floor(-20) # => 0
* ```
*
* When `self` is non-zero and `ndigits` is positive, returns a float with `ndigits`
* digits after the decimal point (as available): * digits after the decimal point (as available):
* *
* ```
* f = 12345.6789 * f = 12345.6789
* f.floor(1) # => 12345.6 * f.floor(1) # => 12345.6
* f.floor(3) # => 12345.678 * f.floor(3) # => 12345.678
* f.floor(30) # => 12345.6789
* f = -12345.6789 * f = -12345.6789
* f.floor(1) # => -12345.7 * f.floor(1) # => -12345.7
* f.floor(3) # => -12345.679 * f.floor(3) # => -12345.679
* f.floor(30) # => -12345.6789
* ```
* *
* When +ndigits+ is non-positive, returns an integer with at least * When `self` is non-zero and `ndigits` is non-positive,
* <code>ndigits.abs</code> trailing zeros: * returns an integer value based on a computed granularity:
* *
* f = 12345.6789 * - The granularity is `10 ** ndigits.abs`.
* f.floor(0) # => 12345 * - The returned value is the largest multiple of the granularity
* f.floor(-3) # => 12000 * that is less than or equal to `self`.
* f = -12345.6789 *
* f.floor(0) # => -12346 * Examples with positive `self`:
* f.floor(-3) # => -13000 *
* | ndigits | Granularity | 12345.6789.floor(ndigits) |
* |--------:|------------:|--------------------------:|
* | 0 | 1 | 12345 |
* | -1 | 10 | 12340 |
* | -2 | 100 | 12300 |
* | -3 | 1000 | 12000 |
* | -4 | 10000 | 10000 |
* | -5 | 100000 | 0 |
*
* Examples with negative `self`:
*
* | ndigits | Granularity | -12345.6789.floor(ndigits) |
* |--------:|------------:|---------------------------:|
* | 0 | 1 | -12346 |
* | -1 | 10 | -12350 |
* | -2 | 100 | -12400 |
* | -3 | 1000 | -13000 |
* | -4 | 10000 | -20000 |
* | -5 | 100000 | -100000 |
* | -6 | 1000000 | -1000000 |
* | -7 | 10000000 | -10000000 |
* | -8 | 100000000 | -100000000 |
* | -9 | 1000000000 | -1000000000 |
* | -10 | 10000000000 | 0 |
* *
* Note that the limited precision of floating-point arithmetic * Note that the limited precision of floating-point arithmetic
* may lead to surprising results: * may lead to surprising results:
* *
* (0.3 / 0.1).floor #=> 2 (!) * ```
* (0.3 / 0.1).floor # => 2 # Not 3, (because (0.3 / 0.1) # => 2.9999999999999996, not 3.0)
* ```
* *
* Related: Float#ceil. * Related: Float#ceil.
* *
@ -2715,13 +2761,16 @@ num_floor(int argc, VALUE *argv, VALUE num)
/* /*
* call-seq: * call-seq:
* ceil(digits = 0) -> integer or float * ceil(ndigits = 0) -> float or integer
* *
* Returns the smallest number that is greater than or equal to +self+ with * Returns the smallest float or integer that is greater than or equal to +self+,
* a precision of +digits+ decimal digits. * as specified by the given `ndigits`,
* which must be an
* [integer-convertible object](rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects).
* *
* \Numeric implements this by converting +self+ to a Float and * Equivalent to <tt>self.to_f.ceil(ndigits)</tt>.
* invoking Float#ceil. *
* Related: #floor, Float#ceil.
*/ */
static VALUE static VALUE