Debian Codegraph @

include/ruby/internal/intern/numeric.h

raw
1 #ifndef RBIMPL_INTERN_NUMERIC_H /*-*-C++-*-vi:se ft=cpp:*/
2 #define RBIMPL_INTERN_NUMERIC_H
3 /**
4 * @file
5 * @author Ruby developers <[email protected]>
6 * @copyright This file is a part of the programming language Ruby.
7 * Permission is hereby granted, to either redistribute and/or
8 * modify this file, provided that the conditions mentioned in the
9 * file COPYING are met. Consult the file for details.
10 * @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are
11 * implementation details. Don't take them as canon. They could
12 * rapidly appear then vanish. The name (path) of this header file
13 * is also an implementation detail. Do not expect it to persist
14 * at the place it is now. Developers are free to move it anywhere
15 * anytime at will.
16 * @note To ruby-core: remember that this header can be possibly
17 * recursively included from extension libraries written in C++.
18 * Do not expect for instance `__VA_ARGS__` is always available.
19 * We assume C99 for ruby itself but we don't assume languages of
20 * extension libraries. They could be written in C++98.
21 * @brief Public APIs related to ::rb_cNumeric.
22 */
23 #include "ruby/internal/attr/cold.h"
24 #include "ruby/internal/attr/noreturn.h"
25 #include "ruby/internal/dllexport.h"
26 #include "ruby/internal/value.h"
27
28 /**
29 * @private
30 *
31 * @deprecated This macro once was a thing in the old days, but makes no sense
32 * any longer today. Exists here for backwards compatibility
33 * only. You can safely forget about it.
34 */
35 #define RB_NUM_COERCE_FUNCS_NEED_OPID 1
36
37 RBIMPL_SYMBOL_EXPORT_BEGIN()
38
39 /* numeric.c */
40
41 RBIMPL_ATTR_NORETURN()
42 RBIMPL_ATTR_COLD()
43 /**
44 * Just always raises an exception.
45 *
46 * @exception rb_eZeroDivError Division by zero error.
47 */
48 void rb_num_zerodiv(void);
49
50 /**
51 * @name Coercion operators.
52 *
53 * What is a coercion? Well Ruby is basically an OOPL but it also has
54 * arithmetic operators. They are implemented in OO manners. For instance
55 * `a+b` is a binary operation `+`, whose receiver is `a`, and whose (sole)
56 * argument is `b`.
57 *
58 * The problem is, you often want `a+b == b+a` to hold. That is easy if both
59 * `a` and `b` belongs to the same class... Ensuring `1 + 2 == 2 + 1` is kind
60 * of intuitive. But if you want `1.0 + 2 == 2 + 1.0`, things start getting
61 * complicated. `1.0+2` is `Float#+`, while `2+1.0` is `Integer#+`. In order
62 * to achieve the equality Float's and Integer's methods must agree with their
63 * behaviours.
64 *
65 * Now. Floats versus Integers situation is still controllable because they
66 * are both built-in. But in Ruby you can define your own numeric classes.
67 * BigDecimal, which is a rubygems gem distributed along with the interpreter,
68 * is one of such examples. Rational was another such example before. In
69 * short you cannot create list of all possible combination of the classes that
70 * could be the operand of `+` operator. Then how do we achieve the
71 * commutativity?
72 *
73 * Here comes the concept of coercion. If a definition of an operator
74 * encounters an object which is unknown to the author, just assumes that the
75 * unknown object knows how to handle the situation. So for instance when
76 * `1+x` has unknown `x`, it lets the `x` handle this.
77 *
78 * ```ruby
79 * class Foo
80 * def +(x)
81 * if we_know_what_is_x? then
82 * ... # handle here
83 * else
84 * y, z = x.coerce self
85 * return y + z
86 * end
87 * end
88 * end
89 * ```
90 *
91 * The `x.coerce` method returns a 2-element array which are "casted" versions
92 * of `x` and `self`.
93 *
94 * @{
95 */
96
97 /**
98 * Coerced binary operation. This function first coerces the two objects, then
99 * applies the operation.
100 *
101 * @param[in] lhs LHS operand.
102 * @param[in] rhs RHS operand.
103 * @param[in] op Operator method name.
104 * @exception rb_eTypeError Coercion failed for some reason.
105 * @return `lhs op rhs`, in a coerced way.
106 */
107 VALUE rb_num_coerce_bin(VALUE lhs, VALUE rhs, ID op);
108
109 /**
110 * Identical to rb_num_coerce_bin(), except for return values. This function
111 * best suits for comparison operators e.g. `<=>`.
112 *
113 * @param[in] lhs LHS operand.
114 * @param[in] rhs RHS operand.
115 * @param[in] op Operator method name.
116 * @retval RUBY_Qnil Coercion failed for some reason.
117 * @retval otherwise `lhs op rhs`, in a coerced way.
118 */
119 VALUE rb_num_coerce_cmp(VALUE lhs, VALUE rhs, ID op);
120
121 /**
122 * Identical to rb_num_coerce_cmp(), except for return values. This function
123 * best suits for relationship operators e.g. `<=`.
124 *
125 * @param[in] lhs LHS operand.
126 * @param[in] rhs RHS operand.
127 * @param[in] op Operator method name.
128 * @exception rb_eArgError Coercion failed for some reason.
129 * @return `lhs op rhs`, in a coerced way.
130 */
131 VALUE rb_num_coerce_relop(VALUE lhs, VALUE rhs, ID op);
132
133 /**
134 * This one is optimised for bitwise operations, but the API is identical to
135 * rb_num_coerce_bin().
136 *
137 * @param[in] lhs LHS operand.
138 * @param[in] rhs RHS operand.
139 * @param[in] op Operator method name.
140 * @exception rb_eArgError Coercion failed for some reason.
141 * @return `lhs op rhs`, in a coerced way.
142 */
143 VALUE rb_num_coerce_bit(VALUE lhs, VALUE rhs, ID op);
144
145 /** @} */
146
147 /**
148 * Converts a numeric value into a Fixnum. This is not a preserving
149 * conversion; for instance 1.5 would be converted into 1.
150 *
151 * @param[in] val A numeric object.
152 * @exception rb_eTypeError No conversion from `val` to Integer.
153 * @exception rb_eRangeError `val` out of range.
154 * @return A fixnum converted from `val`.
155 *
156 * @internal
157 *
158 * This seems used from nowhere?
159 */
160 VALUE rb_num2fix(VALUE val);
161
162 /**
163 * Generates a place-value representation of the given Fixnum, with given
164 * radix.
165 *
166 * @param[in] val A fixnum to stringify.
167 * @param[in] base `2` to `36` inclusive for each radix.
168 * @exception rb_eArgError `base` is out of range.
169 * @return An instance of ::rb_cString representing `val`.
170 * @pre `val` must be a Fixnum (no checks performed).
171 */
172 VALUE rb_fix2str(VALUE val, int base);
173
174 RBIMPL_ATTR_CONST()
175 /**
176 * Compares two `double`s. Handy when implementing a spaceship operator.
177 *
178 * @param[in] lhs A value.
179 * @param[in] rhs Another value.
180 * @retval RB_INT2FIX(-1) `lhs` is "bigger than" `rhs`.
181 * @retval RB_INT2FIX(1) `rhs` is "bigger than" `lhs`.
182 * @retval RB_INT2FIX(0) They are equal.
183 * @retval RUBY_Qnil Not comparable, e.g. NaN.
184 */
185 VALUE rb_dbl_cmp(double lhs, double rhs);
186
187 /**
188 * Raises the passed `x` to the power of `y`.
189 *
190 * @note The return value can be really big.
191 * @note Also the return value can be really small, in case `x` is a
192 * negative number.
193 * @param[in] x A number.
194 * @param[in] y Another number.
195 * @retval Inf Cannot express the result.
196 * @retval 1 Either `y` is 0 or `x` is 1.
197 * @retval otherwise An instance of ::rb_cInteger whose value is `x ** y`.
198 *
199 * @internal
200 *
201 * This function returns Infinity when `y` is big enough not to fit into a
202 * Fixnum. Warning is issued then.
203 */
204 RUBY_EXTERN VALUE rb_int_positive_pow(long x, unsigned long y);
205
206 RBIMPL_SYMBOL_EXPORT_END()
207
208 #endif /* RBIMPL_INTERN_NUMERIC_H */