Returns the floating-point value adjacent to d in the direction of negative infinity. - Java java.lang

Java examples for java.lang:Math Value

Description

Returns the floating-point value adjacent to d in the direction of negative infinity.

Demo Code

/**/*w w  w. j  ava  2 s.c o m*/
 * Java Modular Image Synthesis Toolkit (JMIST)
 * Copyright (C) 2008-2013 Bradley W. Kimmel
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
//package com.java2s;

public class Main {
    /**
     * Returns the floating-point value adjacent to <code>d</code> in
     * the direction of negative infinity.  This method is
     * semantically equivalent to <code>nextAfter(d,
     * Double.NEGATIVE_INFINITY)</code>; however, a
     * <code>nextDown</code> implementation may run faster than its
     * equivalent <code>nextAfter</code> call.
     *
     * <p>Special Cases:
     * <ul>
     * <li> If the argument is NaN, the result is NaN.
     *
     * <li> If the argument is negative infinity, the result is
     * negative infinity.
     *
     * <li> If the argument is zero, the result is
     * <code>-Double.MIN_VALUE</code>
     *
     * </ul>
     *
     * @param d  starting floating-point value
     * @return The adjacent floating-point value closer to negative
     * infinity.
     * @author Joseph D. Darcy
     */
    public static double nextDown(double d) {
        if (Double.isNaN(d) || d == Double.NEGATIVE_INFINITY)
            return d;
        else {
            if (d == 0.0)
                return -Double.MIN_VALUE;
            else
                return Double.longBitsToDouble(Double
                        .doubleToRawLongBits(d) + ((d > 0.0d) ? -1L : +1L));
        }
    }

    /**
     * Returns the floating-point value adjacent to <code>f</code> in
     * the direction of negative infinity.  This method is
     * semantically equivalent to <code>nextAfter(f,
     * Float.NEGATIVE_INFINITY)</code>; however, a
     * <code>nextDown</code> implementation may run faster than its
     * equivalent <code>nextAfter</code> call.
     *
     * <p>Special Cases:
     * <ul>
     * <li> If the argument is NaN, the result is NaN.
     *
     * <li> If the argument is negative infinity, the result is
     * negative infinity.
     *
     * <li> If the argument is zero, the result is
     * <code>-Float.MIN_VALUE</code>
     *
     * </ul>
     *
     * @param f  starting floating-point value
     * @return The adjacent floating-point value closer to negative
     * infinity.
     * @author Joseph D. Darcy
     */
    public static float nextDown(float f) {
        if (Float.isNaN(f) || f == Float.NEGATIVE_INFINITY)
            return f;
        else {
            if (f == 0.0f)
                return -Float.MIN_VALUE;
            else
                return Float.intBitsToFloat(Float.floatToRawIntBits(f)
                        + ((f > 0.0f) ? -1 : +1));
        }
    }
}

Related Tutorials