Ruby
3.4.0dev (2024-11-22 revision 37a72b0150ec36b4ea27175039afc28c62207b0c)
|
Public APIs related to rb_cNumeric. More...
#include "ruby/internal/attr/cold.h"
#include "ruby/internal/attr/noreturn.h"
#include "ruby/internal/dllexport.h"
#include "ruby/internal/value.h"
Go to the source code of this file.
Macros | |
#define | RB_NUM_COERCE_FUNCS_NEED_OPID 1 |
Functions | |
void | rb_num_zerodiv (void) |
Just always raises an exception. More... | |
VALUE | rb_num2fix (VALUE val) |
Converts a numeric value into a Fixnum. More... | |
VALUE | rb_fix2str (VALUE val, int base) |
Generates a place-value representation of the given Fixnum, with given radix. More... | |
VALUE | rb_dbl_cmp (double lhs, double rhs) |
Compares two double s. More... | |
VALUE | rb_int_positive_pow (long x, unsigned long y) |
Raises the passed x to the power of y . More... | |
Coercion operators. | |
What is a coercion? Well Ruby is basically an OOPL but it also has arithmetic operators. They are implemented in OO manners. For instance The problem is, you often want Now. Floats versus Integers situation is still controllable because they are both built-in. But in Ruby you can define your own numeric classes. BigDecimal, which is a rubygems gem distributed along with the interpreter, is one of such examples. Rational was another such example before. In short you cannot create list of all possible combination of the classes that could be the operand of Here comes the concept of coercion. If a definition of an operator encounters an object which is unknown to the author, just assumes that the unknown object knows how to handle the situation. So for instance when class Foo
def +(x)
if we_know_what_is_x? then
... # handle here
else
y, z = x.coerce self
return y + z
end
end
end
The | |
VALUE | rb_num_coerce_bin (VALUE lhs, VALUE rhs, ID op) |
Coerced binary operation. More... | |
VALUE | rb_num_coerce_cmp (VALUE lhs, VALUE rhs, ID op) |
Identical to rb_num_coerce_bin(), except for return values. More... | |
VALUE | rb_num_coerce_relop (VALUE lhs, VALUE rhs, ID op) |
Identical to rb_num_coerce_cmp(), except for return values. More... | |
VALUE | rb_num_coerce_bit (VALUE lhs, VALUE rhs, ID op) |
This one is optimised for bitwise operations, but the API is identical to rb_num_coerce_bin(). More... | |
Public APIs related to rb_cNumeric.
RBIMPL
or rbimpl
are implementation details. Don't take them as canon. They could rapidly appear then vanish. The name (path) of this header file is also an implementation detail. Do not expect it to persist at the place it is now. Developers are free to move it anywhere anytime at will. __VA_ARGS__
is always available. We assume C99 for ruby itself but we don't assume languages of extension libraries. They could be written in C++98. Definition in file numeric.h.
#define RB_NUM_COERCE_FUNCS_NEED_OPID 1 |
Exists here for backwards compatibility only. You can safely forget about it.
VALUE rb_dbl_cmp | ( | double | lhs, |
double | rhs | ||
) |
Compares two double
s.
Handy when implementing a spaceship operator.
[in] | lhs | A value. |
[in] | rhs | Another value. |
RB_INT2FIX(-1) | lhs is "bigger than" rhs . |
RB_INT2FIX(1) | rhs is "bigger than" lhs . |
RB_INT2FIX(0) | They are equal. |
RUBY_Qnil | Not comparable, e.g. NaN. |
Generates a place-value representation of the given Fixnum, with given radix.
[in] | val | A fixnum to stringify. |
[in] | base | 2 to 36 inclusive for each radix. |
rb_eArgError | base is out of range. |
val
. val
must be a Fixnum (no checks performed). VALUE rb_int_positive_pow | ( | long | x, |
unsigned long | y | ||
) |
Raises the passed x
to the power of y
.
x
is a negative number. [in] | x | A number. |
[in] | y | Another number. |
Inf | Cannot express the result. |
1 | Either y is 0 or x is 1. |
otherwise | An instance of rb_cInteger whose value is x ** y . |
Definition at line 4559 of file numeric.c.
Referenced by rb_flt_rationalize(), and rb_str_format().
Converts a numeric value into a Fixnum.
This is not a preserving conversion; for instance 1.5 would be converted into 1.
[in] | val | A numeric object. |
rb_eTypeError | No conversion from val to Integer. |
rb_eRangeError | val out of range. |
val
. Coerced binary operation.
This function first coerces the two objects, then applies the operation.
[in] | lhs | LHS operand. |
[in] | rhs | RHS operand. |
[in] | op | Operator method name. |
rb_eTypeError | Coercion failed for some reason. |
lhs op rhs
, in a coerced way. Definition at line 477 of file numeric.c.
Referenced by rb_big_divmod(), rb_big_minus(), rb_big_modulo(), rb_big_mul(), rb_big_plus(), rb_big_pow(), rb_complex_minus(), rb_complex_mul(), rb_complex_plus(), and rb_complex_pow().
This one is optimised for bitwise operations, but the API is identical to rb_num_coerce_bin().
[in] | lhs | LHS operand. |
[in] | rhs | RHS operand. |
[in] | op | Operator method name. |
rb_eArgError | Coercion failed for some reason. |
lhs op rhs
, in a coerced way. Definition at line 4987 of file numeric.c.
Referenced by rb_big_and(), rb_big_or(), and rb_big_xor().
Identical to rb_num_coerce_bin(), except for return values.
This function best suits for comparison operators e.g. <=>
.
[in] | lhs | LHS operand. |
[in] | rhs | RHS operand. |
[in] | op | Operator method name. |
RUBY_Qnil | Coercion failed for some reason. |
otherwise | lhs op rhs , in a coerced way. |
Definition at line 484 of file numeric.c.
Referenced by rb_big_cmp().
Identical to rb_num_coerce_cmp(), except for return values.
This function best suits for relationship operators e.g. <=
.
[in] | lhs | LHS operand. |
[in] | rhs | RHS operand. |
[in] | op | Operator method name. |
rb_eArgError | Coercion failed for some reason. |
lhs op rhs
, in a coerced way.