📄 mathtools.java
字号:
/** MathTools -- A collection of useful math utility routines.** Copyright (C) 1999-2002 by Joseph A. Huwaldt <jhuwaldt@knology.net>.* All rights reserved.* * This library is free software; you can redistribute it and/or* modify it under the terms of the GNU Library General Public* License as published by the Free Software Foundation; either* version 2 of the License, or (at your option) any later version.* * This library 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* Library General Public License for more details.**/package jahuwaldt.tools.math;import java.util.BitSet;/*** <p> A collection of useful static routines of a general* mathematical nature. This file includes functions that* accomplish all types of wonderous mathematical stuff.* </p>** <p> Modified by: Joseph A. Huwaldt </p>** @author Joseph A. Huwaldt Date: September 29, 1997* @version May 6, 2002**/public class MathTools extends Object { /** * The natural logarithm of 10. **/ public static final double LOG10 = Math.log(10); /** * The natural logarithm of 2. **/ public static final double LOG2 = Math.log(2); /** * The natural logarithm of the maximum double value: log(MAX_VALUE). **/ public static final double MAX_LOG = Math.log(Double.MAX_VALUE); /** * The natural logarithm of the minimum double value: log(MIN_VALUE). **/ public static final double MIN_LOG = Math.log(Double.MIN_VALUE); /** * Prevent anyone from instantiating this utiltity class. **/ private MathTools() {} //----------------------------------------------------------------------------------- /** * Test to see if a given long integer is even. * * @param n Integer number to be tested. * @return True if the number is even, false if it is odd. **/ public static final boolean even( long n ) { return (n & 1) == 0; } /** * Test to see if a given long integer is odd. * * @param n Integer number to be tested. * @return True if the number is odd, false if it is even. **/ public static final boolean odd( long n ) { return (n & 1) != 0; } /** * Calculates the square (x^2) of the argument. * * @param x Argument to be squared. * @return Returns the square (x^2) of the argument. **/ public static final double sqr( double x ) { if ( x == 0. ) return 0.; else return x * x; } /** * Computes the cube root of the specified real number. * If the argument is negative, then the cube root is negative. * * @param x Argument for which the cube root is to be found. * @return The cube root of the argument is returned. **/ public static final double cubeRoot( double x ) { double value = 0; if ( x < 0. ) value = -Math.exp( Math.log(-x) / 3. ); else value = Math.exp( Math.log( x ) / 3. ); return value; } /** * Returns a number "a" raised to the power "b". A "long" version * of Math.pow(). This is much faster than using Math.pow() if * the operands are integers. * * @param a Number to be raised to the power "b". * @param b Power to raise number "a" to. * @return A long integer "a" raised to the integer power "b". * @throws ArithmeticException if "b" is negative. **/ public static final long pow( long a, long b ) throws ArithmeticException { if ( b < 0 ) throw new ArithmeticException( "Exponent must be positive." ); long r = 1; while ( b != 0 ) { if ( odd( b ) ) r *= a; b >>>= 1; a *= a; } return r; } /** * Raises 2 to the small integer power indicated (eg: 2^3 = 8). * This is MUCH faster than calling Math.pow(2, x). * * @param x Amount to raise 2 to the power of. * @return Returns 2 raised to the power indicated. **/ public static final long pow2( long x ) { long value = 1; for ( long i = 0; i < x; ++i ) { value *= 2; } return value; } /** * Raises 10 to the small integer power indicated (eg: 10^5 = 100000). * This is faster than calling Math.pow(10, x). * * @param x Amount to raise 10 to the power of. * @return Returns 10 raised to the power indicated. **/ public static final double pow10(int x) { double pow10 = 10.; if (x != 0) { boolean neg = false; if (x < 0) { x *= -1; neg = true; } for (int i=1; i < x; ++i) pow10 *= 10.; if (neg) pow10 = 1./pow10; } else pow10 = 1.; return(pow10); } /** * Find the base 10 logarithm of the given double. * * @param x Value to find the base 10 logarithm of. * @return The base 10 logarithm of x. **/ public static final double log10( double x ) { return Math.log(x)/LOG10; } /** * Find the base 2 logarithm of the given double. * * @param x Value to find the base 2 logarithm of. * @return The base 2 logarithm of x. **/ public static final double log2( double x ) { return Math.log(x)/LOG2; } /** * Rounds a floating point number to the desired decimal place. * Example: 1346.4667 rounded to the 2nd place = 1300. * * @param value The value to be rounded. * @param place Number of decimal places to round value to. * A place of 1 rounds to 10's place, 2 to 100's * place, -2 to 1/100th place, et cetera. **/ public static final double roundToPlace(double value, int place) { // If the value is zero, just pass the number back out. if (value != 0.) { // If the place is zero, round to the one's place. if (place == 0) value = Math.floor(value+0.5); else { double pow10 = MathTools.pow10(place); // = 10 ^ place double holdvalue = value/pow10; value = Math.floor(holdvalue+0.5); // Round number to nearest integer value *= pow10; } } return value; } /** * Rounds a floating point number up to the desired decimal place. * Example: 1346.4667 rounded up to the 2nd place = 1400. * * @param value The value to be rounded up. * @param place Number of decimal places to round value to. * A place of 1 rounds to 10's place, 2 to 100's * place, -2 to 1/100th place, et cetera. **/ public static final double roundUpToPlace(double value, int place) { // If the value is zero, just pass the number back out. if (value != 0.) { // If the place is zero, round to the one's place. if (place == 0) value = Math.ceil(value); else { double pow10 = MathTools.pow10(place); // = 10 ^ place double holdvalue = value/pow10; value = Math.ceil(holdvalue); // Round number up to nearest integer value *= pow10; } } return value; } /** * Rounds a floating point number down to the desired decimal place. * Example: 1346.4667 rounded down to the 1st place = 1340. * * @param value The value to be rounded down. * @param place Number of decimal places to round value to. * A place of 1 rounds to 10's place, 2 to 100's * place, -2 to 1/100th place, et cetera. **/ public static final double roundDownToPlace(double value, int place) { // If the value is zero, just pass the number back out. if (value != 0.) { // If the place is zero, round to the one's place. if (place == 0) value = Math.floor(value); else { double pow10 = MathTools.pow10(place); // = 10 ^ place double holdvalue = value/pow10; value = Math.floor(holdvalue); // Round number down to nearest integer value *= pow10; } } return value; } /** * Calculates the greatest common divisor between two input * integers. The GCD is the largest number that can be * divided into both input numbers. Uses Euler's method. * * @param xval First integer * @param yval Second integer * @return The largest number that can be divided into both input * values. **/ public static final long greatestCommonDivisor( long xval, long yval ) { long value = 0; while ( value != xval ) { if ( xval < yval ) yval = yval - xval; else { if ( xval > yval ) xval = xval - yval; else value = xval; } } return (value); } /** * Returns the fractional part of a floating point number * (removes the integer part). * * @param x Argument for which the fractional part is to be returned. * @return The fractional part of the argument is returned.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -