Debian Codegraph @

include/ruby/internal/intern/rational.h

raw
1 #ifndef RBIMPL_INTERN_RATIONAL_H /*-*-C++-*-vi:se ft=cpp:*/
2 #define RBIMPL_INTERN_RATIONAL_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_cRational.
22 */
23 #include "ruby/internal/attr/pure.h"
24 #include "ruby/internal/dllexport.h"
25 #include "ruby/internal/value.h"
26 #include "ruby/internal/arithmetic/long.h" /* INT2FIX is here. */
27
28 RBIMPL_SYMBOL_EXPORT_BEGIN()
29
30 /* rational.c */
31
32 /**
33 * Identical to rb_rational_new(), except it skips argument validations. It is
34 * thus dangerous for extension libraries. For instance `1/0r` could be
35 * constructed using this.
36 *
37 * @param[in] num Numerator, an instance of ::rb_cInteger.
38 * @param[in] den Denominator, an instance of ::rb_cInteger.
39 * @exception rb_eTypeError Either argument is not an Integer.
40 * @return An instance of ::rb_cRational whose value is `(num/den)r`.
41 */
42 VALUE rb_rational_raw(VALUE num, VALUE den);
43
44 /**
45 * Shorthand of `(x/1)r`. As `x` is already an Integer, it practically
46 * converts it into a Rational of the identical value.
47 *
48 * @param[in] x An instance of ::rb_cInteger.
49 * @return An instance of ::rb_cRational, whose value is `(x/1)r`.
50 */
51 #define rb_rational_raw1(x) rb_rational_raw((x), INT2FIX(1))
52
53 /** @alias{rb_rational_raw} */
54 #define rb_rational_raw2(x,y) rb_rational_raw((x), (y))
55
56 /**
57 * Constructs a Rational, with reduction. This returns for instance `(2/3)r`
58 * for `rb_rational_new(INT2NUM(-384), INT2NUM(-576))`.
59 *
60 * @param[in] num Numerator, an instance of ::rb_cInteger.
61 * @param[in] den Denominator, an instance of ::rb_cInteger.
62 * @exception rb_eZeroDivError `den` is zero.
63 * @return An instance of ::rb_cRational whose value is `(num/den)r`.
64 */
65 VALUE rb_rational_new(VALUE num, VALUE den);
66
67 /**
68 * Shorthand of `(x/1)r`. As `x` is already an Integer, it practically
69 * converts it into a Rational of the identical value.
70 *
71 * @param[in] x An instance of ::rb_cInteger.
72 * @return An instance of ::rb_cRational, whose value is `(x/1)r`.
73 */
74 #define rb_rational_new1(x) rb_rational_new((x), INT2FIX(1))
75
76 /** @alias{rb_rational_new} */
77 #define rb_rational_new2(x,y) rb_rational_new((x), (y))
78
79 /**
80 * Converts various values into a Rational. This function accepts:
81 *
82 * - Instances of ::rb_cInteger (taken as-is),
83 * - Instances of ::rb_cRational (taken as-is),
84 * - Instances of ::rb_cFloat (applies `#to_r`),
85 * - Instances of ::rb_cComplex (applies `#to_r`),
86 * - Instances of ::rb_cString (applies `#to_r`),
87 * - Other objects that respond to `#to_r`.
88 *
89 * It (possibly recursively) applies `#to_r` until both sides become either
90 * Integer or Rational, then divides them.
91 *
92 * As a special case, passing ::RUBY_Qundef to `den` is the same as passing
93 * `RB_INT2NUM(1)`.
94 *
95 * @param[in] num Numerator (see above).
96 * @param[in] den Denominator (see above).
97 * @exception rb_eTypeError Passed something not described above.
98 * @exception rb_eFloatDomainError `#to_r` produced Nan/Inf.
99 * @exception rb_eZeroDivError `#to_r` produced zero for `den`.
100 * @return An instance of ::rb_cRational whose value is `(num/den)r`.
101 *
102 * @internal
103 *
104 * This was the implementation of `Kernel#Rational` before, but they diverged.
105 */
106 VALUE rb_Rational(VALUE num, VALUE den);
107
108 /**
109 * Shorthand of `(x/1)r`. It practically converts it into a Rational of the
110 * identical value.
111 *
112 * @param[in] x ::rb_cInteger, ::rb_cRational, or something that responds to
113 * `#to_r`.
114 * @return An instance of ::rb_cRational, whose value is `(x/1)r`.
115 */
116 #define rb_Rational1(x) rb_Rational((x), INT2FIX(1))
117
118 /** @alias{rb_Rational} */
119 #define rb_Rational2(x,y) rb_Rational((x), (y))
120
121 RBIMPL_ATTR_PURE()
122 /**
123 * Queries the numerator of the passed Rational.
124 *
125 * @param[in] rat An instance of ::rb_cRational.
126 * @return Its numerator part, which is an instance of ::rb_cInteger.
127 */
128 VALUE rb_rational_num(VALUE rat);
129
130 RBIMPL_ATTR_PURE()
131 /**
132 * Queries the denominator of the passed Rational.
133 *
134 * @param[in] rat An instance of ::rb_cRational.
135 * @return Its denominator part, which is an instance of ::rb_cInteger
136 * greater than or equal to one..
137 */
138 VALUE rb_rational_den(VALUE rat);
139
140 /**
141 * Simplified approximation of a float. It returns a rational `rat` which
142 * satisfies:
143 *
144 * ```
145 * flt - |prec| <= rat <= flt + |prec|
146 * ```
147 *
148 * ```ruby
149 * 3.141592.rationalize(0.001) # => (201/64)r
150 * 3.141592.rationalize(0.01)' # => (22/7)r
151 * 3.141592.rationalize(0.1)' # => (16/5)r
152 * 3.141592.rationalize(1)' # => (3/1)r
153 * ```
154 *
155 * @param[in] flt An instance of ::rb_cFloat to rationalise.
156 * @param[in] prec Another ::rb_cFloat, which is the "precision".
157 * @return Approximation of `flt`, in ::rb_cRational.
158 */
159 VALUE rb_flt_rationalize_with_prec(VALUE flt, VALUE prec);
160
161 /**
162 * Identical to rb_flt_rationalize_with_prec(), except it auto-detects
163 * appropriate precision depending on the passed value.
164 *
165 * @param[in] flt An instance of ::rb_cFloat to rationalise.
166 * @return Approximation of `flt`, in ::rb_cRational.
167 */
168 VALUE rb_flt_rationalize(VALUE flt);
169
170 RBIMPL_SYMBOL_EXPORT_END()
171
172 #endif /* RBIMPL_INTERN_RATIONAL_H */