Skip to content

Add function bitsign (inverse of signbit) #33341

@eschnett

Description

@eschnett
Contributor

The mathematical expression (-1)^n appears in many equations. It would be convenient to have an efficient standard implementation in Julia. This is, in effect, the inverse of the signbit function.

I propose the following implementation:

function bitsign(b::Bool)::Int
    1 - 2 * b   # this seems to be the most efficient way
end

function bitsign(b::I)::I where {I<:Signed}
    I(bitsign(isodd(b)))
end

Activity

added
mathsMathematical functions
speculativeWhether the change will be implemented is speculative
on Sep 24, 2019
StefanKarpinski

StefanKarpinski commented on Sep 26, 2019

@StefanKarpinski
SponsorMember

Would this be generalized to complex powers as well?

eschnett

eschnett commented on Sep 26, 2019

@eschnett
ContributorAuthor

@StefanKarpinski Are you looking for cis? Maybe a new function cispi, similar to sinpi and cospi?

StefanKarpinski

StefanKarpinski commented on Sep 26, 2019

@StefanKarpinski
SponsorMember

I'm not looking for anything, just trying to reason about the function you've proposed. If the definition is that it computes (-1)^n then the fact that it always returns ±1 for integer values is a specialization; for complex arguments it should compute the more general function. Similarly, consider the behavior of sign for a complex argument:

julia> sign(1 + 1im)
0.7071067811865475 + 0.7071067811865475im
eschnett

eschnett commented on Sep 27, 2019

@eschnett
ContributorAuthor

@StefanKarpinski I see; going for a generic definition makes sense. Of course, the efficient implementation (much more efficient than actually calculating (-1)^n) exists only for integer n.

If you allow the exponent to be any real or complex number, then the implementation would be (-1)^x = exp(im*pi*x) = cis(pi*x). That's also an expression that occurs frequently. The name bitsign doesn't work any more here.

StefanKarpinski

StefanKarpinski commented on Sep 27, 2019

@StefanKarpinski
SponsorMember

Right, that's part of why I was asking—the name seemed a bit too specific to the implementation. I think calling it cispi might be good and mentioning it in the help text of signbit. I guess the biggest worry is that it's pretty non-obvious to most people that cispi(n) is (-1)^n.

StefanKarpinski

StefanKarpinski commented on Sep 27, 2019

@StefanKarpinski
SponsorMember

It's also kind of amazing and unexpected that cispi is the inverse function of signbit.

eschnett

eschnett commented on May 6, 2020

@eschnett
ContributorAuthor

I just realize that we don't need cispi, the existing cospi will do fine. Should we instead add a method for cospi (and sinpi, for symmetry) that acts on integers or booleans?

vtjnash

vtjnash commented on Feb 16, 2022

@vtjnash
SponsorMember

No consensus that this is something desired (rejected from #35792)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    mathsMathematical functionsspeculativeWhether the change will be implemented is speculative

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @StefanKarpinski@eschnett@vtjnash@stevengj

      Issue actions

        Add function `bitsign` (inverse of `signbit`) · Issue #33341 · JuliaLang/julia