rbs 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +5 -0
  3. data/CHANGELOG.md +23 -0
  4. data/README.md +6 -1
  5. data/core/array.rbs +2866 -1086
  6. data/core/basic_object.rbs +150 -30
  7. data/core/binding.rbs +33 -0
  8. data/core/builtin.rbs +4 -4
  9. data/core/class.rbs +43 -5
  10. data/core/comparable.rbs +57 -0
  11. data/core/complex.rbs +170 -4
  12. data/core/constants.rbs +51 -0
  13. data/core/deprecated.rbs +7 -0
  14. data/core/dir.rbs +305 -20
  15. data/core/encoding.rbs +472 -77
  16. data/core/enumerable.rbs +2173 -234
  17. data/core/enumerator.rbs +448 -182
  18. data/core/env.rbs +448 -1
  19. data/core/errno.rbs +1 -10
  20. data/core/errors.rbs +152 -2
  21. data/core/exception.rbs +201 -127
  22. data/core/false_class.rbs +27 -0
  23. data/core/fiber.rbs +118 -37
  24. data/core/fiber_error.rbs +8 -9
  25. data/core/file.rbs +1059 -139
  26. data/core/file_test.rbs +287 -32
  27. data/core/float.rbs +776 -300
  28. data/core/gc.rbs +185 -34
  29. data/core/global_variables.rbs +5 -1
  30. data/core/hash.rbs +1582 -649
  31. data/core/integer.rbs +974 -204
  32. data/core/io/buffer.rbs +710 -0
  33. data/core/io/wait.rbs +29 -8
  34. data/core/io.rbs +2438 -417
  35. data/core/kernel.rbs +2315 -316
  36. data/core/marshal.rbs +37 -2
  37. data/core/match_data.rbs +123 -6
  38. data/core/math.rbs +126 -6
  39. data/core/method.rbs +226 -102
  40. data/core/module.rbs +421 -45
  41. data/core/nil_class.rbs +64 -0
  42. data/core/numeric.rbs +620 -142
  43. data/core/object.rbs +453 -81
  44. data/core/object_space.rbs +92 -2
  45. data/core/proc.rbs +482 -285
  46. data/core/process.rbs +443 -34
  47. data/core/ractor.rbs +232 -9
  48. data/core/random.rbs +151 -52
  49. data/core/range.rbs +885 -160
  50. data/core/rational.rbs +122 -6
  51. data/core/rb_config.rbs +14 -4
  52. data/core/refinement.rbs +44 -0
  53. data/core/regexp.rbs +156 -14
  54. data/core/ruby_vm.rbs +42 -3
  55. data/core/signal.rbs +78 -39
  56. data/core/string.rbs +2123 -567
  57. data/core/string_io.rbs +204 -0
  58. data/core/struct.rbs +283 -28
  59. data/core/symbol.rbs +304 -30
  60. data/core/thread.rbs +1288 -688
  61. data/core/thread_group.rbs +66 -10
  62. data/core/time.rbs +643 -217
  63. data/core/trace_point.rbs +100 -12
  64. data/core/true_class.rbs +24 -0
  65. data/core/unbound_method.rbs +73 -7
  66. data/core/warning.rbs +37 -12
  67. data/docs/CONTRIBUTING.md +40 -34
  68. data/docs/stdlib.md +3 -102
  69. data/lib/rbs/annotate/annotations.rb +197 -0
  70. data/lib/rbs/annotate/formatter.rb +80 -0
  71. data/lib/rbs/annotate/rdoc_annotator.rb +398 -0
  72. data/lib/rbs/annotate/rdoc_source.rb +120 -0
  73. data/lib/rbs/annotate.rb +6 -0
  74. data/lib/rbs/cli.rb +45 -1
  75. data/lib/rbs/definition_builder.rb +5 -1
  76. data/lib/rbs/location_aux.rb +12 -0
  77. data/lib/rbs/prototype/rb.rb +12 -0
  78. data/lib/rbs/version.rb +1 -1
  79. data/sig/annotate/annotations.rbs +102 -0
  80. data/sig/annotate/formatter.rbs +24 -0
  81. data/sig/annotate/rdoc_annotater.rbs +82 -0
  82. data/sig/annotate/rdoc_source.rbs +30 -0
  83. data/sig/cli.rbs +2 -0
  84. data/sig/collection/{collections.rbs → sources.rbs} +0 -0
  85. data/sig/location.rbs +6 -0
  86. data/sig/method_types.rbs +5 -1
  87. data/sig/polyfill.rbs +78 -0
  88. data/stdlib/abbrev/0/abbrev.rbs +6 -0
  89. data/stdlib/abbrev/0/array.rbs +26 -0
  90. data/stdlib/base64/0/base64.rbs +31 -0
  91. data/stdlib/benchmark/0/benchmark.rbs +74 -3
  92. data/stdlib/bigdecimal/0/big_decimal.rbs +614 -165
  93. data/stdlib/bigdecimal-math/0/big_math.rbs +41 -64
  94. data/stdlib/cgi/0/core.rbs +59 -0
  95. data/stdlib/coverage/0/coverage.rbs +164 -2
  96. data/stdlib/csv/0/csv.rbs +2862 -398
  97. data/stdlib/date/0/date.rbs +483 -25
  98. data/stdlib/date/0/date_time.rbs +187 -12
  99. data/stdlib/dbm/0/dbm.rbs +152 -17
  100. data/stdlib/digest/0/digest.rbs +146 -0
  101. data/stdlib/erb/0/erb.rbs +65 -245
  102. data/stdlib/fiber/0/fiber.rbs +73 -91
  103. data/stdlib/fileutils/0/fileutils.rbs +301 -1
  104. data/stdlib/find/0/find.rbs +9 -0
  105. data/stdlib/forwardable/0/forwardable.rbs +65 -1
  106. data/stdlib/io-console/0/io-console.rbs +227 -15
  107. data/stdlib/ipaddr/0/ipaddr.rbs +161 -0
  108. data/stdlib/json/0/json.rbs +1146 -144
  109. data/stdlib/logger/0/formatter.rbs +24 -0
  110. data/stdlib/logger/0/log_device.rbs +64 -0
  111. data/stdlib/logger/0/logger.rbs +165 -13
  112. data/stdlib/logger/0/period.rbs +10 -0
  113. data/stdlib/logger/0/severity.rbs +26 -0
  114. data/stdlib/monitor/0/monitor.rbs +163 -0
  115. data/stdlib/mutex_m/0/mutex_m.rbs +35 -6
  116. data/stdlib/net-http/0/net-http.rbs +1492 -683
  117. data/stdlib/nkf/0/nkf.rbs +372 -0
  118. data/stdlib/objspace/0/objspace.rbs +149 -90
  119. data/stdlib/openssl/0/openssl.rbs +8108 -71
  120. data/stdlib/optparse/0/optparse.rbs +487 -19
  121. data/stdlib/pathname/0/pathname.rbs +425 -124
  122. data/stdlib/prettyprint/0/prettyprint.rbs +120 -99
  123. data/stdlib/prime/0/integer-extension.rbs +20 -2
  124. data/stdlib/prime/0/prime.rbs +88 -21
  125. data/stdlib/pstore/0/pstore.rbs +102 -0
  126. data/stdlib/pty/0/pty.rbs +64 -14
  127. data/stdlib/resolv/0/resolv.rbs +420 -31
  128. data/stdlib/rubygems/0/basic_specification.rbs +4 -1
  129. data/stdlib/rubygems/0/config_file.rbs +33 -1
  130. data/stdlib/rubygems/0/dependency_installer.rbs +4 -3
  131. data/stdlib/rubygems/0/installer.rbs +13 -1
  132. data/stdlib/rubygems/0/path_support.rbs +4 -1
  133. data/stdlib/rubygems/0/platform.rbs +5 -1
  134. data/stdlib/rubygems/0/request_set.rbs +44 -2
  135. data/stdlib/rubygems/0/requirement.rbs +65 -2
  136. data/stdlib/rubygems/0/rubygems.rbs +407 -0
  137. data/stdlib/rubygems/0/source_list.rbs +13 -0
  138. data/stdlib/rubygems/0/specification.rbs +21 -1
  139. data/stdlib/rubygems/0/stream_ui.rbs +3 -1
  140. data/stdlib/rubygems/0/uninstaller.rbs +8 -1
  141. data/stdlib/rubygems/0/version.rbs +60 -157
  142. data/stdlib/securerandom/0/securerandom.rbs +44 -0
  143. data/stdlib/set/0/set.rbs +420 -106
  144. data/stdlib/shellwords/0/shellwords.rbs +55 -77
  145. data/stdlib/singleton/0/singleton.rbs +20 -0
  146. data/stdlib/socket/0/addrinfo.rbs +210 -9
  147. data/stdlib/socket/0/basic_socket.rbs +103 -11
  148. data/stdlib/socket/0/ip_socket.rbs +31 -9
  149. data/stdlib/socket/0/socket.rbs +586 -38
  150. data/stdlib/socket/0/tcp_server.rbs +22 -2
  151. data/stdlib/socket/0/tcp_socket.rbs +12 -1
  152. data/stdlib/socket/0/udp_socket.rbs +25 -2
  153. data/stdlib/socket/0/unix_server.rbs +22 -2
  154. data/stdlib/socket/0/unix_socket.rbs +45 -5
  155. data/stdlib/strscan/0/string_scanner.rbs +210 -9
  156. data/stdlib/tempfile/0/tempfile.rbs +58 -10
  157. data/stdlib/time/0/time.rbs +208 -116
  158. data/stdlib/timeout/0/timeout.rbs +10 -0
  159. data/stdlib/tmpdir/0/tmpdir.rbs +13 -4
  160. data/stdlib/tsort/0/cyclic.rbs +1 -0
  161. data/stdlib/tsort/0/interfaces.rbs +1 -0
  162. data/stdlib/tsort/0/tsort.rbs +42 -0
  163. data/stdlib/uri/0/common.rbs +57 -8
  164. data/stdlib/uri/0/file.rbs +55 -109
  165. data/stdlib/uri/0/ftp.rbs +6 -3
  166. data/stdlib/uri/0/generic.rbs +556 -327
  167. data/stdlib/uri/0/http.rbs +26 -115
  168. data/stdlib/uri/0/https.rbs +8 -102
  169. data/stdlib/uri/0/ldap.rbs +143 -137
  170. data/stdlib/uri/0/ldaps.rbs +8 -102
  171. data/stdlib/uri/0/mailto.rbs +3 -0
  172. data/stdlib/uri/0/rfc2396_parser.rbs +66 -26
  173. data/stdlib/uri/0/ws.rbs +6 -3
  174. data/stdlib/uri/0/wss.rbs +5 -3
  175. data/stdlib/yaml/0/dbm.rbs +151 -87
  176. data/stdlib/yaml/0/store.rbs +6 -0
  177. data/stdlib/zlib/0/zlib.rbs +90 -31
  178. metadata +17 -5
  179. data/lib/rbs/location.rb +0 -221
data/core/float.rbs CHANGED
@@ -1,119 +1,390 @@
1
- # Float objects represent inexact real numbers using the native architecture's
2
- # double-precision floating point representation.
1
+ # <!-- rdoc-file=numeric.c -->
2
+ # A Float object represents a sometimes-inexact real number using the native
3
+ # architecture's double-precision floating point representation.
3
4
  #
4
5
  # Floating point has a different arithmetic and is an inexact number. So you
5
6
  # should know its esoteric system. See following:
6
7
  #
7
- # * http://docs.sun.com/source/806-3568/ncg_goldberg.html
8
+ # * https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
8
9
  # * https://github.com/rdp/ruby_tutorials_core/wiki/Ruby-Talk-FAQ#floats_impre
9
10
  # cise
10
- # * http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
11
+ # * https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
11
12
  #
12
13
  #
14
+ # You can create a Float object explicitly with:
15
+ #
16
+ # * A [floating-point
17
+ # literal](doc/syntax/literals_rdoc.html#label-Float+Literals).
18
+ #
19
+ #
20
+ # You can convert certain objects to Floats with:
21
+ #
22
+ # * Method [Float](Kernel.html#method-i-Float).
23
+ #
24
+ #
25
+ # ## What's Here
26
+ #
27
+ # First, what's elsewhere. Class Float:
28
+ #
29
+ # * Inherits from [class
30
+ # Numeric](Numeric.html#class-Numeric-label-What-27s+Here).
31
+ #
32
+ #
33
+ # Here, class Float provides methods for:
34
+ #
35
+ # * [Querying](#class-Float-label-Querying)
36
+ # * [Comparing](#class-Float-label-Comparing)
37
+ # * [Converting](#class-Float-label-Converting)
38
+ #
39
+ #
40
+ # ### Querying
41
+ #
42
+ # #finite?
43
+ # : Returns whether `self` is finite.
44
+ #
45
+ # #hash
46
+ # : Returns the integer hash code for `self`.
47
+ #
48
+ # #infinite?
49
+ # : Returns whether `self` is infinite.
50
+ #
51
+ # #nan?
52
+ # : Returns whether `self` is a NaN (not-a-number).
53
+ #
54
+ #
55
+ #
56
+ # ### Comparing
57
+ #
58
+ # [<](#method-i-3C)
59
+ # : Returns whether `self` is less than the given value.
60
+ #
61
+ # [<=](#method-i-3C-3D)
62
+ # : Returns whether `self` is less than or equal to the given value.
63
+ #
64
+ # [<=>](#method-i-3C-3D-3E)
65
+ # : Returns a number indicating whether `self` is less than, equal to, or
66
+ # greater than the given value.
67
+ #
68
+ # [==](#method-i-3D-3D) (aliased as #=== and #eql>)
69
+ # : Returns whether `self` is equal to the given value.
70
+ #
71
+ # [>](#method-i-3E)
72
+ # : Returns whether `self` is greater than the given value.
73
+ #
74
+ # [>=](#method-i-3E-3D)
75
+ # : Returns whether `self` is greater than or equal to the given value.
76
+ #
77
+ #
78
+ #
79
+ # ### Converting
80
+ #
81
+ # #% (aliased as #modulo)
82
+ # : Returns `self` modulo the given value.
83
+ #
84
+ # #*
85
+ # : Returns the product of `self` and the given value.
86
+ #
87
+ # [**](#method-i-2A-2A)
88
+ # : Returns the value of `self` raised to the power of the given value.
89
+ #
90
+ # #+
91
+ # : Returns the sum of `self` and the given value.
92
+ #
93
+ # #-
94
+ # : Returns the difference of `self` and the given value.
95
+ #
96
+ # [/](#method-i-2F)
97
+ # : Returns the quotient of `self` and the given value.
98
+ #
99
+ # #ceil
100
+ # : Returns the smallest number greater than or equal to `self`.
101
+ #
102
+ # #coerce
103
+ # : Returns a 2-element array containing the given value converted to a
104
+ # Float and `self`
105
+ #
106
+ # #divmod
107
+ # : Returns a 2-element array containing the quotient and remainder
108
+ # results of dividing `self` by the given value.
109
+ #
110
+ # #fdiv
111
+ # : Returns the Float result of dividing `self` by the given value.
112
+ #
113
+ # #floor
114
+ # : Returns the greatest number smaller than or equal to `self`.
115
+ #
116
+ # #next_float
117
+ # : Returns the next-larger representable Float.
118
+ #
119
+ # #prev_float
120
+ # : Returns the next-smaller representable Float.
121
+ #
122
+ # #quo
123
+ # : Returns the quotient from dividing `self` by the given value.
124
+ #
125
+ # #round
126
+ # : Returns `self` rounded to the nearest value, to a given precision.
127
+ #
128
+ # #to_i (aliased as #to_int)
129
+ # : Returns `self` truncated to an Integer.
130
+ #
131
+ # #to_s (aliased as #inspect)
132
+ # : Returns a string containing the place-value representation of `self`
133
+ # in the given radix.
134
+ #
135
+ # #truncate
136
+ # : Returns `self` truncated to a given precision.
137
+ #
13
138
  class Float < Numeric
14
139
  public
15
140
 
16
- # Returns the modulo after division of `float` by `other`.
141
+ # <!--
142
+ # rdoc-file=numeric.c
143
+ # - self % other -> float
144
+ # -->
145
+ # Returns `self` modulo `other` as a float.
146
+ #
147
+ # For float `f` and real number `r`, these expressions are equivalent:
17
148
  #
18
- # 6543.21.modulo(137) #=> 104.21000000000004
19
- # 6543.21.modulo(137.24) #=> 92.92999999999961
149
+ # f % r
150
+ # f-r*(f/r).floor
151
+ # f.divmod(r)[1]
152
+ #
153
+ # See Numeric#divmod.
154
+ #
155
+ # Examples:
156
+ #
157
+ # 10.0 % 2 # => 0.0
158
+ # 10.0 % 3 # => 1.0
159
+ # 10.0 % 4 # => 2.0
160
+ #
161
+ # 10.0 % -2 # => 0.0
162
+ # 10.0 % -3 # => -2.0
163
+ # 10.0 % -4 # => -2.0
164
+ #
165
+ # 10.0 % 4.0 # => 2.0
166
+ # 10.0 % Rational(4, 1) # => 2.0
167
+ #
168
+ # Float#modulo is an alias for Float#%.
20
169
  #
21
170
  def %: (Integer) -> Float
22
171
  | (Float) -> Float
23
172
  | (Rational) -> Float
24
173
  | (Numeric) -> Numeric
25
174
 
26
- # Returns a new Float which is the product of `float` and `other`.
175
+ # <!--
176
+ # rdoc-file=numeric.c
177
+ # - self * other -> numeric
178
+ # -->
179
+ # Returns a new Float which is the product of `self` and `other`:
180
+ #
181
+ # f = 3.14
182
+ # f * 2 # => 6.28
183
+ # f * 2.0 # => 6.28
184
+ # f * Rational(1, 2) # => 1.57
185
+ # f * Complex(2, 0) # => (6.28+0.0i)
27
186
  #
28
187
  def *: (Complex) -> Complex
29
188
  | (Numeric) -> Float
30
189
 
31
- # Raises `float` to the power of `other`.
190
+ # <!--
191
+ # rdoc-file=numeric.c
192
+ # - self ** other -> numeric
193
+ # -->
194
+ # Raises `self` to the power of `other`:
32
195
  #
33
- # 2.0**3 #=> 8.0
196
+ # f = 3.14
197
+ # f ** 2 # => 9.8596
198
+ # f ** -2 # => 0.1014239928597509
199
+ # f ** 2.1 # => 11.054834900588839
200
+ # f ** Rational(2, 1) # => 9.8596
201
+ # f ** Complex(2, 0) # => (9.8596+0i)
34
202
  #
35
203
  def **: (Complex) -> Complex
36
204
  | (Numeric) -> Float
37
205
 
38
- # Returns a new Float which is the sum of `float` and `other`.
206
+ # <!--
207
+ # rdoc-file=numeric.c
208
+ # - self + other -> numeric
209
+ # -->
210
+ # Returns a new Float which is the sum of `self` and `other`:
211
+ #
212
+ # f = 3.14
213
+ # f + 1 # => 4.140000000000001
214
+ # f + 1.0 # => 4.140000000000001
215
+ # f + Rational(1, 1) # => 4.140000000000001
216
+ # f + Complex(1, 0) # => (4.140000000000001+0i)
39
217
  #
40
218
  def +: (Complex) -> Complex
41
219
  | (Numeric) -> Float
42
220
 
43
221
  def +@: () -> Float
44
222
 
45
- # Returns a new Float which is the difference of `float` and `other`.
223
+ # <!--
224
+ # rdoc-file=numeric.c
225
+ # - self - other -> numeric
226
+ # -->
227
+ # Returns a new Float which is the difference of `self` and `other`:
228
+ #
229
+ # f = 3.14
230
+ # f - 1 # => 2.14
231
+ # f - 1.0 # => 2.14
232
+ # f - Rational(1, 1) # => 2.14
233
+ # f - Complex(1, 0) # => (2.14+0i)
46
234
  #
47
235
  def -: (Complex) -> Complex
48
236
  | (Numeric) -> Float
49
237
 
238
+ # <!--
239
+ # rdoc-file=numeric.rb
240
+ # - -float -> float
241
+ # -->
50
242
  # Returns `float`, negated.
51
243
  #
52
244
  def -@: () -> Float
53
245
 
54
- # Returns a new Float which is the result of dividing `float` by `other`.
246
+ # <!--
247
+ # rdoc-file=numeric.c
248
+ # - self / other -> numeric
249
+ # -->
250
+ # Returns a new Float which is the result of dividing `self` by `other`:
251
+ #
252
+ # f = 3.14
253
+ # f / 2 # => 1.57
254
+ # f / 2.0 # => 1.57
255
+ # f / Rational(2, 1) # => 1.57
256
+ # f / Complex(2, 0) # => (1.57+0.0i)
55
257
  #
56
258
  def /: (Complex) -> Complex
57
259
  | (Numeric) -> Float
58
260
 
59
- # Returns `true` if `float` is less than `real`.
261
+ # <!--
262
+ # rdoc-file=numeric.c
263
+ # - self < other -> true or false
264
+ # -->
265
+ # Returns `true` if `self` is numerically less than `other`:
266
+ #
267
+ # 2.0 < 3 # => true
268
+ # 2.0 < 3.0 # => true
269
+ # 2.0 < Rational(3, 1) # => true
270
+ # 2.0 < 2.0 # => false
60
271
  #
61
- # The result of `NaN < NaN` is undefined, so an implementation-dependent value
62
- # is returned.
272
+ # `Float::NAN < Float::NAN` returns an implementation-dependent value.
63
273
  #
64
274
  def <: (Numeric) -> bool
65
275
 
66
- # Returns `true` if `float` is less than or equal to `real`.
276
+ # <!--
277
+ # rdoc-file=numeric.c
278
+ # - self <= other -> true or false
279
+ # -->
280
+ # Returns `true` if `self` is numerically less than or equal to `other`:
281
+ #
282
+ # 2.0 <= 3 # => true
283
+ # 2.0 <= 3.0 # => true
284
+ # 2.0 <= Rational(3, 1) # => true
285
+ # 2.0 <= 2.0 # => true
286
+ # 2.0 <= 1.0 # => false
67
287
  #
68
- # The result of `NaN <= NaN` is undefined, so an implementation-dependent value
69
- # is returned.
288
+ # `Float::NAN <= Float::NAN` returns an implementation-dependent value.
70
289
  #
71
290
  def <=: (Numeric) -> bool
72
291
 
73
- # Returns -1, 0, or +1 depending on whether `float` is less than, equal to, or
74
- # greater than `real`. This is the basis for the tests in the Comparable module.
292
+ # <!--
293
+ # rdoc-file=numeric.c
294
+ # - self <=> other -> -1, 0, +1, or nil
295
+ # -->
296
+ # Returns a value that depends on the numeric relation between `self` and
297
+ # `other`:
298
+ #
299
+ # * -1, if `self` is less than `other`.
300
+ # * 0, if `self` is equal to `other`.
301
+ # * 1, if `self` is greater than `other`.
302
+ # * `nil`, if the two values are incommensurate.
303
+ #
75
304
  #
76
- # The result of `NaN <=> NaN` is undefined, so an implementation-dependent value
77
- # is returned.
305
+ # Examples:
78
306
  #
79
- # `nil` is returned if the two values are incomparable.
307
+ # 2.0 <=> 2 # => 0
308
+ # 2.0 <=> 2.0 # => 0
309
+ # 2.0 <=> Rational(2, 1) # => 0
310
+ # 2.0 <=> Complex(2, 0) # => 0
311
+ # 2.0 <=> 1.9 # => 1
312
+ # 2.0 <=> 2.1 # => -1
313
+ # 2.0 <=> 'foo' # => nil
314
+ #
315
+ # This is the basis for the tests in the Comparable module.
316
+ #
317
+ # `Float::NAN <=> Float::NAN` returns an implementation-dependent value.
80
318
  #
81
319
  def <=>: (Numeric) -> Integer?
82
320
 
83
- # Returns `true` only if `obj` has the same value as `float`. Contrast this with
84
- # Float#eql?, which requires `obj` to be a Float.
321
+ # <!--
322
+ # rdoc-file=numeric.c
323
+ # - self == other -> true or false
324
+ # -->
325
+ # Returns `true` if `other` has the same value as `self`, `false` otherwise:
326
+ #
327
+ # 2.0 == 2 # => true
328
+ # 2.0 == 2.0 # => true
329
+ # 2.0 == Rational(2, 1) # => true
330
+ # 2.0 == Complex(2, 0) # => true
85
331
  #
86
- # 1.0 == 1 #=> true
332
+ # `Float::NAN == Float::NAN` returns an implementation-dependent value.
87
333
  #
88
- # The result of `NaN == NaN` is undefined, so an implementation-dependent value
89
- # is returned.
334
+ # Related: Float#eql? (requires `other` to be a Float).
90
335
  #
91
336
  def ==: (untyped) -> bool
92
337
 
93
- # Returns `true` only if `obj` has the same value as `float`. Contrast this with
94
- # Float#eql?, which requires `obj` to be a Float.
338
+ # <!-- rdoc-file=numeric.c -->
339
+ # Returns `true` if `other` has the same value as `self`, `false` otherwise:
95
340
  #
96
- # 1.0 == 1 #=> true
341
+ # 2.0 == 2 # => true
342
+ # 2.0 == 2.0 # => true
343
+ # 2.0 == Rational(2, 1) # => true
344
+ # 2.0 == Complex(2, 0) # => true
97
345
  #
98
- # The result of `NaN == NaN` is undefined, so an implementation-dependent value
99
- # is returned.
346
+ # `Float::NAN == Float::NAN` returns an implementation-dependent value.
347
+ #
348
+ # Related: Float#eql? (requires `other` to be a Float).
100
349
  #
101
350
  def ===: (untyped) -> bool
102
351
 
103
- # Returns `true` if `float` is greater than `real`.
352
+ # <!--
353
+ # rdoc-file=numeric.c
354
+ # - self > other -> true or false
355
+ # -->
356
+ # Returns `true` if `self` is numerically greater than `other`:
357
+ #
358
+ # 2.0 > 1 # => true
359
+ # 2.0 > 1.0 # => true
360
+ # 2.0 > Rational(1, 2) # => true
361
+ # 2.0 > 2.0 # => false
104
362
  #
105
- # The result of `NaN > NaN` is undefined, so an implementation-dependent value
106
- # is returned.
363
+ # `Float::NAN > Float::NAN` returns an implementation-dependent value.
107
364
  #
108
365
  def >: (Numeric) -> bool
109
366
 
110
- # Returns `true` if `float` is greater than or equal to `real`.
367
+ # <!--
368
+ # rdoc-file=numeric.c
369
+ # - self >= other -> true or false
370
+ # -->
371
+ # Returns `true` if `self` is numerically greater than or equal to `other`:
111
372
  #
112
- # The result of `NaN >= NaN` is undefined, so an implementation-dependent value
113
- # is returned.
373
+ # 2.0 >= 1 # => true
374
+ # 2.0 >= 1.0 # => true
375
+ # 2.0 >= Rational(1, 2) # => true
376
+ # 2.0 >= 2.0 # => true
377
+ # 2.0 >= 2.1 # => false
378
+ #
379
+ # `Float::NAN >= Float::NAN` returns an implementation-dependent value.
114
380
  #
115
381
  def >=: (Numeric) -> bool
116
382
 
383
+ # <!--
384
+ # rdoc-file=numeric.rb
385
+ # - float.abs -> float
386
+ # - float.magnitude -> float
387
+ # -->
117
388
  # Returns the absolute value of `float`.
118
389
  #
119
390
  # (-34.56).abs #=> 34.56
@@ -126,64 +397,82 @@ class Float < Numeric
126
397
 
127
398
  def abs2: () -> Float
128
399
 
400
+ # <!-- rdoc-file=complex.c -->
129
401
  # Returns 0 if the value is positive, pi otherwise.
130
402
  #
131
403
  def angle: () -> (Integer | Float)
132
404
 
405
+ # <!--
406
+ # rdoc-file=complex.c
407
+ # - flo.arg -> 0 or float
408
+ # - flo.angle -> 0 or float
409
+ # - flo.phase -> 0 or float
410
+ # -->
133
411
  # Returns 0 if the value is positive, pi otherwise.
134
412
  #
135
413
  alias arg angle
136
414
 
137
- # Returns the smallest number greater than or equal to `float` with a precision
138
- # of `ndigits` decimal digits (default: 0).
139
- #
140
- # When the precision is negative, the returned value is an integer with at least
141
- # `ndigits.abs` trailing zeros.
142
- #
143
- # Returns a floating point number when `ndigits` is positive, otherwise returns
144
- # an integer.
145
- #
146
- # 1.2.ceil #=> 2
147
- # 2.0.ceil #=> 2
148
- # (-1.2).ceil #=> -1
149
- # (-2.0).ceil #=> -2
150
- #
151
- # 1.234567.ceil(2) #=> 1.24
152
- # 1.234567.ceil(3) #=> 1.235
153
- # 1.234567.ceil(4) #=> 1.2346
154
- # 1.234567.ceil(5) #=> 1.23457
155
- #
156
- # 34567.89.ceil(-5) #=> 100000
157
- # 34567.89.ceil(-4) #=> 40000
158
- # 34567.89.ceil(-3) #=> 35000
159
- # 34567.89.ceil(-2) #=> 34600
160
- # 34567.89.ceil(-1) #=> 34570
161
- # 34567.89.ceil(0) #=> 34568
162
- # 34567.89.ceil(1) #=> 34567.9
163
- # 34567.89.ceil(2) #=> 34567.89
164
- # 34567.89.ceil(3) #=> 34567.89
165
- #
166
- # Note that the limited precision of floating point arithmetic might lead to
415
+ # <!--
416
+ # rdoc-file=numeric.c
417
+ # - ceil(ndigits = 0) -> float or integer
418
+ # -->
419
+ # Returns the smallest number greater than or equal to `self` with a precision
420
+ # of `ndigits` decimal digits.
421
+ #
422
+ # When `ndigits` is positive, returns a float with `ndigits` digits after the
423
+ # decimal point (as available):
424
+ #
425
+ # f = 12345.6789
426
+ # f.ceil(1) # => 12345.7
427
+ # f.ceil(3) # => 12345.679
428
+ # f = -12345.6789
429
+ # f.ceil(1) # => -12345.6
430
+ # f.ceil(3) # => -12345.678
431
+ #
432
+ # When `ndigits` is non-positive, returns an integer with at least `ndigits.abs`
433
+ # trailing zeros:
434
+ #
435
+ # f = 12345.6789
436
+ # f.ceil(0) # => 12346
437
+ # f.ceil(-3) # => 13000
438
+ # f = -12345.6789
439
+ # f.ceil(0) # => -12345
440
+ # f.ceil(-3) # => -12000
441
+ #
442
+ # Note that the limited precision of floating-point arithmetic may lead to
167
443
  # surprising results:
168
444
  #
169
445
  # (2.1 / 0.7).ceil #=> 4 (!)
170
446
  #
447
+ # Related: Float#floor.
448
+ #
171
449
  def ceil: () -> Integer
172
450
  | (int digits) -> (Integer | Float)
173
451
 
174
- # Returns an array with both `numeric` and `float` represented as Float objects.
452
+ # <!--
453
+ # rdoc-file=numeric.c
454
+ # - coerce(other) -> array
455
+ # -->
456
+ # Returns a 2-element array containing `other` converted to a Float and `self`:
175
457
  #
176
- # This is achieved by converting `numeric` to a Float.
458
+ # f = 3.14 # => 3.14
459
+ # f.coerce(2) # => [2.0, 3.14]
460
+ # f.coerce(2.0) # => [2.0, 3.14]
461
+ # f.coerce(Rational(1, 2)) # => [0.5, 3.14]
462
+ # f.coerce(Complex(1, 0)) # => [1.0, 3.14]
177
463
  #
178
- # 1.2.coerce(3) #=> [3.0, 1.2]
179
- # 2.5.coerce(1.1) #=> [1.1, 2.5]
464
+ # Raises an exception if a type conversion fails.
180
465
  #
181
- def coerce: (Numeric) -> [Float, Float]
466
+ def coerce: (Numeric) -> [ Float, Float ]
182
467
 
183
468
  def conj: () -> Float
184
469
 
185
470
  def conjugate: () -> Float
186
471
 
472
+ # <!--
473
+ # rdoc-file=rational.c
474
+ # - flo.denominator -> integer
475
+ # -->
187
476
  # Returns the denominator (always positive). The result is machine dependent.
188
477
  #
189
478
  # See also Float#numerator.
@@ -192,73 +481,127 @@ class Float < Numeric
192
481
 
193
482
  def div: (Numeric) -> Integer
194
483
 
195
- # See Numeric#divmod.
484
+ # <!--
485
+ # rdoc-file=numeric.c
486
+ # - divmod(other) -> array
487
+ # -->
488
+ # Returns a 2-element array `[q, r]`, where
196
489
  #
197
- # 42.0.divmod(6) #=> [7, 0.0]
198
- # 42.0.divmod(5) #=> [8, 2.0]
490
+ # q = (self/other).floor # Quotient
491
+ # r = self % other # Remainder
199
492
  #
200
- def divmod: (Numeric) -> [Numeric, Numeric]
493
+ # Examples:
494
+ #
495
+ # 11.0.divmod(4) # => [2, 3.0]
496
+ # 11.0.divmod(-4) # => [-3, -1.0]
497
+ # -11.0.divmod(4) # => [-3, 1.0]
498
+ # -11.0.divmod(-4) # => [2, -3.0]
499
+ #
500
+ # 12.0.divmod(4) # => [3, 0.0]
501
+ # 12.0.divmod(-4) # => [-3, 0.0]
502
+ # -12.0.divmod(4) # => [-3, -0.0]
503
+ # -12.0.divmod(-4) # => [3, -0.0]
504
+ #
505
+ # 13.0.divmod(4.0) # => [3, 1.0]
506
+ # 13.0.divmod(Rational(4, 1)) # => [3, 1.0]
507
+ #
508
+ def divmod: (Numeric) -> [ Numeric, Numeric ]
201
509
 
202
510
  def dup: () -> self
203
511
 
204
- # Returns `true` only if `obj` is a Float with the same value as `float`.
205
- # Contrast this with Float#==, which performs type conversions.
512
+ # <!--
513
+ # rdoc-file=numeric.c
514
+ # - eql?(other) -> true or false
515
+ # -->
516
+ # Returns `true` if `other` is a Float with the same value as `self`, `false`
517
+ # otherwise:
518
+ #
519
+ # 2.0.eql?(2.0) # => true
520
+ # 2.0.eql?(1.0) # => false
521
+ # 2.0.eql?(1) # => false
522
+ # 2.0.eql?(Rational(2, 1)) # => false
523
+ # 2.0.eql?(Complex(2, 0)) # => false
206
524
  #
207
- # 1.0.eql?(1) #=> false
525
+ # `Float::NAN.eql?(Float::NAN)` returns an implementation-dependent value.
208
526
  #
209
- # The result of `NaN.eql?(NaN)` is undefined, so an implementation-dependent
210
- # value is returned.
527
+ # Related: Float#== (performs type conversions).
211
528
  #
212
529
  def eql?: (untyped) -> bool
213
530
 
214
- # Returns `float / numeric`, same as Float#/.
531
+ # <!-- rdoc-file=numeric.c -->
532
+ # Returns the quotient from dividing `self` by `other`:
533
+ #
534
+ # f = 3.14
535
+ # f.quo(2) # => 1.57
536
+ # f.quo(-2) # => -1.57
537
+ # f.quo(Rational(2, 1)) # => 1.57
538
+ # f.quo(Complex(2, 0)) # => (1.57+0.0i)
539
+ #
540
+ # Float#fdiv is an alias for Float#quo.
215
541
  #
216
542
  def fdiv: (Complex) -> Complex
217
543
  | (Numeric) -> Float
218
544
 
219
- # Returns `true` if `float` is a valid IEEE floating point number, i.e. it is
220
- # not infinite and Float#nan? is `false`.
545
+ # <!--
546
+ # rdoc-file=numeric.c
547
+ # - finite? -> true or false
548
+ # -->
549
+ # Returns `true` if `self` is not `Infinity`, `-Infinity`, or `Nan`, `false`
550
+ # otherwise:
551
+ #
552
+ # f = 2.0 # => 2.0
553
+ # f.finite? # => true
554
+ # f = 1.0/0.0 # => Infinity
555
+ # f.finite? # => false
556
+ # f = -1.0/0.0 # => -Infinity
557
+ # f.finite? # => false
558
+ # f = 0.0/0.0 # => NaN
559
+ # f.finite? # => false
221
560
  #
222
561
  def finite?: () -> bool
223
562
 
224
- # Returns the largest number less than or equal to `float` with a precision of
225
- # `ndigits` decimal digits (default: 0).
226
- #
227
- # When the precision is negative, the returned value is an integer with at least
228
- # `ndigits.abs` trailing zeros.
229
- #
230
- # Returns a floating point number when `ndigits` is positive, otherwise returns
231
- # an integer.
232
- #
233
- # 1.2.floor #=> 1
234
- # 2.0.floor #=> 2
235
- # (-1.2).floor #=> -2
236
- # (-2.0).floor #=> -2
237
- #
238
- # 1.234567.floor(2) #=> 1.23
239
- # 1.234567.floor(3) #=> 1.234
240
- # 1.234567.floor(4) #=> 1.2345
241
- # 1.234567.floor(5) #=> 1.23456
242
- #
243
- # 34567.89.floor(-5) #=> 0
244
- # 34567.89.floor(-4) #=> 30000
245
- # 34567.89.floor(-3) #=> 34000
246
- # 34567.89.floor(-2) #=> 34500
247
- # 34567.89.floor(-1) #=> 34560
248
- # 34567.89.floor(0) #=> 34567
249
- # 34567.89.floor(1) #=> 34567.8
250
- # 34567.89.floor(2) #=> 34567.89
251
- # 34567.89.floor(3) #=> 34567.89
252
- #
253
- # Note that the limited precision of floating point arithmetic might lead to
563
+ # <!--
564
+ # rdoc-file=numeric.c
565
+ # - floor(ndigits = 0) -> float or integer
566
+ # -->
567
+ # Returns the largest number less than or equal to `self` with a precision of
568
+ # `ndigits` decimal digits.
569
+ #
570
+ # When `ndigits` is positive, returns a float with `ndigits` digits after the
571
+ # decimal point (as available):
572
+ #
573
+ # f = 12345.6789
574
+ # f.floor(1) # => 12345.6
575
+ # f.floor(3) # => 12345.678
576
+ # f = -12345.6789
577
+ # f.floor(1) # => -12345.7
578
+ # f.floor(3) # => -12345.679
579
+ #
580
+ # When `ndigits` is non-positive, returns an integer with at least `ndigits.abs`
581
+ # trailing zeros:
582
+ #
583
+ # f = 12345.6789
584
+ # f.floor(0) # => 12345
585
+ # f.floor(-3) # => 12000
586
+ # f = -12345.6789
587
+ # f.floor(0) # => -12346
588
+ # f.floor(-3) # => -13000
589
+ #
590
+ # Note that the limited precision of floating-point arithmetic may lead to
254
591
  # surprising results:
255
592
  #
256
593
  # (0.3 / 0.1).floor #=> 2 (!)
257
594
  #
595
+ # Related: Float#ceil.
596
+ #
258
597
  def floor: () -> Integer
259
598
  | (int digits) -> (Integer | Numeric)
260
599
 
261
- # Returns a hash code for this float.
600
+ # <!--
601
+ # rdoc-file=numeric.c
602
+ # - hash -> integer
603
+ # -->
604
+ # Returns the integer hash value for `self`.
262
605
  #
263
606
  # See also Object#hash.
264
607
  #
@@ -270,101 +613,155 @@ class Float < Numeric
270
613
 
271
614
  def imaginary: () -> Integer
272
615
 
273
- # Returns `nil`, -1, or 1 depending on whether the value is finite, `-Infinity`,
274
- # or `+Infinity`.
616
+ # <!--
617
+ # rdoc-file=numeric.c
618
+ # - infinite? -> -1, 1, or nil
619
+ # -->
620
+ # Returns:
621
+ #
622
+ # * 1, if `self` is `Infinity`.
623
+ # * -1 if `self` is `-Infinity`.
624
+ # * `nil`, otherwise.
275
625
  #
276
- # (0.0).infinite? #=> nil
277
- # (-1.0/0.0).infinite? #=> -1
278
- # (+1.0/0.0).infinite? #=> 1
626
+ #
627
+ # Examples:
628
+ #
629
+ # f = 1.0/0.0 # => Infinity
630
+ # f.infinite? # => 1
631
+ # f = -1.0/0.0 # => -Infinity
632
+ # f.infinite? # => -1
633
+ # f = 1.0 # => 1.0
634
+ # f.infinite? # => nil
635
+ # f = 0.0/0.0 # => NaN
636
+ # f.infinite? # => nil
279
637
  #
280
638
  def infinite?: () -> Integer?
281
639
 
640
+ # <!-- rdoc-file=numeric.c -->
641
+ # Returns a string containing a representation of `self`; depending of the value
642
+ # of `self`, the string representation may contain:
643
+ #
644
+ # * A fixed-point number.
645
+ # * A number in "scientific notation" (containing an exponent).
646
+ # * 'Infinity'.
647
+ # * '-Infinity'.
648
+ # * 'NaN' (indicating not-a-number).
649
+ #
650
+ # 3.14.to_s # => "3.14" (10.1**50).to_s # =>
651
+ # "1.644631821843879e+50" (10.1**500).to_s # => "Infinity"
652
+ # (-10.1**500).to_s # => "-Infinity" (0.0/0.0).to_s # => "NaN"
653
+ #
282
654
  alias inspect to_s
283
655
 
284
656
  def integer?: () -> bool
285
657
 
286
- # Returns the absolute value of `float`.
287
- #
288
- # (-34.56).abs #=> 34.56
289
- # -34.56.abs #=> 34.56
290
- # 34.56.abs #=> 34.56
291
- #
292
- # Float#magnitude is an alias for Float#abs.
658
+ # <!--
659
+ # rdoc-file=numeric.rb
660
+ # - magnitude()
661
+ # -->
293
662
  #
294
663
  alias magnitude abs
295
664
 
296
- # Returns the modulo after division of `float` by `other`.
665
+ # <!-- rdoc-file=numeric.c -->
666
+ # Returns `self` modulo `other` as a float.
667
+ #
668
+ # For float `f` and real number `r`, these expressions are equivalent:
297
669
  #
298
- # 6543.21.modulo(137) #=> 104.21000000000004
299
- # 6543.21.modulo(137.24) #=> 92.92999999999961
670
+ # f % r
671
+ # f-r*(f/r).floor
672
+ # f.divmod(r)[1]
673
+ #
674
+ # See Numeric#divmod.
675
+ #
676
+ # Examples:
677
+ #
678
+ # 10.0 % 2 # => 0.0
679
+ # 10.0 % 3 # => 1.0
680
+ # 10.0 % 4 # => 2.0
681
+ #
682
+ # 10.0 % -2 # => 0.0
683
+ # 10.0 % -3 # => -2.0
684
+ # 10.0 % -4 # => -2.0
685
+ #
686
+ # 10.0 % 4.0 # => 2.0
687
+ # 10.0 % Rational(4, 1) # => 2.0
688
+ #
689
+ # Float#modulo is an alias for Float#%.
300
690
  #
301
691
  def modulo: (Numeric) -> Float
302
692
 
303
- # Returns `true` if `float` is an invalid IEEE floating point number.
693
+ # <!--
694
+ # rdoc-file=numeric.c
695
+ # - nan? -> true or false
696
+ # -->
697
+ # Returns `true` if `self` is a NaN, `false` otherwise.
304
698
  #
305
- # a = -1.0 #=> -1.0
306
- # a.nan? #=> false
307
- # a = 0.0/0.0 #=> NaN
308
- # a.nan? #=> true
699
+ # f = -1.0 #=> -1.0
700
+ # f.nan? #=> false
701
+ # f = 0.0/0.0 #=> NaN
702
+ # f.nan? #=> true
309
703
  #
310
704
  def nan?: () -> bool
311
705
 
706
+ # <!--
707
+ # rdoc-file=numeric.rb
708
+ # - float.negative? -> true or false
709
+ # -->
312
710
  # Returns `true` if `float` is less than 0.
313
711
  #
314
712
  def negative?: () -> bool
315
713
 
316
- # Returns the next representable floating point number.
317
- #
318
- # Float::MAX.next_float and Float::INFINITY.next_float is Float::INFINITY.
319
- #
320
- # Float::NAN.next_float is Float::NAN.
321
- #
322
- # For example:
323
- #
324
- # 0.01.next_float #=> 0.010000000000000002
325
- # 1.0.next_float #=> 1.0000000000000002
326
- # 100.0.next_float #=> 100.00000000000001
327
- #
328
- # 0.01.next_float - 0.01 #=> 1.734723475976807e-18
329
- # 1.0.next_float - 1.0 #=> 2.220446049250313e-16
330
- # 100.0.next_float - 100.0 #=> 1.4210854715202004e-14
331
- #
332
- # f = 0.01; 20.times { printf "%-20a %s\n", f, f.to_s; f = f.next_float }
333
- # #=> 0x1.47ae147ae147bp-7 0.01
334
- # # 0x1.47ae147ae147cp-7 0.010000000000000002
335
- # # 0x1.47ae147ae147dp-7 0.010000000000000004
336
- # # 0x1.47ae147ae147ep-7 0.010000000000000005
337
- # # 0x1.47ae147ae147fp-7 0.010000000000000007
338
- # # 0x1.47ae147ae148p-7 0.010000000000000009
339
- # # 0x1.47ae147ae1481p-7 0.01000000000000001
340
- # # 0x1.47ae147ae1482p-7 0.010000000000000012
341
- # # 0x1.47ae147ae1483p-7 0.010000000000000014
342
- # # 0x1.47ae147ae1484p-7 0.010000000000000016
343
- # # 0x1.47ae147ae1485p-7 0.010000000000000018
344
- # # 0x1.47ae147ae1486p-7 0.01000000000000002
345
- # # 0x1.47ae147ae1487p-7 0.010000000000000021
346
- # # 0x1.47ae147ae1488p-7 0.010000000000000023
347
- # # 0x1.47ae147ae1489p-7 0.010000000000000024
348
- # # 0x1.47ae147ae148ap-7 0.010000000000000026
349
- # # 0x1.47ae147ae148bp-7 0.010000000000000028
350
- # # 0x1.47ae147ae148cp-7 0.01000000000000003
351
- # # 0x1.47ae147ae148dp-7 0.010000000000000031
352
- # # 0x1.47ae147ae148ep-7 0.010000000000000033
353
- #
354
- # f = 0.0
355
- # 100.times { f += 0.1 }
356
- # f #=> 9.99999999999998 # should be 10.0 in the ideal world.
357
- # 10-f #=> 1.9539925233402755e-14 # the floating point error.
358
- # 10.0.next_float-10 #=> 1.7763568394002505e-15 # 1 ulp (unit in the last place).
359
- # (10-f)/(10.0.next_float-10) #=> 11.0 # the error is 11 ulp.
360
- # (10-f)/(10*Float::EPSILON) #=> 8.8 # approximation of the above.
361
- # "%a" % 10 #=> "0x1.4p+3"
362
- # "%a" % f #=> "0x1.3fffffffffff5p+3" # the last hex digit is 5. 16 - 5 = 11 ulp.
714
+ # <!--
715
+ # rdoc-file=numeric.c
716
+ # - next_float -> float
717
+ # -->
718
+ # Returns the next-larger representable Float.
719
+ #
720
+ # These examples show the internally stored values (64-bit hexadecimal) for each
721
+ # Float `f` and for the corresponding `f.next_float`:
722
+ #
723
+ # f = 0.0 # 0x0000000000000000
724
+ # f.next_float # 0x0000000000000001
725
+ #
726
+ # f = 0.01 # 0x3f847ae147ae147b
727
+ # f.next_float # 0x3f847ae147ae147c
728
+ #
729
+ # In the remaining examples here, the output is shown in the usual way (result
730
+ # `to_s`):
731
+ #
732
+ # 0.01.next_float # => 0.010000000000000002
733
+ # 1.0.next_float # => 1.0000000000000002
734
+ # 100.0.next_float # => 100.00000000000001
735
+ #
736
+ # f = 0.01
737
+ # (0..3).each_with_index {|i| printf "%2d %-20a %s\n", i, f, f.to_s; f = f.next_float }
738
+ #
739
+ # Output:
740
+ #
741
+ # 0 0x1.47ae147ae147bp-7 0.01
742
+ # 1 0x1.47ae147ae147cp-7 0.010000000000000002
743
+ # 2 0x1.47ae147ae147dp-7 0.010000000000000004
744
+ # 3 0x1.47ae147ae147ep-7 0.010000000000000005
745
+ #
746
+ # f = 0.0; 100.times { f += 0.1 }
747
+ # f # => 9.99999999999998 # should be 10.0 in the ideal world.
748
+ # 10-f # => 1.9539925233402755e-14 # the floating point error.
749
+ # 10.0.next_float-10 # => 1.7763568394002505e-15 # 1 ulp (unit in the last place).
750
+ # (10-f)/(10.0.next_float-10) # => 11.0 # the error is 11 ulp.
751
+ # (10-f)/(10*Float::EPSILON) # => 8.8 # approximation of the above.
752
+ # "%a" % 10 # => "0x1.4p+3"
753
+ # "%a" % f # => "0x1.3fffffffffff5p+3" # the last hex digit is 5. 16 - 5 = 11 ulp.
754
+ #
755
+ # Related: Float#prev_float
363
756
  #
364
757
  def next_float: () -> Float
365
758
 
366
759
  def nonzero?: () -> self?
367
760
 
761
+ # <!--
762
+ # rdoc-file=rational.c
763
+ # - flo.numerator -> integer
764
+ # -->
368
765
  # Returns the numerator. The result is machine dependent.
369
766
  #
370
767
  # n = 0.3.numerator #=> 5404319552844595
@@ -375,62 +772,78 @@ class Float < Numeric
375
772
  #
376
773
  def numerator: () -> Integer
377
774
 
775
+ # <!-- rdoc-file=complex.c -->
378
776
  # Returns 0 if the value is positive, pi otherwise.
379
777
  #
380
778
  alias phase angle
381
779
 
382
780
  def polar: () -> [ Float, Integer | Float ]
383
781
 
782
+ # <!--
783
+ # rdoc-file=numeric.rb
784
+ # - float.positive? -> true or false
785
+ # -->
384
786
  # Returns `true` if `float` is greater than 0.
385
787
  #
386
788
  def positive?: () -> bool
387
789
 
388
- # Returns the previous representable floating point number.
389
- #
390
- # (-Float::MAX).prev_float and (-Float::INFINITY).prev_float is
391
- # -Float::INFINITY.
392
- #
393
- # Float::NAN.prev_float is Float::NAN.
394
- #
395
- # For example:
396
- #
397
- # 0.01.prev_float #=> 0.009999999999999998
398
- # 1.0.prev_float #=> 0.9999999999999999
399
- # 100.0.prev_float #=> 99.99999999999999
400
- #
401
- # 0.01 - 0.01.prev_float #=> 1.734723475976807e-18
402
- # 1.0 - 1.0.prev_float #=> 1.1102230246251565e-16
403
- # 100.0 - 100.0.prev_float #=> 1.4210854715202004e-14
404
- #
405
- # f = 0.01; 20.times { printf "%-20a %s\n", f, f.to_s; f = f.prev_float }
406
- # #=> 0x1.47ae147ae147bp-7 0.01
407
- # # 0x1.47ae147ae147ap-7 0.009999999999999998
408
- # # 0x1.47ae147ae1479p-7 0.009999999999999997
409
- # # 0x1.47ae147ae1478p-7 0.009999999999999995
410
- # # 0x1.47ae147ae1477p-7 0.009999999999999993
411
- # # 0x1.47ae147ae1476p-7 0.009999999999999992
412
- # # 0x1.47ae147ae1475p-7 0.00999999999999999
413
- # # 0x1.47ae147ae1474p-7 0.009999999999999988
414
- # # 0x1.47ae147ae1473p-7 0.009999999999999986
415
- # # 0x1.47ae147ae1472p-7 0.009999999999999985
416
- # # 0x1.47ae147ae1471p-7 0.009999999999999983
417
- # # 0x1.47ae147ae147p-7 0.009999999999999981
418
- # # 0x1.47ae147ae146fp-7 0.00999999999999998
419
- # # 0x1.47ae147ae146ep-7 0.009999999999999978
420
- # # 0x1.47ae147ae146dp-7 0.009999999999999976
421
- # # 0x1.47ae147ae146cp-7 0.009999999999999974
422
- # # 0x1.47ae147ae146bp-7 0.009999999999999972
423
- # # 0x1.47ae147ae146ap-7 0.00999999999999997
424
- # # 0x1.47ae147ae1469p-7 0.009999999999999969
425
- # # 0x1.47ae147ae1468p-7 0.009999999999999967
790
+ # <!--
791
+ # rdoc-file=numeric.c
792
+ # - float.prev_float -> float
793
+ # -->
794
+ # Returns the next-smaller representable Float.
795
+ #
796
+ # These examples show the internally stored values (64-bit hexadecimal) for each
797
+ # Float `f` and for the corresponding `f.pev_float`:
798
+ #
799
+ # f = 5e-324 # 0x0000000000000001
800
+ # f.prev_float # 0x0000000000000000
801
+ #
802
+ # f = 0.01 # 0x3f847ae147ae147b
803
+ # f.prev_float # 0x3f847ae147ae147a
804
+ #
805
+ # In the remaining examples here, the output is shown in the usual way (result
806
+ # `to_s`):
807
+ #
808
+ # 0.01.prev_float # => 0.009999999999999998
809
+ # 1.0.prev_float # => 0.9999999999999999
810
+ # 100.0.prev_float # => 99.99999999999999
811
+ #
812
+ # f = 0.01
813
+ # (0..3).each_with_index {|i| printf "%2d %-20a %s\n", i, f, f.to_s; f = f.prev_float }
814
+ #
815
+ # Output:
816
+ #
817
+ # 0 0x1.47ae147ae147bp-7 0.01
818
+ # 1 0x1.47ae147ae147ap-7 0.009999999999999998
819
+ # 2 0x1.47ae147ae1479p-7 0.009999999999999997
820
+ # 3 0x1.47ae147ae1478p-7 0.009999999999999995
821
+ #
822
+ # Related: Float#next_float.
426
823
  #
427
824
  def prev_float: () -> Float
428
825
 
429
- # Returns `float / numeric`, same as Float#/.
826
+ # <!--
827
+ # rdoc-file=numeric.c
828
+ # - quo(other) -> numeric
829
+ # -->
830
+ # Returns the quotient from dividing `self` by `other`:
831
+ #
832
+ # f = 3.14
833
+ # f.quo(2) # => 1.57
834
+ # f.quo(-2) # => -1.57
835
+ # f.quo(Rational(2, 1)) # => 1.57
836
+ # f.quo(Complex(2, 0)) # => (1.57+0.0i)
837
+ #
838
+ # Float#fdiv is an alias for Float#quo.
430
839
  #
431
840
  def quo: (Complex) -> Complex
432
841
  | (Numeric) -> Float
433
842
 
843
+ # <!--
844
+ # rdoc-file=rational.c
845
+ # - flt.rationalize([eps]) -> rational
846
+ # -->
434
847
  # Returns a simpler approximation of the value (flt-|eps| <= result <=
435
848
  # flt+|eps|). If the optional argument `eps` is not given, it will be chosen
436
849
  # automatically.
@@ -453,52 +866,58 @@ class Float < Numeric
453
866
 
454
867
  def remainder: (Numeric) -> Float
455
868
 
456
- # Returns `float` rounded to the nearest value with a precision of `ndigits`
457
- # decimal digits (default: 0).
458
- #
459
- # When the precision is negative, the returned value is an integer with at least
460
- # `ndigits.abs` trailing zeros.
461
- #
462
- # Returns a floating point number when `ndigits` is positive, otherwise returns
463
- # an integer.
464
- #
465
- # 1.4.round #=> 1
466
- # 1.5.round #=> 2
467
- # 1.6.round #=> 2
468
- # (-1.5).round #=> -2
469
- #
470
- # 1.234567.round(2) #=> 1.23
471
- # 1.234567.round(3) #=> 1.235
472
- # 1.234567.round(4) #=> 1.2346
473
- # 1.234567.round(5) #=> 1.23457
474
- #
475
- # 34567.89.round(-5) #=> 0
476
- # 34567.89.round(-4) #=> 30000
477
- # 34567.89.round(-3) #=> 35000
478
- # 34567.89.round(-2) #=> 34600
479
- # 34567.89.round(-1) #=> 34570
480
- # 34567.89.round(0) #=> 34568
481
- # 34567.89.round(1) #=> 34567.9
482
- # 34567.89.round(2) #=> 34567.89
483
- # 34567.89.round(3) #=> 34567.89
484
- #
485
- # If the optional `half` keyword argument is given, numbers that are half-way
486
- # between two possible rounded values will be rounded according to the specified
487
- # tie-breaking `mode`:
488
- #
489
- # * `:up` or `nil`: round half away from zero (default)
490
- # * `:down`: round half toward zero
491
- # * `:even`: round half toward the nearest even number
492
- #
493
- # 2.5.round(half: :up) #=> 3
494
- # 2.5.round(half: :down) #=> 2
495
- # 2.5.round(half: :even) #=> 2
496
- # 3.5.round(half: :up) #=> 4
497
- # 3.5.round(half: :down) #=> 3
498
- # 3.5.round(half: :even) #=> 4
499
- # (-2.5).round(half: :up) #=> -3
500
- # (-2.5).round(half: :down) #=> -2
501
- # (-2.5).round(half: :even) #=> -2
869
+ # <!--
870
+ # rdoc-file=numeric.c
871
+ # - round(ndigits = 0, half: :up]) -> integer or float
872
+ # -->
873
+ # Returns `self` rounded to the nearest value with a precision of `ndigits`
874
+ # decimal digits.
875
+ #
876
+ # When `ndigits` is non-negative, returns a float with `ndigits` after the
877
+ # decimal point (as available):
878
+ #
879
+ # f = 12345.6789
880
+ # f.round(1) # => 12345.7
881
+ # f.round(3) # => 12345.679
882
+ # f = -12345.6789
883
+ # f.round(1) # => -12345.7
884
+ # f.round(3) # => -12345.679
885
+ #
886
+ # When `ndigits` is negative, returns an integer with at least `ndigits.abs`
887
+ # trailing zeros:
888
+ #
889
+ # f = 12345.6789
890
+ # f.round(0) # => 12346
891
+ # f.round(-3) # => 12000
892
+ # f = -12345.6789
893
+ # f.round(0) # => -12346
894
+ # f.round(-3) # => -12000
895
+ #
896
+ # If keyword argument `half` is given, and `self` is equidistant from the two
897
+ # candidate values, the rounding is according to the given `half` value:
898
+ #
899
+ # * `:up` or `nil`: round away from zero:
900
+ #
901
+ # 2.5.round(half: :up) # => 3
902
+ # 3.5.round(half: :up) # => 4
903
+ # (-2.5).round(half: :up) # => -3
904
+ #
905
+ # * `:down`: round toward zero:
906
+ #
907
+ # 2.5.round(half: :down) # => 2
908
+ # 3.5.round(half: :down) # => 3
909
+ # (-2.5).round(half: :down) # => -2
910
+ #
911
+ # * `:even`: round toward the candidate whose last nonzero digit is even:
912
+ #
913
+ # 2.5.round(half: :even) # => 2
914
+ # 3.5.round(half: :even) # => 4
915
+ # (-2.5).round(half: :even) # => -2
916
+ #
917
+ #
918
+ # Raises and exception if the value for `half` is invalid.
919
+ #
920
+ # Related: Float#truncate.
502
921
  #
503
922
  def round: (?half: :up | :down | :even) -> Integer
504
923
  | (int digits, ?half: :up | :down | :even) -> (Integer | Float)
@@ -510,38 +929,51 @@ class Float < Numeric
510
929
 
511
930
  def to_c: () -> Complex
512
931
 
932
+ # <!--
933
+ # rdoc-file=numeric.rb
934
+ # - float.to_f -> self
935
+ # -->
513
936
  # Since `float` is already a Float, returns `self`.
514
937
  #
515
938
  def to_f: () -> Float
516
939
 
517
- # Returns the `float` truncated to an Integer.
940
+ # <!--
941
+ # rdoc-file=numeric.c
942
+ # - to_i -> integer
943
+ # -->
944
+ # Returns `self` truncated to an Integer.
518
945
  #
519
- # 1.2.to_i #=> 1
520
- # (-1.2).to_i #=> -1
946
+ # 1.2.to_i # => 1
947
+ # (-1.2).to_i # => -1
521
948
  #
522
- # Note that the limited precision of floating point arithmetic might lead to
949
+ # Note that the limited precision of floating-point arithmetic may lead to
523
950
  # surprising results:
524
951
  #
525
- # (0.3 / 0.1).to_i #=> 2 (!)
952
+ # (0.3 / 0.1).to_i # => 2 (!)
526
953
  #
527
- # #to_int is an alias for #to_i.
954
+ # Float#to_int is an alias for Float#to_i.
528
955
  #
529
956
  def to_i: () -> Integer
530
957
 
531
- # Returns the `float` truncated to an Integer.
958
+ # <!-- rdoc-file=numeric.c -->
959
+ # Returns `self` truncated to an Integer.
532
960
  #
533
- # 1.2.to_i #=> 1
534
- # (-1.2).to_i #=> -1
961
+ # 1.2.to_i # => 1
962
+ # (-1.2).to_i # => -1
535
963
  #
536
- # Note that the limited precision of floating point arithmetic might lead to
964
+ # Note that the limited precision of floating-point arithmetic may lead to
537
965
  # surprising results:
538
966
  #
539
- # (0.3 / 0.1).to_i #=> 2 (!)
967
+ # (0.3 / 0.1).to_i # => 2 (!)
540
968
  #
541
- # #to_int is an alias for #to_i.
969
+ # Float#to_int is an alias for Float#to_i.
542
970
  #
543
971
  alias to_int to_i
544
972
 
973
+ # <!--
974
+ # rdoc-file=rational.c
975
+ # - flt.to_r -> rational
976
+ # -->
545
977
  # Returns the value as a rational.
546
978
  #
547
979
  # 2.0.to_r #=> (2/1)
@@ -560,39 +992,72 @@ class Float < Numeric
560
992
  #
561
993
  def to_r: () -> Rational
562
994
 
563
- # Returns a string containing a representation of `self`. As well as a fixed or
564
- # exponential form of the `float`, the call may return `NaN`, `Infinity`, and
565
- # `-Infinity`.
566
- #
567
- def to_s: () -> String
568
-
569
- # Returns `float` truncated (toward zero) to a precision of `ndigits` decimal
570
- # digits (default: 0).
995
+ # <!--
996
+ # rdoc-file=numeric.c
997
+ # - to_s -> string
998
+ # -->
999
+ # Returns a string containing a representation of `self`; depending of the value
1000
+ # of `self`, the string representation may contain:
571
1001
  #
572
- # When the precision is negative, the returned value is an integer with at least
573
- # `ndigits.abs` trailing zeros.
1002
+ # * A fixed-point number.
1003
+ # * A number in "scientific notation" (containing an exponent).
1004
+ # * 'Infinity'.
1005
+ # * '-Infinity'.
1006
+ # * 'NaN' (indicating not-a-number).
574
1007
  #
575
- # Returns a floating point number when `ndigits` is positive, otherwise returns
576
- # an integer.
1008
+ # 3.14.to_s # => "3.14" (10.1**50).to_s # =>
1009
+ # "1.644631821843879e+50" (10.1**500).to_s # => "Infinity"
1010
+ # (-10.1**500).to_s # => "-Infinity" (0.0/0.0).to_s # => "NaN"
577
1011
  #
578
- # 2.8.truncate #=> 2
579
- # (-2.8).truncate #=> -2
580
- # 1.234567.truncate(2) #=> 1.23
581
- # 34567.89.truncate(-2) #=> 34500
582
- #
583
- # Note that the limited precision of floating point arithmetic might lead to
1012
+ def to_s: () -> String
1013
+
1014
+ # <!--
1015
+ # rdoc-file=numeric.c
1016
+ # - truncate(ndigits = 0) -> float or integer
1017
+ # -->
1018
+ # Returns `self` truncated (toward zero) to a precision of `ndigits` decimal
1019
+ # digits.
1020
+ #
1021
+ # When `ndigits` is positive, returns a float with `ndigits` digits after the
1022
+ # decimal point (as available):
1023
+ #
1024
+ # f = 12345.6789
1025
+ # f.truncate(1) # => 12345.6
1026
+ # f.truncate(3) # => 12345.678
1027
+ # f = -12345.6789
1028
+ # f.truncate(1) # => -12345.6
1029
+ # f.truncate(3) # => -12345.678
1030
+ #
1031
+ # When `ndigits` is negative, returns an integer with at least `ndigits.abs`
1032
+ # trailing zeros:
1033
+ #
1034
+ # f = 12345.6789
1035
+ # f.truncate(0) # => 12345
1036
+ # f.truncate(-3) # => 12000
1037
+ # f = -12345.6789
1038
+ # f.truncate(0) # => -12345
1039
+ # f.truncate(-3) # => -12000
1040
+ #
1041
+ # Note that the limited precision of floating-point arithmetic may lead to
584
1042
  # surprising results:
585
1043
  #
586
1044
  # (0.3 / 0.1).truncate #=> 2 (!)
587
1045
  #
1046
+ # Related: Float#round.
1047
+ #
588
1048
  def truncate: () -> Integer
589
1049
  | (Integer ndigits) -> (Integer | Float)
590
1050
 
1051
+ # <!--
1052
+ # rdoc-file=numeric.rb
1053
+ # - float.zero? -> true or false
1054
+ # -->
591
1055
  # Returns `true` if `float` is 0.0.
592
1056
  #
593
1057
  def zero?: () -> bool
594
1058
  end
595
1059
 
1060
+ # <!-- rdoc-file=numeric.c -->
596
1061
  # The minimum number of significant decimal digits in a double-precision
597
1062
  # floating point.
598
1063
  #
@@ -600,6 +1065,7 @@ end
600
1065
  #
601
1066
  Float::DIG: Integer
602
1067
 
1068
+ # <!-- rdoc-file=numeric.c -->
603
1069
  # The difference between 1 and the smallest double-precision floating point
604
1070
  # number greater than 1.
605
1071
  #
@@ -607,22 +1073,26 @@ Float::DIG: Integer
607
1073
  #
608
1074
  Float::EPSILON: Float
609
1075
 
1076
+ # <!-- rdoc-file=numeric.c -->
610
1077
  # An expression representing positive infinity.
611
1078
  #
612
1079
  Float::INFINITY: Float
613
1080
 
1081
+ # <!-- rdoc-file=numeric.c -->
614
1082
  # The number of base digits for the `double` data type.
615
1083
  #
616
1084
  # Usually defaults to 53.
617
1085
  #
618
1086
  Float::MANT_DIG: Integer
619
1087
 
1088
+ # <!-- rdoc-file=numeric.c -->
620
1089
  # The largest possible integer in a double-precision floating point number.
621
1090
  #
622
1091
  # Usually defaults to 1.7976931348623157e+308.
623
1092
  #
624
1093
  Float::MAX: Float
625
1094
 
1095
+ # <!-- rdoc-file=numeric.c -->
626
1096
  # The largest positive exponent in a double-precision floating point where 10
627
1097
  # raised to this power minus 1.
628
1098
  #
@@ -630,12 +1100,14 @@ Float::MAX: Float
630
1100
  #
631
1101
  Float::MAX_10_EXP: Integer
632
1102
 
1103
+ # <!-- rdoc-file=numeric.c -->
633
1104
  # The largest possible exponent value in a double-precision floating point.
634
1105
  #
635
1106
  # Usually defaults to 1024.
636
1107
  #
637
1108
  Float::MAX_EXP: Integer
638
1109
 
1110
+ # <!-- rdoc-file=numeric.c -->
639
1111
  # The smallest positive normalized number in a double-precision floating point.
640
1112
  #
641
1113
  # Usually defaults to 2.2250738585072014e-308.
@@ -646,6 +1118,7 @@ Float::MAX_EXP: Integer
646
1118
  #
647
1119
  Float::MIN: Float
648
1120
 
1121
+ # <!-- rdoc-file=numeric.c -->
649
1122
  # The smallest negative exponent in a double-precision floating point where 10
650
1123
  # raised to this power minus 1.
651
1124
  #
@@ -653,16 +1126,19 @@ Float::MIN: Float
653
1126
  #
654
1127
  Float::MIN_10_EXP: Integer
655
1128
 
1129
+ # <!-- rdoc-file=numeric.c -->
656
1130
  # The smallest possible exponent value in a double-precision floating point.
657
1131
  #
658
1132
  # Usually defaults to -1021.
659
1133
  #
660
1134
  Float::MIN_EXP: Integer
661
1135
 
1136
+ # <!-- rdoc-file=numeric.c -->
662
1137
  # An expression representing a value which is "not a number".
663
1138
  #
664
1139
  Float::NAN: Float
665
1140
 
1141
+ # <!-- rdoc-file=numeric.c -->
666
1142
  # The base of the floating point, or number of unique digits used to represent
667
1143
  # the number.
668
1144
  #