Fastest way to get sign in Java?

前端 未结 3 879
忘了有多久
忘了有多久 2021-01-01 10:39

I\'d like to get the sign of a float value as an int value of -1 or 1.

Avoiding conditionals is always a good idea in reducing computationa

3条回答
  •  难免孤独
    2021-01-01 10:59

    Any reasons why you don't simply use:

    int sign = (int) Math.signum(a); //1 cast for floating-points, 2 for Integer types
    

    Additionally most Number implementations have a signum method taking a primitive of that type and returning an int, so you can avoid casting for extra performance.

    int sign1 = Integer.signum(12); //no casting
    int sign2 = Long.signum(-24l); //no casting
    

    It will return +1 / 0 / -1 and it has been optimized to deliver a good performance.

    For reference, you can have a look at the implementation in openJDK. The relevant bits are:

    public static float signum(float f) {
        return (f == 0.0f || isNaN(f)) ? f : copySign(1.0f, f);
    }
    
    public static boolean isNaN(float f) {
        return (f != f);
    }
    
    public static float copySign(float magnitude, float sign) {
        return rawCopySign(magnitude, (isNaN(sign) ? 1.0f : sign));
    }
    
    public static float rawCopySign(float magnitude, float sign) {
        return Float.intBitsToFloat((Float.floatToRawIntBits(sign)
                & (FloatConsts.SIGN_BIT_MASK))
                | (Float.floatToRawIntBits(magnitude)
                & (FloatConsts.EXP_BIT_MASK
                | FloatConsts.SIGNIF_BIT_MASK)));
    }
    
    static class FloatConsts {
        public static final int SIGN_BIT_MASK = -2147483648;
        public static final int EXP_BIT_MASK = 2139095040;
        public static final int SIGNIF_BIT_MASK = 8388607;
    }
    

提交回复
热议问题