Java Float to Double floatToDoubleLower(float f)

Here you can find the source of floatToDoubleLower(float f)

Description

Return the largest double that rounds up to this float.

License

Open Source License

Parameter

Parameter Description
f Float value

Return

Double value

Declaration

public static double floatToDoubleLower(float f) 

Method Source Code

//package com.java2s;
/*/* w w  w  . j av a 2s.c  om*/
 This file is part of ELKI:
 Environment for Developing KDD-Applications Supported by Index-Structures

 Copyright (C) 2013
 Ludwig-Maximilians-Universit?t M?nchen
 Lehr- und Forschungseinheit f?r Datenbanksysteme
 ELKI Development Team

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Return the largest double that rounds up to this float.
     * 
     * Note: Probably not always correct - subnormal values are quite tricky. So
     * some of the bounds might not be tight.
     * 
     * @param f Float value
     * @return Double value
     */
    public static double floatToDoubleLower(float f) {
        if (Float.isNaN(f)) {
            return Double.NaN;
        }
        if (Float.isInfinite(f)) {
            if (f < 0) {
                return Double.NEGATIVE_INFINITY;
            } else {
                return Double.longBitsToDouble(0x47efffffffffffffL);
            }
        }
        long bits = Double.doubleToRawLongBits((double) f);
        if ((bits & 0x8000000000000000L) == 0) { // Positive
            if (bits == 0L) {
                return +0.0d;
            }
            if (f == Float.MIN_VALUE) {
                // bits -= 0xf_ffff_ffff_ffffl;
                return Double.longBitsToDouble(0x3690000000000001L);
            }
            if (Float.MIN_NORMAL > f /* && f >= Double.MIN_NORMAL */) {
                // The most tricky case:
                // a denormalized float, but a normalized double
                final long bits2 = Double
                        .doubleToRawLongBits((double) -Math.nextUp(-f));
                bits = (bits >>> 1) + (bits2 >>> 1) + 1L; // + (0xfff_ffffL << 18);
            } else {
                bits -= 0xfffffffL; // 28 extra bits
            }
            return Double.longBitsToDouble(bits);
        } else {
            if (bits == 0x8000000000000000L) {
                return Double.longBitsToDouble(0xb690000000000000L);
            }
            if (f == -Float.MIN_VALUE) {
                // bits += 0x7_ffff_ffff_ffffl;
                return Double.longBitsToDouble(0xb6a7ffffffffffffL);
            }
            if (-Float.MIN_NORMAL < f /* && f <= -Double.MIN_NORMAL */) {
                // The most tricky case:
                // a denormalized float, but a normalized double
                final long bits2 = Double
                        .doubleToRawLongBits((double) -Math.nextUp(-f));
                bits = (bits >>> 1) + (bits2 >>> 1) - 1L;
            } else {
                bits += 0xfffffffL; // 28 extra bits
            }
            return Double.longBitsToDouble(bits);
        }
    }
}

Related

  1. floatToDouble(final float value)
  2. floatToDouble(float converThisNumberToFloat)
  3. floatToDouble(float[] array)
  4. floatToDouble(float[] values)
  5. floatToDoubleArray(float[] arr)