Can Numeric be used for non-real types?

I was thinking of trying out a complex number type. I was planning a ComplexNumeric protocol that parallels Numeric. But looking further, since Numeric does not refine Comparable, I could base ComplexNumeric off of SignedNumeric directly. This is OK as long as no code assumes that Numeric means that a conforming type models (a subset of) real numbers.

Is not assuming a conforming type models real numbers OK? If so, should we add a RealNumber empty protocol (that all current numeric types would conform to) to differentiate real-number types and other mathematical types?

i doubt it, any algorithm that requires comparing two Numerics is also going to require Comparable and if your complex type doesn’t conform to Comparable then none of that code will assume it

Yes, Numeric was designed in a way that would accommodate complex numbers.

1 Like

How would you make ComplexNumeric support the * operator?

* on Numeric is defined as a (Self, Self) → Self function, but with ComplexNumeric, you’d need a (ComplexNumeric, ComplexNumeric) → Numeric version.


Because a complex number multiplied by another complex number results in a real number.

I think you’re thinking specifically of imaginary numbers.

1 Like

Ah, you’re correct. Sorry for the confusion.

Yes, if ComplexNumeric is essentially a typed (a + bi) value, you can absolutely make this conform to Numeric