Java Float to floatToSortableInt(float val)

Here you can find the source of floatToSortableInt(float val)

Description

Converts a float value to a sortable signed int.

License

Apache License

Declaration

public static int floatToSortableInt(float val) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

public class Main {
    /**//from  w  w w  .  j  av  a 2 s .co m
     * Converts a <code>float</code> value to a sortable signed <code>int</code>.
     * The value is converted by getting their IEEE 754 floating-point &quot;float format&quot;
     * bit layout and then some bits are swapped, to be able to compare the result as int.
     * By this the precision is not reduced, but the value can easily used as an int.
     * The sort order (including {@link Float#NaN}) is defined by
     * {@link Float#compareTo}; {@code NaN} is greater than positive infinity.
     * @see #sortableIntToFloat
     */
    public static int floatToSortableInt(float val) {
        return sortableFloatBits(Float.floatToIntBits(val));
    }

    /** Converts IEEE 754 representation of a float to sortable order (or back to the original) */
    public static int sortableFloatBits(int bits) {
        return bits ^ (bits >> 31) & 0x7fffffff;
    }
}

Related

  1. floatToIntColor(float value)
  2. floatToPrefixCoded(float val)
  3. floatToRegisters(float f)
  4. floatToS390IntBits(float ieeeFloat)
  5. floatToSemiPrecision(float f, byte abyte0[], int i)
  6. floatToSortableInt(float value)
  7. floatToTime(float aTimeDuration)
  8. single2short(float f)
  9. single2short(float f)