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
#### `Integer#**` with a negative exponentCRuby evaluates a negative integer exponent to a `Rational` (`2 ** -1 # => (1/2)`).Spinel has no `Rational` type. Rather than silently truncating the result to `0`(the previous behavior), a negative integer exponent raises:Spinel now has a `Rational` type, but `Integer#**` keeps a static `Integer`result type: the exponent's sign is generally not known at compile time, sotyping `x ** y` as a sometimes-`Rational` would force the result poly andcascade through inference. A negative integer exponent therefore raises ratherthan silently truncating to `0`:```ruby2 ** -1 # RangeError: negative exponentThis applies to `Integer#**` / `Integer#pow` across the int, bigint, andpoly-dispatched paths, and is catchable as usual (`rescue RangeError`).`Float#**` is unaffected and stays CRuby-compatible, because a float result isrepresentable (`2.0 ** -1 # => 0.5`).representable (`2.0 ** -1 # => 0.5`). A `Rational` base is also fine(`Rational(1,2) ** -1 # => (2/1)`).#### `Rational` precision and `Complex` components`Rational` is stored as a pair of fixed `mrb_int` numerator/denominator. Thearithmetic is exact while the reduced terms fit in `mrb_int`; an operation whoseresult would overflow raises `RangeError` rather than promoting to a Bigint asCRuby does:```rubyRational(10**18, 1) * Rational(10**18, 1) # RangeError (CRuby: a Bigint Rational)````Complex` is stored as a pair of `mrb_float` components, so unlike CRuby it doesnot preserve `Integer`/`Rational` component types. Operators and display matchCRuby for the common cases, but `#real` / `#imaginary` / `#abs2` return `Float`,and exact division yields `Float` components instead of `Rational`:```rubyComplex(1, 2).real # => 1.0 (CRuby: 1)Complex(1, 2) / Complex(3, -1) # => (0.1+0.7i) (CRuby: ((1/10)+(7/10)*i))```Storing a `Rational` or `Complex` in a heterogeneous (poly) array is not yetsupported -- the 16-byte value does not fit the boxed-value union, so such anelement reads back as `nil`.#### `String#grapheme_clusters`lib/sp_runtime.hmodifiedView file
Loading diff…
src/analyze_infer.cmodifiedView file
Loading diff…
src/codegen_call.cmodifiedView file
Loading diff…
src/codegen_expr.cmodifiedView file
Loading diff…
src/codegen_stmt.cmodifiedView file
Loading diff…
test/complex_arithmetic.rbaddedView file
Loading diff…
test/complex_arithmetic.rb.expectedaddedView file
Loading diff…