📄 math.java
字号:
package org.jutil.math;/** * This is a general utility class for mathematical stuff. * * @path $Source: /cvsroot/org-jutil/jutil.org/src/org/jutil/math/Math.java,v $ * @version $Revision: 1.6 $ * @date $Date: 2002/07/02 14:27:31 $ * @state $State: Exp $ * @author Marko van Dooren * @author Tom Schrijvers * @release $Name: $ */public abstract class Math { /* The revision of this class */ public final static String CVS_REVISION ="$Revision: 1.6 $"; /** * Return the sign of x * * @param x * The number of which the sign is requested. */ /*@ @ public behavior @ @ post (x>=0) ==> \result == 1; @ post (x<0) ==> \result == -1; @*/ public static double sign(double x) { if (x>=0) { return 1; } return -1; } /** * <p>Compute a^b when b is positive. * This metod is significantly faster than Math.pow, but introduces * slightly different result than Math.pow (Although I've only seen * differences in the last digit up to now, and also Math.pow doesn't * always return the exact same result as Matlab).</p> * * <p>To give you an idea, it is more than <b>10</b> times faster using * Sun jdk 1.4.0 on a celeron 366MHz and a P4 1.5GHz processor.<p> * * @param base * The number of which the exponent'th power is requested * @param exponent * The exponent */ /*@ @ public behavior @ @ pre exponent > 0; @ @ post (* \result == java.lang.Math.pow(base,exponent) *); @*/ public static final long lpower(long base, long exponent){ long result = base; long pos = exponent; while (pos > 1){ pos = pos >> 1; result = result * result * (((exponent & pos) > ((exponent ^ pos) & pos)) ? base : 1); } return result; } /** * <p>Compute a^b when b is positive. * This metod is significantly faster than Math.pow, but introduces * slightly different result than Math.pow (Although I've only seen * differences in the last digit up to now, and also Math.pow doesn't * always return the exact same result as Matlab).</p> * * <p>To give you an idea, it is more than <b>10</b> times faster using * Sun jdk 1.4.0 on a celeron 366MHz and a P4 1.5GHz processor.<p> * * @param base * The number of which the exponent'th power is requested * @param exponent * The exponent */ /*@ @ public behavior @ @ pre exponent > 0; @ @ post (* \result == java.lang.Math.pow(base,exponent) *); @*/ public static final double power(double base, long exponent){ double result = base; long pos = exponent; while (pos > 1){ pos >>= 1; result *= result * (((exponent & pos) > ((exponent ^ pos) & pos)) ? base : 1); } return result; } // small benchmark, must be removed some time. public static void main(String[] args) { double result=0; double a=3.434283471834239; long c=13; long b=30; long d=3; System.out.println(java.lang.Math.pow(a,b)); System.out.println(power(a,b)); for(int i=1; i<=1000000; i++) { result = java.lang.Math.pow(c,d); result = lpower(c,d); result = java.lang.Math.pow(a,b); result = power(a,b); } int reps=100000; long startMath=System.currentTimeMillis(); for(int i=1; i<=reps;i++) { result = java.lang.Math.pow(a,b); } long stopMath=System.currentTimeMillis(); long startPower=System.currentTimeMillis(); for(int i=1; i<=reps;i++) { result = power(a,b); } long stopPower=System.currentTimeMillis(); System.out.println("java.lang.Math.pow : "+(stopMath-startMath)+"ms"); System.out.println("power : "+(stopPower-startPower)+"ms"); startMath=System.currentTimeMillis(); for(int i=1; i<=reps;i++) { result = java.lang.Math.pow(c,d); } stopMath=System.currentTimeMillis(); startPower=System.currentTimeMillis(); for(int i=1; i<=reps;i++) { result = lpower(c,d); } stopPower=System.currentTimeMillis(); System.out.println("java.lang.Math.pow : "+(stopMath-startMath)+"ms"); System.out.println("power : "+(stopPower-startPower)+"ms"); }} /*<copyright>Copyright (C) 1997-2001. This software is copyrighted by the people and entities mentioned after the "@author" tags above, on behalf of the JUTIL.ORG Project. The copyright is dated by the dates after the "@date" tags above. All rights reserved.This software is published under the terms of the JUTIL.ORG SoftwareLicense version 1.1 or later, a copy of which has been included withthis distribution in the LICENSE file, which can also be found athttp://org-jutil.sourceforge.net/LICENSE. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the JUTIL.ORG Software License for more details. For more information,please see http://org-jutil.sourceforge.net/</copyright>/*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -