Math class and class StrictMath Detailed source

Math class and class StrictMath Detailed source

Class definition

public final class Math {

}
public final class StrictMath {

}

The modified final class can not be inherited, that it can not have your own subclass

Math class will provide some trigonometric functions

Compared

   public static double sin(double a) {
        return StrictMath.sin(a); // default impl. delegates to StrictMath
    }
    public static native double sin(double a);

The Java platform has users and native C code API interoperability, referred to as a Java Native Interface (Java Native Interface).

Use native Keyword Description This method is a native function, that is, this method is to use C / C ++ language, and compiled as DLL, to call the java.
Achieve these bodies function in the DLL, JDK source code is not included, you should not see. For the different platforms they are also different. This is the underlying mechanism of java, java is actually calling different native methods on different platforms to achieve access to the operating system.

 

Two most essential difference -strictfp

   public static strictfp double toRadians(double angdeg) {
        // Do not delegate to Math.toRadians(angdeg) because
        // this method has the strictfp modifier.
        return angdeg / 180.0 * PI;
    }
    public static double toRadians(double angdeg) {
        return angdeg / 180.0 * PI;
    }

strictfp explanation:

 

About floor and floorMod method

    public static double ceil(double a) {
        return StrictMath.ceil(a); // default impl. delegates to StrictMath
    }

    public static double floor(double a) {
        return StrictMath.floor(a); // default impl. delegates to StrictMath
    }
    public static double ceil(double a) {
        return floorOrCeil(a, -0.0, 1.0, 1.0);
    }

    public static double floor(double a) {
        return floorOrCeil(a, -1.0, 0.0, -1.0);
    }

    private static double floorOrCeil(double a,
                                      double negativeBoundary,
                                      double positiveBoundary,
                                      double sign) {
        int exponent = Math.getExponent(a);

        if (exponent < 0) {
            /*
             * Absolute value of argument is less than 1.
             * floorOrceil(-0.0) => -0.0
             * floorOrceil(+0.0) => +0.0
             */
            return ((a == 0.0) ? a :
                    ( (a < 0.0) ?  negativeBoundary : positiveBoundary) );
        } else if (exponent >= 52) {
            /*
             * Infinity, NaN, or a value so large it must be integral.
             */
            return a;
        }
        // Else the argument is either an integral value already XOR it
        // has to be rounded to one.
        assert exponent >= 0 && exponent <= 51;

        long doppel = Double.doubleToRawLongBits(a);
        long mask   = DoubleConsts.SIGNIF_BIT_MASK >> exponent;

        if ( (mask & doppel) == 0L )
            return a; // integral value
        else {
            double result = Double.longBitsToDouble(doppel & (~mask));
            if (sign*a > 0.0)
                result = result + sign;
            return result;
        }
    }

 x = Math.getExponent (y) corresponding to Math.pow = Y (2, X), X ^ 2
Double exponent The Math.getExponent = (. 1); // unbiased exponent
If the argument is infinite or NaN, then the result is Float .MAX_EXPONENT + 1.
If the parameter is zero or less than normal, then the result is Float.MIN_EXPONENT-1.

 

About 0 and 0L

java memory system of
Example:
Long fw = 10000000000L;
in fact only a memory type variable fw long, it is present in, where the value is not stored in the Stack (Stack), which points to the heap (stack) another piece of real storage memory, plus the value of L purpose is to allow the heap create a long type of memory required for to put value.
So both before and after said = is in fact two different memory, but there is an invisible pointer to connect the two.

Without consequences:

  1. Without L default is int, int into long to be safe, it will automatically turn, can compile
  2. Float without F is the default type double, double turn float possible loss of precision, because it would not automatically switch, the compiler pass
  3. If the range is not exceeded int plus L, it will direct the error

 

    public static int floorMod(int x, int y) {
        int r = x - floorDiv(x, y) * y;
        return r;
    }

    public static long floorMod(long x, long y) {
        return x - floorDiv(x, y) * y;
    }

    public static int floorDiv(int x, int y) {
        int r = x / y;
        // if the signs are different and modulo not zero, round down
        if ((x ^ y) < 0 && (r * y != x)) {
            r--;
        }
        return r;
    }

  java seek mode is used Math.floorMod (dividend, divisor) Method

 When the same number of dividend and divisor, and remainder modulo results obtained are equal

       Process remainder quotient is obtained and added to the positive or negative number with an absolute value of the quotient

       Process modulo quotient is obtained except quotient value +1 sign with an absolute value and added, the aliquot.

       Remainder process:

              I sign depends on the number of dividend, the dividend is positive, compared with positive and negative dividend is negative

       Modulo process:

              Die sign depends on the divisor, the divisor is positive, compared with a positive, the divisor is responsible for the negative

 

 

    public static int abs(int a) {
        return (a < 0) ? -a : a;
    }
    public static float abs(float a) {
        return (a <= 0.0F) ? 0.0F - a : a;
    }
    public static double abs(double a) {
        return (a <= 0.0D) ? 0.0D - a : a;
    }
    public static int max(int a, int b) {
        return (a >= b) ? a : b;
    }
    public static float max(float a, float b) {
        if (a != a)
            return a;   // a is NaN
        if ((a == 0.0f) &&
            (b == 0.0f) &&
            (Float.floatToRawIntBits(a) == negativeZeroFloatBits)) {
            // Raw conversion ok since NaN can't map to -0.0.
            return b;
        }
        return (a >= b) ? a : b;
    }

 

 

 

 

 

 

 

Published 370 original articles · won praise 88 · views 290 000 +

Guess you like

Origin blog.csdn.net/qq_35029061/article/details/100168694