Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code 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 General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import sun.misc.FloatConsts;
import sun.misc.DoubleConsts;

public class Main {
    /**
     * Returns the floating-point number adjacent to the first
     * argument in the direction of the second argument.  If both
     * arguments compare as equal the second argument is returned.
     *
     * <p>
     * Special cases:
     * <ul>
     * <li> If either argument is a NaN, then NaN is returned.
     *
     * <li> If both arguments are signed zeros, <code>direction</code>
     * is returned unchanged (as implied by the requirement of
     * returning the second argument if the arguments compare as
     * equal).
     *
     * <li> If <code>start</code> is
     * &plusmn;<code>Double.MIN_VALUE</code> and <code>direction</code>
     * has a value such that the result should have a smaller
     * magnitude, then a zero with the same sign as <code>start</code>
     * is returned.
     *
     * <li> If <code>start</code> is infinite and
     * <code>direction</code> has a value such that the result should
     * have a smaller magnitude, <code>Double.MAX_VALUE</code> with the
     * same sign as <code>start</code> is returned.
     *
     * <li> If <code>start</code> is equal to &plusmn;
     * <code>Double.MAX_VALUE</code> and <code>direction</code> has a
     * value such that the result should have a larger magnitude, an
     * infinity with same sign as <code>start</code> is returned.
     * </ul>
     *
     * @param start     starting floating-point value
     * @param direction value indicating which of
     * <code>start</code>'s neighbors or <code>start</code> should
     * be returned
     * @return The floating-point number adjacent to <code>start</code> in the
     * direction of <code>direction</code>.
     * @author Joseph D. Darcy
     */
    public static double nextAfter(double start, double direction) {
        /*
         * The cases:
         *
         * nextAfter(+infinity, 0)  == MAX_VALUE
         * nextAfter(+infinity, +infinity)  == +infinity
         * nextAfter(-infinity, 0)  == -MAX_VALUE
         * nextAfter(-infinity, -infinity)  == -infinity
         *
         * are naturally handled without any additional testing
         */

        // First check for NaN values
        if (isNaN(start) || isNaN(direction)) {
            // return a NaN derived from the input NaN(s)
            return start + direction;
        } else if (start == direction) {
            return direction;
        } else { // start > direction or start < direction
            // Add +0.0 to get rid of a -0.0 (+0.0 + -0.0 => +0.0)
            // then bitwise convert start to integer.
            long transducer = Double.doubleToRawLongBits(start + 0.0d);

            /*
             * IEEE 754 floating-point numbers are lexicographically
             * ordered if treated as signed- magnitude integers .
             * Since Java's integers are two's complement,
             * incrementing" the two's complement representation of a
             * logically negative floating-point value *decrements*
             * the signed-magnitude representation. Therefore, when
             * the integer representation of a floating-point values
             * is less than zero, the adjustment to the representation
             * is in the opposite direction than would be expected at
             * first .
             */
            if (direction > start) { // Calculate next greater value
                transducer = transducer + (transducer >= 0L ? 1L : -1L);
            } else { // Calculate next lesser value
                assert direction < start;
                if (transducer > 0L)
                    --transducer;
                else if (transducer < 0L)
                    ++transducer;
                /*
                 * transducer==0, the result is -MIN_VALUE
                 *
                 * The transition from zero (implicitly
                 * positive) to the smallest negative
                 * signed magnitude value must be done
                 * explicitly.
                 */
                else
                    transducer = DoubleConsts.SIGN_BIT_MASK | 1L;
            }

            return Double.longBitsToDouble(transducer);
        }
    }

    /**
     * Returns the floating-point number adjacent to the first
     * argument in the direction of the second argument.  If both
     * arguments compare as equal, the second argument is returned.
     *
     * <p>
     * Special cases:
     * <ul>
     * <li> If either argument is a NaN, then NaN is returned.
     *
     * <li> If both arguments are signed zeros, a <code>float</code>
     * zero with the same sign as <code>direction</code> is returned
     * (as implied by the requirement of returning the second argument
     * if the arguments compare as equal).
     *
     * <li> If <code>start</code> is
     * &plusmn;<code>Float.MIN_VALUE</code> and <code>direction</code>
     * has a value such that the result should have a smaller
     * magnitude, then a zero with the same sign as <code>start</code>
     * is returned.
     *
     * <li> If <code>start</code> is infinite and
     * <code>direction</code> has a value such that the result should
     * have a smaller magnitude, <code>Float.MAX_VALUE</code> with the
     * same sign as <code>start</code> is returned.
     *
     * <li> If <code>start</code> is equal to &plusmn;
     * <code>Float.MAX_VALUE</code> and <code>direction</code> has a
     * value such that the result should have a larger magnitude, an
     * infinity with same sign as <code>start</code> is returned.
     * </ul>
     *
     * @param start     starting floating-point value
     * @param direction value indicating which of
     * <code>start</code>'s neighbors or <code>start</code> should
     * be returned
     * @return The floating-point number adjacent to <code>start</code> in the
     * direction of <code>direction</code>.
     * @author Joseph D. Darcy
     */
    public static float nextAfter(float start, double direction) {
        /*
         * The cases:
         *
         * nextAfter(+infinity, 0)  == MAX_VALUE
         * nextAfter(+infinity, +infinity)  == +infinity
         * nextAfter(-infinity, 0)  == -MAX_VALUE
         * nextAfter(-infinity, -infinity)  == -infinity
         *
         * are naturally handled without any additional testing
         */

        // First check for NaN values
        if (isNaN(start) || isNaN(direction)) {
            // return a NaN derived from the input NaN(s)
            return start + (float) direction;
        } else if (start == direction) {
            return (float) direction;
        } else { // start > direction or start < direction
            // Add +0.0 to get rid of a -0.0 (+0.0 + -0.0 => +0.0)
            // then bitwise convert start to integer.
            int transducer = Float.floatToRawIntBits(start + 0.0f);

            /*
             * IEEE 754 floating-point numbers are lexicographically
             * ordered if treated as signed- magnitude integers .
             * Since Java's integers are two's complement,
             * incrementing" the two's complement representation of a
             * logically negative floating-point value *decrements*
             * the signed-magnitude representation. Therefore, when
             * the integer representation of a floating-point values
             * is less than zero, the adjustment to the representation
             * is in the opposite direction than would be expected at
             * first.
             */
            if (direction > start) {// Calculate next greater value
                transducer = transducer + (transducer >= 0 ? 1 : -1);
            } else { // Calculate next lesser value
                assert direction < start;
                if (transducer > 0)
                    --transducer;
                else if (transducer < 0)
                    ++transducer;
                /*
                 * transducer==0, the result is -MIN_VALUE
                 *
                 * The transition from zero (implicitly
                 * positive) to the smallest negative
                 * signed magnitude value must be done
                 * explicitly.
                 */
                else
                    transducer = FloatConsts.SIGN_BIT_MASK | 1;
            }

            return Float.intBitsToFloat(transducer);
        }
    }

    /**
     * Returns <code>true</code> if the specified number is a
     * Not-a-Number (NaN) value, <code>false</code> otherwise.
     *
     * <p>Note that this method is equivalent to the {@link
     * Double#isNaN(double) Double.isNaN} method; the functionality is
     * included in this class for convenience.
     *
     * @param   d   the value to be tested.
     * @return  <code>true</code> if the value of the argument is NaN;
     *          <code>false</code> otherwise.
     */
    public static boolean isNaN(double d) {
        return Double.isNaN(d);
    }

    /**
     * Returns <code>true</code> if the specified number is a
     * Not-a-Number (NaN) value, <code>false</code> otherwise.
     *
     * <p>Note that this method is equivalent to the {@link
     * Float#isNaN(float) Float.isNaN} method; the functionality is
     * included in this class for convenience.
     *
     * @param   f   the value to be tested.
     * @return  <code>true</code> if the argument is NaN;
     *          <code>false</code> otherwise.
     */
    public static boolean isNaN(float f) {
        return Float.isNaN(f);
    }
}