Java examples for java.lang:Math Calculation
Compute logFactorial(N).
/*/*w w w . j a v a 2 s . c o m*/ * Copyright (c) 2014. Real Time Genomics Limited. * * Use of this source code is bound by the Real Time Genomics Limited Software Licence Agreement * for Academic Non-commercial Research Purposes only. * * If you did not receive a license accompanying this file, a copy must first be obtained by email * from support@realtimegenomics.com. On downloading, using and/or continuing to use this source * code you accept the terms of that license agreement and any amendments to those terms that may * be made from time to time by Real Time Genomics Limited. */ //package com.java2s; public class Main { private static final double C0 = Math.PI / 3.0; private static final double C1 = Math.PI * 2.0; private static final int EXPONENT_OFFSET = 52; private static final int EXPONENT_BIAS = 1023; private static final double LN2 = Math.log(2); private static final int BITS = 16; private static final int MASK = (1 << BITS) - 1; private static final double[] LOG_TABLE = new double[1 << BITS]; /** Cache low values to get errors lower. */ private static final double[] LOG_F = new double[31]; /** * Compute <code>logFactorial(N)</code>. Worst relative error at 31 = -9.1E-8 * * @param n number * @return log n! */ public static double logFactorial(final int n) { assert n >= 0; final double res; if (n < LOG_F.length) { res = LOG_F[n]; } else { // Use Gospers Formula. res = Math.log(C0 + n * C1) * 0.5 + (Math.log(n) - 1.0) * n; } assert res >= 0.0 && !Double.isNaN(res) && !Double.isInfinite(res); return res; } /** * Compute an approximation to the natural logarithm. Assumes * parameter is positive and finite. * * @param x parameter * @return <code>ln(x)</code> */ public static double log(final double x) { assert x >= 0 && !Double.isInfinite(x) && !Double.isNaN(x); if (x == 0.0) { return Double.NEGATIVE_INFINITY; } final long t = Double.doubleToRawLongBits(x); final long lg = (t >>> EXPONENT_OFFSET) - EXPONENT_BIAS; final int mantissa = (int) (t >> (EXPONENT_OFFSET - BITS)); final double mlg = LOG_TABLE[mantissa & MASK]; return mlg + lg * LN2; } }