rbs 4.0.0.dev.4 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (281) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +14 -14
  3. data/.github/workflows/bundle-update.yml +60 -0
  4. data/.github/workflows/c-check.yml +18 -11
  5. data/.github/workflows/comments.yml +5 -3
  6. data/.github/workflows/dependabot.yml +2 -2
  7. data/.github/workflows/ruby.yml +27 -34
  8. data/.github/workflows/rust.yml +95 -0
  9. data/.github/workflows/typecheck.yml +2 -2
  10. data/.github/workflows/windows.yml +2 -2
  11. data/.rubocop.yml +1 -1
  12. data/CHANGELOG.md +323 -0
  13. data/README.md +1 -1
  14. data/Rakefile +43 -33
  15. data/Steepfile +1 -0
  16. data/config.yml +426 -24
  17. data/core/array.rbs +307 -227
  18. data/core/basic_object.rbs +9 -8
  19. data/core/binding.rbs +0 -2
  20. data/core/builtin.rbs +2 -2
  21. data/core/class.rbs +6 -5
  22. data/core/comparable.rbs +55 -34
  23. data/core/complex.rbs +104 -78
  24. data/core/dir.rbs +61 -49
  25. data/core/encoding.rbs +12 -15
  26. data/core/enumerable.rbs +179 -87
  27. data/core/enumerator/arithmetic_sequence.rbs +70 -0
  28. data/core/enumerator.rbs +65 -2
  29. data/core/errno.rbs +11 -2
  30. data/core/errors.rbs +58 -29
  31. data/core/exception.rbs +13 -13
  32. data/core/fiber.rbs +74 -54
  33. data/core/file.rbs +280 -177
  34. data/core/file_test.rbs +3 -3
  35. data/core/float.rbs +257 -92
  36. data/core/gc.rbs +425 -281
  37. data/core/hash.rbs +1045 -739
  38. data/core/integer.rbs +135 -137
  39. data/core/io/buffer.rbs +53 -42
  40. data/core/io/wait.rbs +13 -35
  41. data/core/io.rbs +192 -144
  42. data/core/kernel.rbs +216 -155
  43. data/core/marshal.rbs +4 -4
  44. data/core/match_data.rbs +15 -13
  45. data/core/math.rbs +107 -66
  46. data/core/method.rbs +69 -33
  47. data/core/module.rbs +244 -106
  48. data/core/nil_class.rbs +7 -6
  49. data/core/numeric.rbs +74 -63
  50. data/core/object.rbs +9 -11
  51. data/core/object_space.rbs +30 -23
  52. data/core/pathname.rbs +1322 -0
  53. data/core/proc.rbs +95 -58
  54. data/core/process.rbs +222 -202
  55. data/core/ractor.rbs +371 -515
  56. data/core/random.rbs +21 -3
  57. data/core/range.rbs +159 -57
  58. data/core/rational.rbs +60 -89
  59. data/core/rbs/unnamed/argf.rbs +60 -53
  60. data/core/rbs/unnamed/env_class.rbs +19 -14
  61. data/core/rbs/unnamed/main_class.rbs +123 -0
  62. data/core/rbs/unnamed/random.rbs +11 -118
  63. data/core/regexp.rbs +258 -214
  64. data/core/ruby.rbs +53 -0
  65. data/core/ruby_vm.rbs +38 -34
  66. data/core/rubygems/config_file.rbs +5 -5
  67. data/core/rubygems/errors.rbs +4 -71
  68. data/core/rubygems/requirement.rbs +5 -5
  69. data/core/rubygems/rubygems.rbs +16 -82
  70. data/core/rubygems/version.rbs +2 -3
  71. data/core/set.rbs +490 -360
  72. data/core/signal.rbs +26 -16
  73. data/core/string.rbs +3234 -1285
  74. data/core/struct.rbs +27 -26
  75. data/core/symbol.rbs +41 -34
  76. data/core/thread.rbs +135 -67
  77. data/core/time.rbs +81 -50
  78. data/core/trace_point.rbs +41 -35
  79. data/core/true_class.rbs +2 -2
  80. data/core/unbound_method.rbs +24 -16
  81. data/core/warning.rbs +7 -7
  82. data/docs/aliases.md +79 -0
  83. data/docs/collection.md +3 -3
  84. data/docs/config.md +171 -0
  85. data/docs/encoding.md +56 -0
  86. data/docs/gem.md +0 -1
  87. data/docs/inline.md +576 -0
  88. data/docs/sigs.md +3 -3
  89. data/docs/syntax.md +46 -16
  90. data/docs/type_fingerprint.md +21 -0
  91. data/exe/rbs +1 -1
  92. data/ext/rbs_extension/ast_translation.c +544 -116
  93. data/ext/rbs_extension/ast_translation.h +3 -0
  94. data/ext/rbs_extension/class_constants.c +16 -2
  95. data/ext/rbs_extension/class_constants.h +8 -0
  96. data/ext/rbs_extension/extconf.rb +5 -1
  97. data/ext/rbs_extension/legacy_location.c +33 -56
  98. data/ext/rbs_extension/legacy_location.h +37 -0
  99. data/ext/rbs_extension/main.c +44 -35
  100. data/include/rbs/ast.h +448 -173
  101. data/include/rbs/defines.h +27 -0
  102. data/include/rbs/lexer.h +30 -11
  103. data/include/rbs/location.h +25 -44
  104. data/include/rbs/parser.h +6 -6
  105. data/include/rbs/string.h +0 -2
  106. data/include/rbs/util/rbs_allocator.h +34 -13
  107. data/include/rbs/util/rbs_assert.h +12 -1
  108. data/include/rbs/util/rbs_constant_pool.h +0 -3
  109. data/include/rbs/util/rbs_encoding.h +2 -0
  110. data/include/rbs/util/rbs_unescape.h +2 -1
  111. data/include/rbs.h +8 -0
  112. data/lib/rbs/ast/annotation.rb +1 -1
  113. data/lib/rbs/ast/comment.rb +1 -1
  114. data/lib/rbs/ast/declarations.rb +10 -10
  115. data/lib/rbs/ast/members.rb +14 -14
  116. data/lib/rbs/ast/ruby/annotations.rb +293 -3
  117. data/lib/rbs/ast/ruby/comment_block.rb +24 -0
  118. data/lib/rbs/ast/ruby/declarations.rb +198 -3
  119. data/lib/rbs/ast/ruby/helpers/constant_helper.rb +4 -0
  120. data/lib/rbs/ast/ruby/members.rb +532 -22
  121. data/lib/rbs/ast/type_param.rb +24 -4
  122. data/lib/rbs/buffer.rb +20 -15
  123. data/lib/rbs/cli/diff.rb +16 -15
  124. data/lib/rbs/cli/validate.rb +38 -106
  125. data/lib/rbs/cli.rb +52 -19
  126. data/lib/rbs/collection/config/lockfile_generator.rb +14 -2
  127. data/lib/rbs/collection/sources/git.rb +1 -0
  128. data/lib/rbs/definition.rb +1 -1
  129. data/lib/rbs/definition_builder/ancestor_builder.rb +62 -9
  130. data/lib/rbs/definition_builder/method_builder.rb +20 -0
  131. data/lib/rbs/definition_builder.rb +147 -25
  132. data/lib/rbs/diff.rb +7 -1
  133. data/lib/rbs/environment.rb +227 -74
  134. data/lib/rbs/environment_loader.rb +0 -6
  135. data/lib/rbs/errors.rb +27 -18
  136. data/lib/rbs/inline_parser.rb +342 -6
  137. data/lib/rbs/location_aux.rb +1 -1
  138. data/lib/rbs/locator.rb +5 -1
  139. data/lib/rbs/method_type.rb +5 -3
  140. data/lib/rbs/parser_aux.rb +20 -7
  141. data/lib/rbs/prototype/helpers.rb +57 -0
  142. data/lib/rbs/prototype/rb.rb +3 -28
  143. data/lib/rbs/prototype/rbi.rb +3 -20
  144. data/lib/rbs/prototype/runtime.rb +8 -0
  145. data/lib/rbs/resolver/constant_resolver.rb +2 -2
  146. data/lib/rbs/resolver/type_name_resolver.rb +116 -38
  147. data/lib/rbs/subtractor.rb +3 -1
  148. data/lib/rbs/test/type_check.rb +19 -2
  149. data/lib/rbs/type_name.rb +1 -1
  150. data/lib/rbs/types.rb +88 -78
  151. data/lib/rbs/unit_test/type_assertions.rb +35 -8
  152. data/lib/rbs/validator.rb +2 -2
  153. data/lib/rbs/version.rb +1 -1
  154. data/lib/rbs.rb +1 -2
  155. data/lib/rdoc/discover.rb +1 -1
  156. data/lib/rdoc_plugin/parser.rb +1 -1
  157. data/rbs.gemspec +4 -3
  158. data/rust/.gitignore +1 -0
  159. data/rust/Cargo.lock +378 -0
  160. data/rust/Cargo.toml +7 -0
  161. data/rust/ruby-rbs/Cargo.toml +22 -0
  162. data/rust/ruby-rbs/build.rs +764 -0
  163. data/rust/ruby-rbs/examples/locations.rs +60 -0
  164. data/rust/ruby-rbs/src/lib.rs +1 -0
  165. data/rust/ruby-rbs/src/node/mod.rs +742 -0
  166. data/rust/ruby-rbs/tests/sanity.rs +47 -0
  167. data/rust/ruby-rbs/vendor/rbs/config.yml +1 -0
  168. data/rust/ruby-rbs-sys/Cargo.toml +23 -0
  169. data/rust/ruby-rbs-sys/build.rs +204 -0
  170. data/rust/ruby-rbs-sys/src/lib.rs +50 -0
  171. data/rust/ruby-rbs-sys/vendor/rbs/include +1 -0
  172. data/rust/ruby-rbs-sys/vendor/rbs/src +1 -0
  173. data/rust/ruby-rbs-sys/wrapper.h +1 -0
  174. data/schema/typeParam.json +17 -1
  175. data/sig/ast/ruby/annotations.rbs +315 -4
  176. data/sig/ast/ruby/comment_block.rbs +8 -0
  177. data/sig/ast/ruby/declarations.rbs +102 -4
  178. data/sig/ast/ruby/members.rbs +108 -2
  179. data/sig/cli/diff.rbs +5 -11
  180. data/sig/cli/validate.rbs +12 -8
  181. data/sig/cli.rbs +18 -18
  182. data/sig/definition.rbs +6 -1
  183. data/sig/definition_builder.rbs +2 -0
  184. data/sig/environment.rbs +70 -12
  185. data/sig/errors.rbs +13 -14
  186. data/sig/inline_parser.rbs +39 -2
  187. data/sig/locator.rbs +0 -2
  188. data/sig/manifest.yaml +0 -1
  189. data/sig/method_builder.rbs +3 -1
  190. data/sig/parser.rbs +31 -13
  191. data/sig/prototype/helpers.rbs +2 -0
  192. data/sig/resolver/type_name_resolver.rbs +35 -7
  193. data/sig/source.rbs +3 -3
  194. data/sig/type_param.rbs +13 -8
  195. data/sig/types.rbs +6 -7
  196. data/sig/unit_test/spy.rbs +0 -8
  197. data/sig/unit_test/type_assertions.rbs +11 -0
  198. data/src/ast.c +410 -153
  199. data/src/lexer.c +1392 -1313
  200. data/src/lexer.re +3 -0
  201. data/src/lexstate.c +58 -37
  202. data/src/location.c +8 -48
  203. data/src/parser.c +977 -516
  204. data/src/string.c +0 -48
  205. data/src/util/rbs_allocator.c +89 -71
  206. data/src/util/rbs_assert.c +1 -1
  207. data/src/util/rbs_buffer.c +2 -2
  208. data/src/util/rbs_constant_pool.c +10 -14
  209. data/src/util/rbs_encoding.c +4 -8
  210. data/src/util/rbs_unescape.c +56 -20
  211. data/stdlib/bigdecimal/0/big_decimal.rbs +116 -98
  212. data/stdlib/bigdecimal-math/0/big_math.rbs +169 -8
  213. data/stdlib/cgi/0/core.rbs +9 -393
  214. data/stdlib/cgi/0/manifest.yaml +1 -0
  215. data/stdlib/cgi-escape/0/escape.rbs +171 -0
  216. data/stdlib/coverage/0/coverage.rbs +7 -4
  217. data/stdlib/date/0/date.rbs +92 -79
  218. data/stdlib/date/0/date_time.rbs +25 -24
  219. data/stdlib/delegate/0/delegator.rbs +10 -7
  220. data/stdlib/did_you_mean/0/did_you_mean.rbs +17 -16
  221. data/stdlib/digest/0/digest.rbs +110 -0
  222. data/stdlib/erb/0/erb.rbs +748 -347
  223. data/stdlib/etc/0/etc.rbs +55 -50
  224. data/stdlib/fileutils/0/fileutils.rbs +158 -139
  225. data/stdlib/forwardable/0/forwardable.rbs +13 -10
  226. data/stdlib/io-console/0/io-console.rbs +2 -2
  227. data/stdlib/json/0/json.rbs +217 -136
  228. data/stdlib/monitor/0/monitor.rbs +3 -3
  229. data/stdlib/net-http/0/net-http.rbs +162 -134
  230. data/stdlib/objspace/0/objspace.rbs +17 -34
  231. data/stdlib/open-uri/0/open-uri.rbs +48 -8
  232. data/stdlib/open3/0/open3.rbs +469 -10
  233. data/stdlib/openssl/0/openssl.rbs +475 -357
  234. data/stdlib/optparse/0/optparse.rbs +26 -17
  235. data/stdlib/pathname/0/pathname.rbs +11 -1381
  236. data/stdlib/pp/0/pp.rbs +9 -8
  237. data/stdlib/prettyprint/0/prettyprint.rbs +7 -7
  238. data/stdlib/pstore/0/pstore.rbs +35 -30
  239. data/stdlib/psych/0/psych.rbs +65 -12
  240. data/stdlib/psych/0/store.rbs +2 -4
  241. data/stdlib/pty/0/pty.rbs +9 -6
  242. data/stdlib/random-formatter/0/random-formatter.rbs +277 -0
  243. data/stdlib/rdoc/0/code_object.rbs +2 -1
  244. data/stdlib/rdoc/0/parser.rbs +1 -1
  245. data/stdlib/rdoc/0/rdoc.rbs +1 -1
  246. data/stdlib/rdoc/0/store.rbs +1 -1
  247. data/stdlib/resolv/0/resolv.rbs +25 -68
  248. data/stdlib/ripper/0/ripper.rbs +22 -19
  249. data/stdlib/securerandom/0/manifest.yaml +2 -0
  250. data/stdlib/securerandom/0/securerandom.rbs +7 -20
  251. data/stdlib/shellwords/0/shellwords.rbs +2 -2
  252. data/stdlib/singleton/0/singleton.rbs +3 -0
  253. data/stdlib/socket/0/addrinfo.rbs +7 -7
  254. data/stdlib/socket/0/basic_socket.rbs +3 -3
  255. data/stdlib/socket/0/ip_socket.rbs +10 -8
  256. data/stdlib/socket/0/socket.rbs +23 -10
  257. data/stdlib/socket/0/tcp_server.rbs +1 -1
  258. data/stdlib/socket/0/tcp_socket.rbs +11 -3
  259. data/stdlib/socket/0/udp_socket.rbs +1 -1
  260. data/stdlib/socket/0/unix_server.rbs +1 -1
  261. data/stdlib/stringio/0/stringio.rbs +1177 -85
  262. data/stdlib/strscan/0/string_scanner.rbs +27 -25
  263. data/stdlib/tempfile/0/tempfile.rbs +25 -21
  264. data/stdlib/time/0/time.rbs +8 -6
  265. data/stdlib/timeout/0/timeout.rbs +63 -7
  266. data/stdlib/tsort/0/cyclic.rbs +3 -0
  267. data/stdlib/tsort/0/tsort.rbs +7 -6
  268. data/stdlib/uri/0/common.rbs +42 -20
  269. data/stdlib/uri/0/file.rbs +3 -3
  270. data/stdlib/uri/0/generic.rbs +26 -18
  271. data/stdlib/uri/0/http.rbs +2 -2
  272. data/stdlib/uri/0/ldap.rbs +2 -2
  273. data/stdlib/uri/0/mailto.rbs +3 -3
  274. data/stdlib/uri/0/rfc2396_parser.rbs +12 -12
  275. data/stdlib/zlib/0/deflate.rbs +4 -3
  276. data/stdlib/zlib/0/gzip_reader.rbs +6 -6
  277. data/stdlib/zlib/0/gzip_writer.rbs +14 -12
  278. data/stdlib/zlib/0/inflate.rbs +1 -1
  279. data/stdlib/zlib/0/need_dict.rbs +1 -1
  280. data/stdlib/zlib/0/zstream.rbs +1 -0
  281. metadata +50 -6
data/core/file_test.rbs CHANGED
@@ -145,7 +145,7 @@ module FileTest
145
145
  # rdoc-file=file.c
146
146
  # - File.owned?(file_name) -> true or false
147
147
  # -->
148
- # Returns `true` if the named file exists and the effective used id of the
148
+ # Returns `true` if the named file exists and the effective user id of the
149
149
  # calling process is the owner of the file.
150
150
  #
151
151
  # *file_name* can be an IO object.
@@ -269,7 +269,7 @@ module FileTest
269
269
  # -->
270
270
  # If *file_name* is readable by others, returns an integer representing the file
271
271
  # permission bits of *file_name*. Returns `nil` otherwise. The meaning of the
272
- # bits is platform dependent; on Unix systems, see `stat(2)`.
272
+ # bits is platform dependent; on Unix systems, see <code>stat(2)</code>.
273
273
  #
274
274
  # *file_name* can be an IO object.
275
275
  #
@@ -285,7 +285,7 @@ module FileTest
285
285
  # -->
286
286
  # If *file_name* is writable by others, returns an integer representing the file
287
287
  # permission bits of *file_name*. Returns `nil` otherwise. The meaning of the
288
- # bits is platform dependent; on Unix systems, see `stat(2)`.
288
+ # bits is platform dependent; on Unix systems, see <code>stat(2)</code>.
289
289
  #
290
290
  # *file_name* can be an IO object.
291
291
  #
data/core/float.rbs CHANGED
@@ -1,15 +1,193 @@
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.
1
+ # <!-- rdoc-file=float.rb -->
2
+ # A Float object stores a real number using the native architecture's
3
+ # double-precision floating-point representation.
4
+ #
5
+ # ## Float Imprecisions
6
+ #
7
+ # Some real numbers can be represented precisely as Float objects:
8
+ #
9
+ # 37.5 # => 37.5
10
+ # 98.75 # => 98.75
11
+ # 12.3125 # => 12.3125
12
+ #
13
+ # Others cannot; among these are the transcendental numbers, including:
14
+ #
15
+ # * Pi, <em>π</em>: in mathematics, a number of infinite precision:
16
+ # 3.1415926535897932384626433... (to 25 places); in Ruby, it is of limited
17
+ # precision (in this case, to 16 decimal places):
18
+ #
19
+ # Math::PI # => 3.141592653589793
20
+ #
21
+ # * Euler's number, *e*: in mathematics, a number of infinite precision:
22
+ # 2.7182818284590452353602874... (to 25 places); in Ruby, it is of limited
23
+ # precision (in this case, to 15 decimal places):
24
+ #
25
+ # Math::E # => 2.718281828459045
26
+ #
27
+ # Some floating-point computations in Ruby give precise results:
28
+ #
29
+ # 1.0/2 # => 0.5
30
+ # 100.0/8 # => 12.5
31
+ #
32
+ # Others do not:
33
+ #
34
+ # * In mathematics, 2/3 as a decimal number is an infinitely-repeating
35
+ # decimal: 0.666... (forever); in Ruby, <code>2.0/3</code> is of limited
36
+ # precision (in this case, to 16 decimal places):
37
+ #
38
+ # 2.0/3 # => 0.6666666666666666
39
+ #
40
+ # * In mathematics, the square root of 2 is an irrational number of infinite
41
+ # precision: 1.4142135623730950488016887... (to 25 decimal places); in Ruby,
42
+ # it is of limited precision (in this case, to 16 decimal places):
43
+ #
44
+ # Math.sqrt(2.0) # => 1.4142135623730951
45
+ #
46
+ # * Even a simple computation can introduce imprecision:
4
47
  #
5
- # Floating point has a different arithmetic and is an inexact number. So you
6
- # should know its esoteric system. See following:
48
+ # x = 0.1 + 0.2 # => 0.30000000000000004
49
+ # y = 0.3 # => 0.3
50
+ # x == y # => false
51
+ #
52
+ # See:
7
53
  #
8
54
  # * https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
9
55
  # * https://github.com/rdp/ruby_tutorials_core/wiki/Ruby-Talk-FAQ#-why-are-rub
10
56
  # ys-floats-imprecise
11
57
  # * https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
12
58
  #
59
+ # Note that precise storage and computation of rational numbers is possible
60
+ # using Rational objects.
61
+ #
62
+ # ## Creating a Float
63
+ #
64
+ # You can create a Float object explicitly with:
65
+ #
66
+ # * A [floating-point literal](rdoc-ref:syntax/literals.rdoc@Float+Literals).
67
+ #
68
+ # You can convert certain objects to Floats with:
69
+ #
70
+ # * Method #Float.
71
+ #
72
+ # ## What's Here
73
+ #
74
+ # First, what's elsewhere. Class Float:
75
+ #
76
+ # * Inherits from [class Numeric](rdoc-ref:Numeric@What-27s+Here) and [class
77
+ # Object](rdoc-ref:Object@What-27s+Here).
78
+ # * Includes [module Comparable](rdoc-ref:Comparable@What-27s+Here).
79
+ #
80
+ # Here, class Float provides methods for:
81
+ #
82
+ # * [Querying](rdoc-ref:Float@Querying)
83
+ # * [Comparing](rdoc-ref:Float@Comparing)
84
+ # * [Converting](rdoc-ref:Float@Converting)
85
+ #
86
+ # ### Querying
87
+ #
88
+ # * #finite?: Returns whether `self` is finite.
89
+ # * #hash: Returns the integer hash code for `self`.
90
+ # * #infinite?: Returns whether `self` is infinite.
91
+ # * #nan?: Returns whether `self` is a NaN (not-a-number).
92
+ #
93
+ # ### Comparing
94
+ #
95
+ # * #<: Returns whether `self` is less than the given value.
96
+ # * #<=: Returns whether `self` is less than or equal to the given value.
97
+ # * #<=>: Returns a number indicating whether `self` is less than, equal to,
98
+ # or greater than the given value.
99
+ # * #== (aliased as #=== and #eql?): Returns whether `self` is equal to the
100
+ # given value.
101
+ # * #>: Returns whether `self` is greater than the given value.
102
+ # * #>=: Returns whether `self` is greater than or equal to the given value.
103
+ #
104
+ # ### Converting
105
+ #
106
+ # * #% (aliased as #modulo): Returns `self` modulo the given value.
107
+ # * #*: Returns the product of `self` and the given value.
108
+ # * #**: Returns the value of `self` raised to the power of the given value.
109
+ # * #+: Returns the sum of `self` and the given value.
110
+ # * #-: Returns the difference of `self` and the given value.
111
+ # * #/: Returns the quotient of `self` and the given value.
112
+ # * #ceil: Returns the smallest number greater than or equal to `self`.
113
+ # * #coerce: Returns a 2-element array containing the given value converted to
114
+ # a Float and `self`
115
+ # * #divmod: Returns a 2-element array containing the quotient and remainder
116
+ # results of dividing `self` by the given value.
117
+ # * #fdiv: Returns the Float result of dividing `self` by the given value.
118
+ # * #floor: Returns the greatest number smaller than or equal to `self`.
119
+ # * #next_float: Returns the next-larger representable Float.
120
+ # * #prev_float: Returns the next-smaller representable Float.
121
+ # * #quo: Returns the quotient from dividing `self` by the given value.
122
+ # * #round: Returns `self` rounded to the nearest value, to a given precision.
123
+ # * #to_i (aliased as #to_int): Returns `self` truncated to an Integer.
124
+ # * #to_s (aliased as #inspect): Returns a string containing the place-value
125
+ # representation of `self` in the given radix.
126
+ # * #truncate: Returns `self` truncated to a given precision.
127
+ #
128
+ # <!-- rdoc-file=float.rb -->
129
+ # A Float object stores a real number using the native architecture's
130
+ # double-precision floating-point representation.
131
+ #
132
+ # ## Float Imprecisions
133
+ #
134
+ # Some real numbers can be represented precisely as Float objects:
135
+ #
136
+ # 37.5 # => 37.5
137
+ # 98.75 # => 98.75
138
+ # 12.3125 # => 12.3125
139
+ #
140
+ # Others cannot; among these are the transcendental numbers, including:
141
+ #
142
+ # * Pi, <em>π</em>: in mathematics, a number of infinite precision:
143
+ # 3.1415926535897932384626433... (to 25 places); in Ruby, it is of limited
144
+ # precision (in this case, to 16 decimal places):
145
+ #
146
+ # Math::PI # => 3.141592653589793
147
+ #
148
+ # * Euler's number, *e*: in mathematics, a number of infinite precision:
149
+ # 2.7182818284590452353602874... (to 25 places); in Ruby, it is of limited
150
+ # precision (in this case, to 15 decimal places):
151
+ #
152
+ # Math::E # => 2.718281828459045
153
+ #
154
+ # Some floating-point computations in Ruby give precise results:
155
+ #
156
+ # 1.0/2 # => 0.5
157
+ # 100.0/8 # => 12.5
158
+ #
159
+ # Others do not:
160
+ #
161
+ # * In mathematics, 2/3 as a decimal number is an infinitely-repeating
162
+ # decimal: 0.666... (forever); in Ruby, <code>2.0/3</code> is of limited
163
+ # precision (in this case, to 16 decimal places):
164
+ #
165
+ # 2.0/3 # => 0.6666666666666666
166
+ #
167
+ # * In mathematics, the square root of 2 is an irrational number of infinite
168
+ # precision: 1.4142135623730950488016887... (to 25 decimal places); in Ruby,
169
+ # it is of limited precision (in this case, to 16 decimal places):
170
+ #
171
+ # Math.sqrt(2.0) # => 1.4142135623730951
172
+ #
173
+ # * Even a simple computation can introduce imprecision:
174
+ #
175
+ # x = 0.1 + 0.2 # => 0.30000000000000004
176
+ # y = 0.3 # => 0.3
177
+ # x == y # => false
178
+ #
179
+ # See:
180
+ #
181
+ # * https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
182
+ # * https://github.com/rdp/ruby_tutorials_core/wiki/Ruby-Talk-FAQ#-why-are-rub
183
+ # ys-floats-imprecise
184
+ # * https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
185
+ #
186
+ # Note that precise storage and computation of rational numbers is possible
187
+ # using Rational objects.
188
+ #
189
+ # ## Creating a Float
190
+ #
13
191
  # You can create a Float object explicitly with:
14
192
  #
15
193
  # * A [floating-point literal](rdoc-ref:syntax/literals.rdoc@Float+Literals).
@@ -79,7 +257,7 @@ class Float < Numeric
79
257
  # rdoc-file=numeric.c
80
258
  # - self % other -> float
81
259
  # -->
82
- # Returns `self` modulo `other` as a float.
260
+ # Returns `self` modulo `other` as a Float.
83
261
  #
84
262
  # For float `f` and real number `r`, these expressions are equivalent:
85
263
  #
@@ -111,7 +289,7 @@ class Float < Numeric
111
289
  # rdoc-file=numeric.c
112
290
  # - self * other -> numeric
113
291
  # -->
114
- # Returns a new Float which is the product of `self` and `other`:
292
+ # Returns the numeric product of `self` and `other`:
115
293
  #
116
294
  # f = 3.14
117
295
  # f * 2 # => 6.28
@@ -124,9 +302,9 @@ class Float < Numeric
124
302
 
125
303
  # <!--
126
304
  # rdoc-file=numeric.c
127
- # - self ** other -> numeric
305
+ # - self ** exponent -> numeric
128
306
  # -->
129
- # Raises `self` to the power of `other`:
307
+ # Returns `self` raised to the power `exponent`:
130
308
  #
131
309
  # f = 3.14
132
310
  # f ** 2 # => 9.8596
@@ -140,26 +318,29 @@ class Float < Numeric
140
318
 
141
319
  # <!--
142
320
  # rdoc-file=numeric.c
143
- # - self + other -> numeric
321
+ # - self + other -> float or complex
144
322
  # -->
145
- # Returns a new Float which is the sum of `self` and `other`:
323
+ # Returns the sum of `self` and `other`; the result may be inexact (see Float):
146
324
  #
147
- # f = 3.14
148
- # f + 1 # => 4.140000000000001
149
- # f + 1.0 # => 4.140000000000001
150
- # f + Rational(1, 1) # => 4.140000000000001
151
- # f + Complex(1, 0) # => (4.140000000000001+0i)
325
+ # 3.14 + 0 # => 3.14
326
+ # 3.14 + 1 # => 4.140000000000001
327
+ # -3.14 + 0 # => -3.14
328
+ # -3.14 + 1 # => -2.14
329
+ #
330
+ # 3.14 + -3.14 # => 0.0
331
+ # -3.14 + -3.14 # => -6.28
332
+ #
333
+ # 3.14 + Complex(1, 0) # => (4.140000000000001+0i)
334
+ # 3.14 + Rational(1, 1) # => 4.140000000000001
152
335
  #
153
336
  def +: (Complex) -> Complex
154
337
  | (Numeric) -> Float
155
338
 
156
- def +@: () -> Float
157
-
158
339
  # <!--
159
340
  # rdoc-file=numeric.c
160
341
  # - self - other -> numeric
161
342
  # -->
162
- # Returns a new Float which is the difference of `self` and `other`:
343
+ # Returns the difference of `self` and `other`:
163
344
  #
164
345
  # f = 3.14
165
346
  # f - 1 # => 2.14
@@ -172,9 +353,13 @@ class Float < Numeric
172
353
 
173
354
  # <!--
174
355
  # rdoc-file=numeric.rb
175
- # - -float -> float
356
+ # - -self -> float
176
357
  # -->
177
- # Returns `self`, negated.
358
+ # Returns `self`, negated:
359
+ #
360
+ # -3.14 # => -3.14
361
+ # -(-3.14) # => 3.14
362
+ # -0.0 # => -0.0
178
363
  #
179
364
  def -@: () -> Float
180
365
 
@@ -182,7 +367,7 @@ class Float < Numeric
182
367
  # rdoc-file=numeric.c
183
368
  # - self / other -> numeric
184
369
  # -->
185
- # Returns a new Float which is the result of dividing `self` by `other`:
370
+ # Returns the quotient of `self` and `other`:
186
371
  #
187
372
  # f = 3.14
188
373
  # f / 2 # => 1.57
@@ -197,14 +382,16 @@ class Float < Numeric
197
382
  # rdoc-file=numeric.c
198
383
  # - self < other -> true or false
199
384
  # -->
200
- # Returns `true` if `self` is numerically less than `other`:
385
+ # Returns whether the value of `self` is less than the value of `other`; `other`
386
+ # must be numeric, but may not be Complex:
201
387
  #
202
388
  # 2.0 < 3 # => true
203
389
  # 2.0 < 3.0 # => true
204
390
  # 2.0 < Rational(3, 1) # => true
205
391
  # 2.0 < 2.0 # => false
206
392
  #
207
- # `Float::NAN < Float::NAN` returns an implementation-dependent value.
393
+ # <code>Float::NAN < Float::NAN</code> returns an implementation-dependent
394
+ # value.
208
395
  #
209
396
  def <: (Numeric) -> bool
210
397
 
@@ -212,7 +399,8 @@ class Float < Numeric
212
399
  # rdoc-file=numeric.c
213
400
  # - self <= other -> true or false
214
401
  # -->
215
- # Returns `true` if `self` is numerically less than or equal to `other`:
402
+ # Returns whether the value of `self` is less than or equal to the value of
403
+ # `other`; `other` must be numeric, but may not be Complex:
216
404
  #
217
405
  # 2.0 <= 3 # => true
218
406
  # 2.0 <= 3.0 # => true
@@ -220,35 +408,39 @@ class Float < Numeric
220
408
  # 2.0 <= 2.0 # => true
221
409
  # 2.0 <= 1.0 # => false
222
410
  #
223
- # `Float::NAN <= Float::NAN` returns an implementation-dependent value.
411
+ # <code>Float::NAN <= Float::NAN</code> returns an implementation-dependent
412
+ # value.
224
413
  #
225
414
  def <=: (Numeric) -> bool
226
415
 
227
416
  # <!--
228
417
  # rdoc-file=numeric.c
229
- # - self <=> other -> -1, 0, +1, or nil
418
+ # - self <=> other -> -1, 0, 1, or nil
230
419
  # -->
231
- # Returns a value that depends on the numeric relation between `self` and
232
- # `other`:
420
+ # Compares `self` and `other`.
233
421
  #
234
- # * -1, if `self` is less than `other`.
235
- # * 0, if `self` is equal to `other`.
236
- # * 1, if `self` is greater than `other`.
422
+ # Returns:
423
+ #
424
+ # * <code>-1</code>, if `self` is less than `other`.
425
+ # * `0`, if `self` is equal to `other`.
426
+ # * `1`, if `self` is greater than `other`.
237
427
  # * `nil`, if the two values are incommensurate.
238
428
  #
239
429
  # Examples:
240
430
  #
431
+ # 2.0 <=> 2.1 # => -1
241
432
  # 2.0 <=> 2 # => 0
242
433
  # 2.0 <=> 2.0 # => 0
243
434
  # 2.0 <=> Rational(2, 1) # => 0
244
435
  # 2.0 <=> Complex(2, 0) # => 0
245
436
  # 2.0 <=> 1.9 # => 1
246
- # 2.0 <=> 2.1 # => -1
247
437
  # 2.0 <=> 'foo' # => nil
248
438
  #
249
- # This is the basis for the tests in the Comparable module.
439
+ # <code>Float::NAN <=> Float::NAN</code> returns an implementation-dependent
440
+ # value.
250
441
  #
251
- # `Float::NAN <=> Float::NAN` returns an implementation-dependent value.
442
+ # Class Float includes module Comparable, each of whose methods uses Float#<=>
443
+ # for comparison.
252
444
  #
253
445
  def <=>: (Numeric) -> Integer?
254
446
 
@@ -263,7 +455,8 @@ class Float < Numeric
263
455
  # 2.0 == Rational(2, 1) # => true
264
456
  # 2.0 == Complex(2, 0) # => true
265
457
  #
266
- # `Float::NAN == Float::NAN` returns an implementation-dependent value.
458
+ # <code>Float::NAN == Float::NAN</code> returns an implementation-dependent
459
+ # value.
267
460
  #
268
461
  # Related: Float#eql? (requires `other` to be a Float).
269
462
  #
@@ -277,7 +470,8 @@ class Float < Numeric
277
470
  # 2.0 == Rational(2, 1) # => true
278
471
  # 2.0 == Complex(2, 0) # => true
279
472
  #
280
- # `Float::NAN == Float::NAN` returns an implementation-dependent value.
473
+ # <code>Float::NAN == Float::NAN</code> returns an implementation-dependent
474
+ # value.
281
475
  #
282
476
  # Related: Float#eql? (requires `other` to be a Float).
283
477
  #
@@ -294,7 +488,8 @@ class Float < Numeric
294
488
  # 2.0 > Rational(1, 2) # => true
295
489
  # 2.0 > 2.0 # => false
296
490
  #
297
- # `Float::NAN > Float::NAN` returns an implementation-dependent value.
491
+ # <code>Float::NAN > Float::NAN</code> returns an implementation-dependent
492
+ # value.
298
493
  #
299
494
  def >: (Numeric) -> bool
300
495
 
@@ -310,7 +505,8 @@ class Float < Numeric
310
505
  # 2.0 >= 2.0 # => true
311
506
  # 2.0 >= 2.1 # => false
312
507
  #
313
- # `Float::NAN >= Float::NAN` returns an implementation-dependent value.
508
+ # <code>Float::NAN >= Float::NAN</code> returns an implementation-dependent
509
+ # value.
314
510
  #
315
511
  def >=: (Numeric) -> bool
316
512
 
@@ -326,12 +522,10 @@ class Float < Numeric
326
522
  #
327
523
  def abs: () -> Float
328
524
 
329
- def abs2: () -> Float
330
-
331
525
  # <!-- rdoc-file=complex.c -->
332
526
  # Returns 0 if `self` is positive, Math::PI otherwise.
333
527
  #
334
- def angle: () -> (Integer | Float)
528
+ def angle: ...
335
529
 
336
530
  # <!--
337
531
  # rdoc-file=complex.c
@@ -367,7 +561,7 @@ class Float < Numeric
367
561
  #
368
562
  # When `ndigits` is non-positive,
369
563
  # returns an Integer based on a computed granularity:
370
- # * The granularity is `10 ** ndigits.abs`.
564
+ # * The granularity is <code>10 ** ndigits.abs</code>.
371
565
  # * The returned value is the largest multiple of the granularity
372
566
  # that is less than or equal to `self`.
373
567
  # Examples with positive `self`:
@@ -419,10 +613,6 @@ class Float < Numeric
419
613
  #
420
614
  def coerce: (Numeric) -> [ Float, Float ]
421
615
 
422
- def conj: () -> Float
423
-
424
- def conjugate: () -> Float
425
-
426
616
  # <!--
427
617
  # rdoc-file=rational.c
428
618
  # - flo.denominator -> integer
@@ -433,13 +623,11 @@ class Float < Numeric
433
623
  #
434
624
  def denominator: () -> Integer
435
625
 
436
- def div: (Numeric) -> Integer
437
-
438
626
  # <!--
439
627
  # rdoc-file=numeric.c
440
628
  # - divmod(other) -> array
441
629
  # -->
442
- # Returns a 2-element array `[q, r]`, where
630
+ # Returns a 2-element array <code>[q, r]</code>, where
443
631
  #
444
632
  # q = (self/other).floor # Quotient
445
633
  # r = self % other # Remainder
@@ -462,8 +650,6 @@ class Float < Numeric
462
650
  def divmod: (Integer | Float | Rational) -> [ Integer, Float ]
463
651
  | (Numeric) -> [ Numeric, Numeric ]
464
652
 
465
- def dup: () -> self
466
-
467
653
  # <!--
468
654
  # rdoc-file=numeric.c
469
655
  # - eql?(other) -> true or false
@@ -477,7 +663,8 @@ class Float < Numeric
477
663
  # 2.0.eql?(Rational(2, 1)) # => false
478
664
  # 2.0.eql?(Complex(2, 0)) # => false
479
665
  #
480
- # `Float::NAN.eql?(Float::NAN)` returns an implementation-dependent value.
666
+ # <code>Float::NAN.eql?(Float::NAN)</code> returns an implementation-dependent
667
+ # value.
481
668
  #
482
669
  # Related: Float#== (performs type conversions).
483
670
  #
@@ -499,8 +686,8 @@ class Float < Numeric
499
686
  # rdoc-file=numeric.c
500
687
  # - finite? -> true or false
501
688
  # -->
502
- # Returns `true` if `self` is not `Infinity`, `-Infinity`, or `NaN`, `false`
503
- # otherwise:
689
+ # Returns `true` if `self` is not `Infinity`, <code>-Infinity</code>, or `NaN`,
690
+ # `false` otherwise:
504
691
  #
505
692
  # f = 2.0 # => 2.0
506
693
  # f.finite? # => true
@@ -545,7 +732,7 @@ class Float < Numeric
545
732
  #
546
733
  # When `self` is non-zero and `ndigits` is non-positive,
547
734
  # returns an integer value based on a computed granularity:
548
- # * The granularity is `10 ** ndigits.abs`.
735
+ # * The granularity is <code>10 ** ndigits.abs</code>.
549
736
  # * The returned value is the largest multiple of the granularity
550
737
  # that is less than or equal to `self`.
551
738
  # Examples with positive `self`:
@@ -586,12 +773,6 @@ class Float < Numeric
586
773
  #
587
774
  def hash: () -> Integer
588
775
 
589
- def i: () -> Complex
590
-
591
- def imag: () -> Integer
592
-
593
- def imaginary: () -> Integer
594
-
595
776
  # <!--
596
777
  # rdoc-file=numeric.c
597
778
  # - infinite? -> -1, 1, or nil
@@ -599,7 +780,7 @@ class Float < Numeric
599
780
  # Returns:
600
781
  #
601
782
  # * 1, if `self` is `Infinity`.
602
- # * -1 if `self` is `-Infinity`.
783
+ # * -1 if `self` is <code>-Infinity</code>.
603
784
  # * `nil`, otherwise.
604
785
  #
605
786
  # Examples:
@@ -636,8 +817,6 @@ class Float < Numeric
636
817
  #
637
818
  alias inspect to_s
638
819
 
639
- def integer?: () -> bool
640
-
641
820
  # <!--
642
821
  # rdoc-file=numeric.rb
643
822
  # - magnitude()
@@ -646,7 +825,7 @@ class Float < Numeric
646
825
  alias magnitude abs
647
826
 
648
827
  # <!-- rdoc-file=numeric.c -->
649
- # Returns `self` modulo `other` as a float.
828
+ # Returns `self` modulo `other` as a Float.
650
829
  #
651
830
  # For float `f` and real number `r`, these expressions are equivalent:
652
831
  #
@@ -699,7 +878,7 @@ class Float < Numeric
699
878
  # Returns the next-larger representable Float.
700
879
  #
701
880
  # These examples show the internally stored values (64-bit hexadecimal) for each
702
- # Float `f` and for the corresponding `f.next_float`:
881
+ # Float `f` and for the corresponding <code>f.next_float</code>:
703
882
  #
704
883
  # f = 0.0 # 0x0000000000000000
705
884
  # f.next_float # 0x0000000000000001
@@ -737,8 +916,6 @@ class Float < Numeric
737
916
  #
738
917
  def next_float: () -> Float
739
918
 
740
- def nonzero?: () -> self?
741
-
742
919
  # <!--
743
920
  # rdoc-file=rational.c
744
921
  # - flo.numerator -> integer
@@ -775,7 +952,7 @@ class Float < Numeric
775
952
  # Returns the next-smaller representable Float.
776
953
  #
777
954
  # These examples show the internally stored values (64-bit hexadecimal) for each
778
- # Float `f` and for the corresponding `f.pev_float`:
955
+ # Float `f` and for the corresponding <code>f.pev_float</code>:
779
956
  #
780
957
  # f = 5e-324 # 0x0000000000000001
781
958
  # f.prev_float # 0x0000000000000000
@@ -835,14 +1012,8 @@ class Float < Numeric
835
1012
  #
836
1013
  def rationalize: (?Numeric eps) -> Rational
837
1014
 
838
- def real: () -> Float
839
-
840
- def real?: () -> true
841
-
842
1015
  def rect: () -> [ Float, Numeric ]
843
1016
 
844
- alias rectangular rect
845
-
846
1017
  def remainder: (Numeric) -> Float
847
1018
 
848
1019
  # <!--
@@ -862,8 +1033,8 @@ class Float < Numeric
862
1033
  # f.round(1) # => -12345.7
863
1034
  # f.round(3) # => -12345.679
864
1035
  #
865
- # When `ndigits` is negative, returns an integer with at least `ndigits.abs`
866
- # trailing zeros:
1036
+ # When `ndigits` is negative, returns an integer with at least
1037
+ # <code>ndigits.abs</code> trailing zeros:
867
1038
  #
868
1039
  # f = 12345.6789
869
1040
  # f.round(0) # => 12346
@@ -875,19 +1046,20 @@ class Float < Numeric
875
1046
  # If keyword argument `half` is given, and `self` is equidistant from the two
876
1047
  # candidate values, the rounding is according to the given `half` value:
877
1048
  #
878
- # * `:up` or `nil`: round away from zero:
1049
+ # * <code>:up</code> or `nil`: round away from zero:
879
1050
  #
880
1051
  # 2.5.round(half: :up) # => 3
881
1052
  # 3.5.round(half: :up) # => 4
882
1053
  # (-2.5).round(half: :up) # => -3
883
1054
  #
884
- # * `:down`: round toward zero:
1055
+ # * <code>:down</code>: round toward zero:
885
1056
  #
886
1057
  # 2.5.round(half: :down) # => 2
887
1058
  # 3.5.round(half: :down) # => 3
888
1059
  # (-2.5).round(half: :down) # => -2
889
1060
  #
890
- # * `:even`: round toward the candidate whose last nonzero digit is even:
1061
+ # * <code>:even</code>: round toward the candidate whose last nonzero digit is
1062
+ # even:
891
1063
  #
892
1064
  # 2.5.round(half: :even) # => 2
893
1065
  # 3.5.round(half: :even) # => 4
@@ -900,13 +1072,6 @@ class Float < Numeric
900
1072
  def round: (?half: :up | :down | :even) -> Integer
901
1073
  | (int digits, ?half: :up | :down | :even) -> (Integer | Float)
902
1074
 
903
- def step: (?Numeric limit, ?Numeric step) { (Float) -> void } -> self
904
- | (?Numeric limit, ?Numeric step) -> Enumerator[Float, self]
905
- | (?by: Numeric, ?to: Numeric) { (Float) -> void } -> self
906
- | (?by: Numeric, ?to: Numeric) -> Enumerator[Float, self]
907
-
908
- def to_c: () -> Complex
909
-
910
1075
  # <!--
911
1076
  # rdoc-file=numeric.rb
912
1077
  # - to_f -> self
@@ -1007,8 +1172,8 @@ class Float < Numeric
1007
1172
  # f.truncate(1) # => -12345.6
1008
1173
  # f.truncate(3) # => -12345.678
1009
1174
  #
1010
- # When `ndigits` is negative, returns an integer with at least `ndigits.abs`
1011
- # trailing zeros:
1175
+ # When `ndigits` is negative, returns an integer with at least
1176
+ # <code>ndigits.abs</code> trailing zeros:
1012
1177
  #
1013
1178
  # f = 12345.6789
1014
1179
  # f.truncate(0) # => 12345
@@ -1094,8 +1259,8 @@ Float::MAX_EXP: Integer
1094
1259
  # Usually defaults to 2.2250738585072014e-308.
1095
1260
  #
1096
1261
  # If the platform supports denormalized numbers, there are numbers between zero
1097
- # and Float::MIN. 0.0.next_float returns the smallest positive floating point
1098
- # number including denormalized numbers.
1262
+ # and Float::MIN. <code>0.0.next_float</code> returns the smallest positive
1263
+ # floating point number including denormalized numbers.
1099
1264
  #
1100
1265
  Float::MIN: Float
1101
1266