Skip to content

Add function bitsign (inverse of signbit) #33341

@eschnett

Description

@eschnett

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

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

    No branches or pull requests

    Issue actions