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

No more ImaginaryUnit: const im = Complex(false,true). #5468

Merged
merged 4 commits into from
Jan 22, 2014

Conversation

StefanKarpinski
Copy link
Member

This change requires giving multiplition by Bools special behavior. Approximately, true * x = x and false * x = zero(x), but a bit complicated for the sake of promotion, the only non-trivial example of which is Bool * MathConst, which is promoted to Float64.

Idea originally due to @GunnarFarneback:

Compare against #5313.

This change requires giving multiplition by Bools special behavior.
Approximately, `true * x = x` and `false * x = zero(x)`, but a bit
complicated for the sake of promotion, the only non-trivial example
of which is `Bool * MathConst`, which is promoted to Float64.

Idea originally due to @GunnarFarneback:

    https://groups.google.com/forum/#!topic/julia-dev/VkGrqnrAdaY
    #2980
    #3728
@JeffBezanson
Copy link
Member

This change is hard to resist. It simplifies the code and adds nice behavior like isa(im,Complex) (which means fewer cases to handle).

In the issues linked above, I said I couldn't think of a justification for this. Now I might have one: a pure imaginary type behaves identically to a complex number whose real part behaves like the false in this PR. So if a pure imaginary type is acceptable (as endorsed by Kahan), this should be too. The only remaining question is whether it's ok to use Bool as the requisite "exotic" type. Often bool is not treated as numeric, so that might make it ok.

@@ -42,6 +42,12 @@ abs2(x::Bool) = x
^(x::Bool, y::Bool) = x|!y
^(x::Integer, y::Bool) = y ? x : one(x)

function *{T<:Number}(x::Bool, y::T)
S = promote_type(Bool,T)
ifelse(x, convert(S,y), convert(S,0))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, why the use of ifelse here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't require a branch --- both clauses can be evaluated, which often allows better code generation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@stevengj
Copy link
Member

I like it; I don't see any use for Bool arithmetic otherwise.

@StefanKarpinski
Copy link
Member Author

Alright. If it's good enough for @stevengj, there's not much more I could ask for. The only way we're going to find out if this is ok is by merging it.

StefanKarpinski added a commit that referenced this pull request Jan 22, 2014
No more ImaginaryUnit: `const im = Complex(false,true)`.
@StefanKarpinski StefanKarpinski merged commit 562f015 into master Jan 22, 2014
@StefanKarpinski StefanKarpinski deleted the sk/no-imaginary branch January 22, 2014 23:23
@stevengj
Copy link
Member

c(x,y) = x + im*y
code_native(c, (Float64,Float64))

is a lot longer than code_native(Complex, (Float64, Float64)). Seems like some more specialized methods for Complex{Bool} may be needed?

@JeffBezanson
Copy link
Member

Working on improving the codegen now.

@stevengj
Copy link
Member

Codegen is much improved, but it still does an unnecessary addition of x with 0.0 in the real part. Presumably this will go away once a pure-imaginary type is added.

@JeffBezanson
Copy link
Member

We were hoping to avoid a pure imaginary type. But, in addition to the codegen issue, there is also a potential bug:

julia> c(-0.0,0.0)
0.0 + 0.0im

@StefanKarpinski
Copy link
Member Author

What is c?

@JeffBezanson
Copy link
Member

Steven's function c(x,y) = x + im*y above.

@StefanKarpinski
Copy link
Member Author

Ok, the core problem is that -0.0 + false is 0.0 rather than -0.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants