Repository navigation
feat(compiler): compute and format complex numbers - #547
Merged
Merged
Conversation
Complex numbers are objects of their two parts, but the arithmetic operators lowered to the JavaScript ones, which concatenate or give NaN for objects, and fmt formatted them as structs. The compiler now lowers +, -, *, / and unary - of complex operands, and their assignment forms, to runtime helpers, rounding complex64 results to float32 parts. Division follows the Go runtime's complex128div, so division by zero and by infinities gives the same infinities and NaNs. fmt formats a complex number as Go does, for %v and the float verbs. Signed-off-by: Randall C. O'Reilly <oreilly@ucdavis.edu>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Complex numbers are objects of their real and imaginary parts, but +, -, * and / of complex operands lowered to the JavaScript operators, which concatenate the objects as strings or give NaN, and fmt formatted a complex number as a struct, {1 2} rather than (1+2i).
The compiler now lowers the binary arithmetic operators, unary minus and the assignment forms on complex types to runtime helpers, complexAdd, complexSub, complexMul, complexDiv and complexNeg. An operand that is a constant of integer or float value lowers to a number, which the helpers take as a complex number with a zero imaginary part. A complex64 result is rounded to float32 parts. complexDiv follows the Go runtime's complex128div (Smith's algorithm, with the C99 corrections for infinities and zeros), so division by zero and by infinities gives the same results as Go, without panicking.
fmt formats a complex number as Go does for %v and the float verbs e, f and g, with the imaginary part always signed: (1+2i), (1.00+2.00i), (3.000000e+00-1.000000e+00i). It recognizes one by the basic type it is boxed with or, inside composite values, by its form.
complex_arithmetic checks the operators, the assignment forms, complex64 rounding, division by zero and infinity, and formatting against Go.