rbs 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +5 -0
- data/CHANGELOG.md +23 -0
- data/README.md +6 -1
- data/core/array.rbs +2866 -1086
- data/core/basic_object.rbs +150 -30
- data/core/binding.rbs +33 -0
- data/core/builtin.rbs +4 -4
- data/core/class.rbs +43 -5
- data/core/comparable.rbs +57 -0
- data/core/complex.rbs +170 -4
- data/core/constants.rbs +51 -0
- data/core/deprecated.rbs +7 -0
- data/core/dir.rbs +305 -20
- data/core/encoding.rbs +472 -77
- data/core/enumerable.rbs +2173 -234
- data/core/enumerator.rbs +448 -182
- data/core/env.rbs +448 -1
- data/core/errno.rbs +1 -10
- data/core/errors.rbs +152 -2
- data/core/exception.rbs +201 -127
- data/core/false_class.rbs +27 -0
- data/core/fiber.rbs +118 -37
- data/core/fiber_error.rbs +8 -9
- data/core/file.rbs +1059 -139
- data/core/file_test.rbs +287 -32
- data/core/float.rbs +776 -300
- data/core/gc.rbs +185 -34
- data/core/global_variables.rbs +5 -1
- data/core/hash.rbs +1582 -649
- data/core/integer.rbs +974 -204
- data/core/io/buffer.rbs +710 -0
- data/core/io/wait.rbs +29 -8
- data/core/io.rbs +2438 -417
- data/core/kernel.rbs +2315 -316
- data/core/marshal.rbs +37 -2
- data/core/match_data.rbs +123 -6
- data/core/math.rbs +126 -6
- data/core/method.rbs +226 -102
- data/core/module.rbs +421 -45
- data/core/nil_class.rbs +64 -0
- data/core/numeric.rbs +620 -142
- data/core/object.rbs +453 -81
- data/core/object_space.rbs +92 -2
- data/core/proc.rbs +482 -285
- data/core/process.rbs +443 -34
- data/core/ractor.rbs +232 -9
- data/core/random.rbs +151 -52
- data/core/range.rbs +885 -160
- data/core/rational.rbs +122 -6
- data/core/rb_config.rbs +14 -4
- data/core/refinement.rbs +44 -0
- data/core/regexp.rbs +156 -14
- data/core/ruby_vm.rbs +42 -3
- data/core/signal.rbs +78 -39
- data/core/string.rbs +2123 -567
- data/core/string_io.rbs +204 -0
- data/core/struct.rbs +283 -28
- data/core/symbol.rbs +304 -30
- data/core/thread.rbs +1288 -688
- data/core/thread_group.rbs +66 -10
- data/core/time.rbs +643 -217
- data/core/trace_point.rbs +100 -12
- data/core/true_class.rbs +24 -0
- data/core/unbound_method.rbs +73 -7
- data/core/warning.rbs +37 -12
- data/docs/CONTRIBUTING.md +40 -34
- data/docs/stdlib.md +3 -102
- data/lib/rbs/annotate/annotations.rb +197 -0
- data/lib/rbs/annotate/formatter.rb +80 -0
- data/lib/rbs/annotate/rdoc_annotator.rb +398 -0
- data/lib/rbs/annotate/rdoc_source.rb +120 -0
- data/lib/rbs/annotate.rb +6 -0
- data/lib/rbs/cli.rb +45 -1
- data/lib/rbs/definition_builder.rb +5 -1
- data/lib/rbs/location_aux.rb +12 -0
- data/lib/rbs/prototype/rb.rb +12 -0
- data/lib/rbs/version.rb +1 -1
- data/sig/annotate/annotations.rbs +102 -0
- data/sig/annotate/formatter.rbs +24 -0
- data/sig/annotate/rdoc_annotater.rbs +82 -0
- data/sig/annotate/rdoc_source.rbs +30 -0
- data/sig/cli.rbs +2 -0
- data/sig/collection/{collections.rbs → sources.rbs} +0 -0
- data/sig/location.rbs +6 -0
- data/sig/method_types.rbs +5 -1
- data/sig/polyfill.rbs +78 -0
- data/stdlib/abbrev/0/abbrev.rbs +6 -0
- data/stdlib/abbrev/0/array.rbs +26 -0
- data/stdlib/base64/0/base64.rbs +31 -0
- data/stdlib/benchmark/0/benchmark.rbs +74 -3
- data/stdlib/bigdecimal/0/big_decimal.rbs +614 -165
- data/stdlib/bigdecimal-math/0/big_math.rbs +41 -64
- data/stdlib/cgi/0/core.rbs +59 -0
- data/stdlib/coverage/0/coverage.rbs +164 -2
- data/stdlib/csv/0/csv.rbs +2862 -398
- data/stdlib/date/0/date.rbs +483 -25
- data/stdlib/date/0/date_time.rbs +187 -12
- data/stdlib/dbm/0/dbm.rbs +152 -17
- data/stdlib/digest/0/digest.rbs +146 -0
- data/stdlib/erb/0/erb.rbs +65 -245
- data/stdlib/fiber/0/fiber.rbs +73 -91
- data/stdlib/fileutils/0/fileutils.rbs +301 -1
- data/stdlib/find/0/find.rbs +9 -0
- data/stdlib/forwardable/0/forwardable.rbs +65 -1
- data/stdlib/io-console/0/io-console.rbs +227 -15
- data/stdlib/ipaddr/0/ipaddr.rbs +161 -0
- data/stdlib/json/0/json.rbs +1146 -144
- data/stdlib/logger/0/formatter.rbs +24 -0
- data/stdlib/logger/0/log_device.rbs +64 -0
- data/stdlib/logger/0/logger.rbs +165 -13
- data/stdlib/logger/0/period.rbs +10 -0
- data/stdlib/logger/0/severity.rbs +26 -0
- data/stdlib/monitor/0/monitor.rbs +163 -0
- data/stdlib/mutex_m/0/mutex_m.rbs +35 -6
- data/stdlib/net-http/0/net-http.rbs +1492 -683
- data/stdlib/nkf/0/nkf.rbs +372 -0
- data/stdlib/objspace/0/objspace.rbs +149 -90
- data/stdlib/openssl/0/openssl.rbs +8108 -71
- data/stdlib/optparse/0/optparse.rbs +487 -19
- data/stdlib/pathname/0/pathname.rbs +425 -124
- data/stdlib/prettyprint/0/prettyprint.rbs +120 -99
- data/stdlib/prime/0/integer-extension.rbs +20 -2
- data/stdlib/prime/0/prime.rbs +88 -21
- data/stdlib/pstore/0/pstore.rbs +102 -0
- data/stdlib/pty/0/pty.rbs +64 -14
- data/stdlib/resolv/0/resolv.rbs +420 -31
- data/stdlib/rubygems/0/basic_specification.rbs +4 -1
- data/stdlib/rubygems/0/config_file.rbs +33 -1
- data/stdlib/rubygems/0/dependency_installer.rbs +4 -3
- data/stdlib/rubygems/0/installer.rbs +13 -1
- data/stdlib/rubygems/0/path_support.rbs +4 -1
- data/stdlib/rubygems/0/platform.rbs +5 -1
- data/stdlib/rubygems/0/request_set.rbs +44 -2
- data/stdlib/rubygems/0/requirement.rbs +65 -2
- data/stdlib/rubygems/0/rubygems.rbs +407 -0
- data/stdlib/rubygems/0/source_list.rbs +13 -0
- data/stdlib/rubygems/0/specification.rbs +21 -1
- data/stdlib/rubygems/0/stream_ui.rbs +3 -1
- data/stdlib/rubygems/0/uninstaller.rbs +8 -1
- data/stdlib/rubygems/0/version.rbs +60 -157
- data/stdlib/securerandom/0/securerandom.rbs +44 -0
- data/stdlib/set/0/set.rbs +420 -106
- data/stdlib/shellwords/0/shellwords.rbs +55 -77
- data/stdlib/singleton/0/singleton.rbs +20 -0
- data/stdlib/socket/0/addrinfo.rbs +210 -9
- data/stdlib/socket/0/basic_socket.rbs +103 -11
- data/stdlib/socket/0/ip_socket.rbs +31 -9
- data/stdlib/socket/0/socket.rbs +586 -38
- data/stdlib/socket/0/tcp_server.rbs +22 -2
- data/stdlib/socket/0/tcp_socket.rbs +12 -1
- data/stdlib/socket/0/udp_socket.rbs +25 -2
- data/stdlib/socket/0/unix_server.rbs +22 -2
- data/stdlib/socket/0/unix_socket.rbs +45 -5
- data/stdlib/strscan/0/string_scanner.rbs +210 -9
- data/stdlib/tempfile/0/tempfile.rbs +58 -10
- data/stdlib/time/0/time.rbs +208 -116
- data/stdlib/timeout/0/timeout.rbs +10 -0
- data/stdlib/tmpdir/0/tmpdir.rbs +13 -4
- data/stdlib/tsort/0/cyclic.rbs +1 -0
- data/stdlib/tsort/0/interfaces.rbs +1 -0
- data/stdlib/tsort/0/tsort.rbs +42 -0
- data/stdlib/uri/0/common.rbs +57 -8
- data/stdlib/uri/0/file.rbs +55 -109
- data/stdlib/uri/0/ftp.rbs +6 -3
- data/stdlib/uri/0/generic.rbs +556 -327
- data/stdlib/uri/0/http.rbs +26 -115
- data/stdlib/uri/0/https.rbs +8 -102
- data/stdlib/uri/0/ldap.rbs +143 -137
- data/stdlib/uri/0/ldaps.rbs +8 -102
- data/stdlib/uri/0/mailto.rbs +3 -0
- data/stdlib/uri/0/rfc2396_parser.rbs +66 -26
- data/stdlib/uri/0/ws.rbs +6 -3
- data/stdlib/uri/0/wss.rbs +5 -3
- data/stdlib/yaml/0/dbm.rbs +151 -87
- data/stdlib/yaml/0/store.rbs +6 -0
- data/stdlib/zlib/0/zlib.rbs +90 -31
- metadata +17 -5
- data/lib/rbs/location.rb +0 -221
data/core/float.rbs
CHANGED
@@ -1,119 +1,390 @@
|
|
1
|
-
#
|
2
|
-
#
|
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
|
-
# *
|
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
|
-
# *
|
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
|
-
#
|
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
|
-
#
|
19
|
-
#
|
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
|
-
#
|
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
|
-
#
|
190
|
+
# <!--
|
191
|
+
# rdoc-file=numeric.c
|
192
|
+
# - self ** other -> numeric
|
193
|
+
# -->
|
194
|
+
# Raises `self` to the power of `other`:
|
32
195
|
#
|
33
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
62
|
-
# is returned.
|
272
|
+
# `Float::NAN < Float::NAN` returns an implementation-dependent value.
|
63
273
|
#
|
64
274
|
def <: (Numeric) -> bool
|
65
275
|
|
66
|
-
#
|
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
|
-
#
|
69
|
-
# is returned.
|
288
|
+
# `Float::NAN <= Float::NAN` returns an implementation-dependent value.
|
70
289
|
#
|
71
290
|
def <=: (Numeric) -> bool
|
72
291
|
|
73
|
-
#
|
74
|
-
#
|
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
|
-
#
|
77
|
-
# is returned.
|
305
|
+
# Examples:
|
78
306
|
#
|
79
|
-
#
|
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
|
-
#
|
84
|
-
#
|
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
|
-
#
|
332
|
+
# `Float::NAN == Float::NAN` returns an implementation-dependent value.
|
87
333
|
#
|
88
|
-
#
|
89
|
-
# is returned.
|
334
|
+
# Related: Float#eql? (requires `other` to be a Float).
|
90
335
|
#
|
91
336
|
def ==: (untyped) -> bool
|
92
337
|
|
93
|
-
#
|
94
|
-
#
|
338
|
+
# <!-- rdoc-file=numeric.c -->
|
339
|
+
# Returns `true` if `other` has the same value as `self`, `false` otherwise:
|
95
340
|
#
|
96
|
-
#
|
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
|
-
#
|
99
|
-
#
|
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
|
-
#
|
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
|
-
#
|
106
|
-
# is returned.
|
363
|
+
# `Float::NAN > Float::NAN` returns an implementation-dependent value.
|
107
364
|
#
|
108
365
|
def >: (Numeric) -> bool
|
109
366
|
|
110
|
-
#
|
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
|
-
#
|
113
|
-
#
|
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
|
-
#
|
138
|
-
#
|
139
|
-
#
|
140
|
-
#
|
141
|
-
# `
|
142
|
-
#
|
143
|
-
#
|
144
|
-
#
|
145
|
-
#
|
146
|
-
#
|
147
|
-
#
|
148
|
-
# (
|
149
|
-
# (
|
150
|
-
#
|
151
|
-
#
|
152
|
-
#
|
153
|
-
#
|
154
|
-
#
|
155
|
-
#
|
156
|
-
#
|
157
|
-
#
|
158
|
-
#
|
159
|
-
#
|
160
|
-
#
|
161
|
-
#
|
162
|
-
#
|
163
|
-
#
|
164
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
484
|
+
# <!--
|
485
|
+
# rdoc-file=numeric.c
|
486
|
+
# - divmod(other) -> array
|
487
|
+
# -->
|
488
|
+
# Returns a 2-element array `[q, r]`, where
|
196
489
|
#
|
197
|
-
#
|
198
|
-
#
|
490
|
+
# q = (self/other).floor # Quotient
|
491
|
+
# r = self % other # Remainder
|
199
492
|
#
|
200
|
-
|
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
|
-
#
|
205
|
-
#
|
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
|
-
#
|
525
|
+
# `Float::NAN.eql?(Float::NAN)` returns an implementation-dependent value.
|
208
526
|
#
|
209
|
-
#
|
210
|
-
# value is returned.
|
527
|
+
# Related: Float#== (performs type conversions).
|
211
528
|
#
|
212
529
|
def eql?: (untyped) -> bool
|
213
530
|
|
214
|
-
#
|
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
|
-
#
|
220
|
-
#
|
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
|
-
#
|
225
|
-
#
|
226
|
-
#
|
227
|
-
#
|
228
|
-
# `
|
229
|
-
#
|
230
|
-
#
|
231
|
-
#
|
232
|
-
#
|
233
|
-
#
|
234
|
-
#
|
235
|
-
# (
|
236
|
-
# (
|
237
|
-
#
|
238
|
-
#
|
239
|
-
#
|
240
|
-
#
|
241
|
-
#
|
242
|
-
#
|
243
|
-
#
|
244
|
-
#
|
245
|
-
#
|
246
|
-
#
|
247
|
-
#
|
248
|
-
#
|
249
|
-
#
|
250
|
-
#
|
251
|
-
#
|
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
|
-
#
|
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
|
-
#
|
274
|
-
#
|
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
|
-
#
|
277
|
-
#
|
278
|
-
#
|
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
|
-
#
|
287
|
-
#
|
288
|
-
#
|
289
|
-
#
|
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
|
-
#
|
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
|
-
#
|
299
|
-
#
|
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
|
-
#
|
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
|
-
#
|
306
|
-
#
|
307
|
-
#
|
308
|
-
#
|
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
|
-
#
|
317
|
-
#
|
318
|
-
#
|
319
|
-
#
|
320
|
-
#
|
321
|
-
#
|
322
|
-
#
|
323
|
-
#
|
324
|
-
#
|
325
|
-
#
|
326
|
-
#
|
327
|
-
#
|
328
|
-
#
|
329
|
-
#
|
330
|
-
#
|
331
|
-
#
|
332
|
-
#
|
333
|
-
#
|
334
|
-
# #
|
335
|
-
#
|
336
|
-
#
|
337
|
-
#
|
338
|
-
#
|
339
|
-
#
|
340
|
-
#
|
341
|
-
#
|
342
|
-
#
|
343
|
-
#
|
344
|
-
#
|
345
|
-
#
|
346
|
-
#
|
347
|
-
#
|
348
|
-
#
|
349
|
-
# #
|
350
|
-
# #
|
351
|
-
# #
|
352
|
-
#
|
353
|
-
#
|
354
|
-
#
|
355
|
-
#
|
356
|
-
#
|
357
|
-
#
|
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
|
-
#
|
389
|
-
#
|
390
|
-
#
|
391
|
-
#
|
392
|
-
#
|
393
|
-
#
|
394
|
-
#
|
395
|
-
#
|
396
|
-
#
|
397
|
-
#
|
398
|
-
#
|
399
|
-
#
|
400
|
-
#
|
401
|
-
#
|
402
|
-
#
|
403
|
-
#
|
404
|
-
#
|
405
|
-
#
|
406
|
-
#
|
407
|
-
# #
|
408
|
-
#
|
409
|
-
#
|
410
|
-
#
|
411
|
-
#
|
412
|
-
#
|
413
|
-
#
|
414
|
-
#
|
415
|
-
#
|
416
|
-
#
|
417
|
-
#
|
418
|
-
#
|
419
|
-
#
|
420
|
-
#
|
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
|
-
#
|
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
|
-
#
|
457
|
-
#
|
458
|
-
#
|
459
|
-
#
|
460
|
-
# `
|
461
|
-
#
|
462
|
-
#
|
463
|
-
#
|
464
|
-
#
|
465
|
-
#
|
466
|
-
#
|
467
|
-
#
|
468
|
-
# (
|
469
|
-
#
|
470
|
-
#
|
471
|
-
#
|
472
|
-
#
|
473
|
-
#
|
474
|
-
#
|
475
|
-
#
|
476
|
-
#
|
477
|
-
#
|
478
|
-
#
|
479
|
-
#
|
480
|
-
#
|
481
|
-
#
|
482
|
-
#
|
483
|
-
#
|
484
|
-
#
|
485
|
-
#
|
486
|
-
#
|
487
|
-
#
|
488
|
-
#
|
489
|
-
#
|
490
|
-
#
|
491
|
-
#
|
492
|
-
#
|
493
|
-
#
|
494
|
-
# 2.5.round(half: :down)
|
495
|
-
#
|
496
|
-
#
|
497
|
-
#
|
498
|
-
#
|
499
|
-
#
|
500
|
-
#
|
501
|
-
#
|
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
|
-
#
|
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
|
520
|
-
# (-1.2).to_i
|
946
|
+
# 1.2.to_i # => 1
|
947
|
+
# (-1.2).to_i # => -1
|
521
948
|
#
|
522
|
-
# Note that the limited precision of floating
|
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
|
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
|
-
#
|
958
|
+
# <!-- rdoc-file=numeric.c -->
|
959
|
+
# Returns `self` truncated to an Integer.
|
532
960
|
#
|
533
|
-
# 1.2.to_i
|
534
|
-
# (-1.2).to_i
|
961
|
+
# 1.2.to_i # => 1
|
962
|
+
# (-1.2).to_i # => -1
|
535
963
|
#
|
536
|
-
# Note that the limited precision of floating
|
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
|
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
|
-
#
|
564
|
-
#
|
565
|
-
#
|
566
|
-
#
|
567
|
-
|
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
|
-
#
|
573
|
-
#
|
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
|
-
#
|
576
|
-
#
|
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
|
-
|
579
|
-
|
580
|
-
#
|
581
|
-
#
|
582
|
-
#
|
583
|
-
#
|
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
|
#
|