Commit 1d704986

1d70498652a8Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 8 files · +2 added · ~6 modified

Description

codegen: complete Complex arithmetic over float components

Complex had only + and * (Complex operand only), the accessors and inspect. Fill out the operator set on the existing float-component sp_Complex:

        • / against a Complex, Integer, or Float operand (the scalar is lifted to re+0i); and Integer/Float Complex.
  • ** with an Integer exponent, unary -@/+@, abs/magnitude, abs2.
  • == / != (component-wise), conjugate/conj, to_c.
  • to_s (1+2i) split from inspect ((1+2i)) -- to_s previously reused the parenthesized inspect form; puts / interpolation now use to_s.

Components stay mrb_float, so unlike CRuby a Complex does not preserve Integer/Rational component types: #real/#imaginary/#abs2 return Float and exact division yields Float (not Rational) components. Documented under the intentional incompatibilities, alongside a refresh of the now-stale "Spinel has no Rational type" note in the Integer#** section.

Adds test/complex_arithmetic.rb. Gates: test 1001/0/0, bench 57/57, optcarrot checksum 59662.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
1d70498652a8
Parent
f9c166c8e26b

docs/limitations.mdmodifiedView file

@@ -82,8 +82,11 @@
8282 #### `Integer#**` with a negative exponent
8383
8484 CRuby evaluates a negative integer exponent to a `Rational` (`2 ** -1 # => (1/2)`).
85-Spinel has no `Rational` type. Rather than silently truncating the result to `0`
86-(the previous behavior), a negative integer exponent raises:
85+Spinel now has a `Rational` type, but `Integer#**` keeps a static `Integer`
86+result type: the exponent's sign is generally not known at compile time, so
87+typing `x ** y` as a sometimes-`Rational` would force the result poly and
88+cascade through inference. A negative integer exponent therefore raises rather
89+than silently truncating to `0`:
8790
8891 ```ruby
8992 2 ** -1 # RangeError: negative exponent
@@ -92,7 +95,33 @@
9295 This applies to `Integer#**` / `Integer#pow` across the int, bigint, and
9396 poly-dispatched paths, and is catchable as usual (`rescue RangeError`).
9497 `Float#**` is unaffected and stays CRuby-compatible, because a float result is
95-representable (`2.0 ** -1 # => 0.5`).
98+representable (`2.0 ** -1 # => 0.5`). A `Rational` base is also fine
99+(`Rational(1,2) ** -1 # => (2/1)`).
100+
101+#### `Rational` precision and `Complex` components
102+
103+`Rational` is stored as a pair of fixed `mrb_int` numerator/denominator. The
104+arithmetic is exact while the reduced terms fit in `mrb_int`; an operation whose
105+result would overflow raises `RangeError` rather than promoting to a Bigint as
106+CRuby does:
107+
108+```ruby
109+Rational(10**18, 1) * Rational(10**18, 1) # RangeError (CRuby: a Bigint Rational)
110+```
111+
112+`Complex` is stored as a pair of `mrb_float` components, so unlike CRuby it does
113+not preserve `Integer`/`Rational` component types. Operators and display match
114+CRuby for the common cases, but `#real` / `#imaginary` / `#abs2` return `Float`,
115+and exact division yields `Float` components instead of `Rational`:
116+
117+```ruby
118+Complex(1, 2).real # => 1.0 (CRuby: 1)
119+Complex(1, 2) / Complex(3, -1) # => (0.1+0.7i) (CRuby: ((1/10)+(7/10)*i))
120+```
121+
122+Storing a `Rational` or `Complex` in a heterogeneous (poly) array is not yet
123+supported -- the 16-byte value does not fit the boxed-value union, so such an
124+element reads back as `nil`.
96125
97126 #### `String#grapheme_clusters`
98127