-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add bswap method for complex numbers. #21346
Conversation
It byte-swaps the real and imaginary parts individually, following what Fortran compilers do. The tests read the binary test data from an ad-hoc IOBuffer, because reinterpret doesn't work for complex numbers.
Should this be documented e.g. in the docstring? The docstring currently only speaks about byte-swapping integers, but one can easily guess what it does for floating points. For complex numbers, OTOH, it could reasonably also swap real and imaginary parts (reading the whole thing backwards), no? |
No, it wouldn't be reasonable to swap real and imaginary parts. endian-ness is a concept that applies only to real values taken individually. |
@@ -167,6 +167,9 @@ function write(s::IO, z::Complex) | |||
write(s,real(z),imag(z)) | |||
end | |||
|
|||
## byte order swaps: real and imaginary part are swapped individually | |||
bswap(z::Complex) = Complex(bswap(real(z)), bswap(imag(z))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, shouldn't this be Complex{<:Integer}
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if we defined them but floating points have endianess too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bswap
is defined for hardware floating-point values as well. I think it is appropriate to define this method for all Complex{T}
, and let it fail with a bswap
methoderror if the latter is not defined for T
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, sounds good to me.
It byte-swaps the real and imaginary parts individually, following what Fortran compilers do.
The tests read the binary test data from an ad-hoc IOBuffer, because reinterpret doesn't work for complex numbers.
Originally I had this method in my own package FortranFiles.jl, but because this defines a Base method for a Base datatype, @tkelman suggested it might go into Base.