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

float(Complex{Integer}) should give a complex float #2930

Closed
ViralBShah opened this issue Apr 25, 2013 · 5 comments
Closed

float(Complex{Integer}) should give a complex float #2930

ViralBShah opened this issue Apr 25, 2013 · 5 comments
Labels
needs decision A decision on this change is needed

Comments

@ViralBShah
Copy link
Member

float(Complex{Integer}) should give a complex float.

julia> float(1+im)
ERROR: InexactError()
 in convert at complex.jl:23
 in float at float.jl:46
@stevengj
Copy link
Member

1+im cannot be converted exactly into a Real value, so InexactError seems correct to me. Note that float(Complex{Float64}) also throws an InexactError under the same circumstances, e.g. float(1.+im).

If the imaginary part is zero, so that the conversion is exact, then this already works: float(1+0im) and float(1.+0im) both give 1.0.

@vtjnash
Copy link
Member

vtjnash commented Apr 25, 2013

I think I agree with Viral that the return type should be Complex{Float64} instead of Float64. There doesn't seem to be a reason that float must return only the Real part, instead of the same value but represented as a floating point number.
e.g. float(1+im) => 1.0+1.0im

@JeffBezanson
Copy link
Member

Yes, generally convert has the conservative behavior of raising errors, and the functions such as int() and float() convert forcibly since they are always called explicitly.

@ViralBShah
Copy link
Member Author

I was looking at cases where BLAS operations are to be performed on non BlasFloat arrays. If float() worked on complex numbers as I suggested, then it would be really easy to have generic code. Otherwise, every function has to have a separate case to handle complex.

I always interpreted the float function name as something that converted the input to floating point types in a general sense, rather than something that should give Real values. Having convert to raise an error here, but having float do a forcible conversion would do the trick for me.

@JeffBezanson
Copy link
Member

Yes that would be good. The whole point of a function like float64(x) as opposed to convert(Float64, x) is that it can do whatever we want, not constrained to convert to a specific type (Float64).

andrioni added a commit to andrioni/julia that referenced this issue May 9, 2013
This will be very useful in the future, as it solves JuliaLang#2930 for
BigInts (`float(x)` uses `convert(FloatingPoint, x)`), so
float(BigInt(1)im) now works.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs decision A decision on this change is needed
Projects
None yet
Development

No branches or pull requests

4 participants