Skip to content
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

Merged
merged 1 commit into from
Apr 11, 2017

Conversation

traktofon
Copy link
Contributor

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.

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.
@martinholters
Copy link
Member

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?

@stevengj
Copy link
Member

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.

@ararslan ararslan added complex Complex numbers maths Mathematical functions labels Apr 11, 2017
@@ -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)))
Copy link
Member

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}?

Copy link
Contributor

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.

Copy link
Member

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.

Copy link
Member

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.

@stevengj stevengj merged commit b7836db into JuliaLang:master Apr 11, 2017
@traktofon traktofon deleted the complex-bswap branch April 13, 2017 04:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
complex Complex numbers maths Mathematical functions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants