swift-numerics icon indicating copy to clipboard operation
swift-numerics copied to clipboard

Should `Complex.magnitude` use `.magnitude` instead of `abs()`?

Open NevinBR opened this issue 6 years ago • 2 comments
trafficstars

https://github.com/apple/swift-numerics/blob/f96c6c20666aefcccb63630659eae15623e47837/Sources/Complex/Complex.swift#L227

Currently, Complex.magnitude is calculated using the abs() free function instead of the .magnitude property on its real and imaginary parts. I don’t know if there is any difference at runtime (I suspect not, as the conditional checks in Swift.abs() should get optimized away), but from a code-reading standpoint it seems reasonable to prefer implementing the Complex.magnitude property in terms of the RealType.magnitude property.

NevinBR avatar Nov 18 '19 21:11 NevinBR

Yeah, these produce identical code (you can check for yourself pretty easily using godbolt.org, by the way).

stephentyrone avatar Nov 18 '19 22:11 stephentyrone

@NevinBR OK to close this?

stephentyrone avatar Nov 11 '21 13:11 stephentyrone