java.lang.Math.log()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.1k)|赞(0)|评价(0)|浏览(200)

本文整理了Java中java.lang.Math.log()方法的一些代码示例,展示了Math.log()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Math.log()方法的具体详情如下:
包路径:java.lang.Math
类名称:Math
方法名:log

Math.log介绍

[英]Returns the closest double approximation of the natural logarithm of the argument. The returned result is within 1 ulp (unit in the last place) of the real result.

Special cases:

  • log(+0.0) = -infinity
  • log(-0.0) = -infinity
  • log((anything < 0) = NaN
  • log(+infinity) = +infinity
  • log(-infinity) = NaN
  • log(NaN) = NaN
    [中]返回参数的自然对数的最接近的双近似值。返回的结果在实际结果的1 ulp(最后一位的单位)范围内。
    特殊情况:
    *对数(+0.0)=-无穷大
    *对数(-0.0)=-无穷大
    *对数((任何小于0的项)=NaN
    *对数(+无穷大)=+无穷大
    *对数(-无穷大)=NaN
    *对数(NaN)=NaN

代码示例

代码示例来源:origin: libgdx/libgdx

/** @return the logarithm of value with base a */
static public float log (float a, float value) {
  return (float)(Math.log(value) / Math.log(a));
}

代码示例来源:origin: libgdx/libgdx

/** @return the logarithm of value with base a */
static public float log (float a, float value) {
  return (float)(Math.log(value) / Math.log(a));
}

代码示例来源:origin: google/guava

/**
 * Returns the base 2 logarithm of a double value.
 *
 * <p>Special cases:
 *
 * <ul>
 *   <li>If {@code x} is NaN or less than zero, the result is NaN.
 *   <li>If {@code x} is positive infinity, the result is positive infinity.
 *   <li>If {@code x} is positive or negative zero, the result is negative infinity.
 * </ul>
 *
 * <p>The computed result is within 1 ulp of the exact result.
 *
 * <p>If the result of this method will be immediately rounded to an {@code int}, {@link
 * #log2(double, RoundingMode)} is faster.
 */
public static double log2(double x) {
 return log(x) / LN_2; // surprisingly within 1 ulp according to tests
}

代码示例来源:origin: hankcs/HanLP

/**
 * Log Gamma Function
 *
 * @param Z
 * @return
 */
public static double LogGamma(double Z)
{
  double S = 1.0 + 76.18009173 / Z - 86.50532033 / (Z + 1.0) + 24.01409822 / (Z + 2.0) - 1.231739516 / (Z + 3.0) + 0.00120858003 / (Z + 4.0) - 0.00000536382 / (Z + 5.0);
  double LG = (Z - 0.5) * Math.log(Z + 4.5) - (Z + 4.5) + Math.log(S * 2.50662827465);
  return LG;
}

代码示例来源:origin: hankcs/HanLP

protected void toLog()
{
  if (start_probability == null || transition_probability == null || emission_probability == null) return;
  for (int i = 0; i < start_probability.length; i++)
  {
    start_probability[i] = (float) Math.log(start_probability[i]);
    for (int j = 0; j < start_probability.length; j++)
      transition_probability[i][j] = (float) Math.log(transition_probability[i][j]);
    for (int j = 0; j < emission_probability[0].length; j++)
      emission_probability[i][j] = (float) Math.log(emission_probability[i][j]);
  }
}

代码示例来源:origin: hankcs/HanLP

/**
   * 初始化
   * @param outcomeLabels
   */
  public void setLabels(String[] outcomeLabels)
  {
    this.numOutcomes = outcomeLabels.length;
    r = Math.log(1.0 / numOutcomes);
  }
}

代码示例来源:origin: stackoverflow.com

// raw estimation how many output digits we will need.
 // This is just enough in cases like BASE-1, and up to
 // 30 digits (for base 2) too much for something like (1,0,0).
 int len = (int) (Math.log(BASE) / Math.log(radix) * digits.length)+1;
 int[] rDigits = new int[len];
 int rIndex = len-1;
 int[] current = digits;
 int quotLen = digits.length;

代码示例来源:origin: stackoverflow.com

public static String humanReadableByteCount(long bytes, boolean si) {
  int unit = si ? 1000 : 1024;
  if (bytes < unit) return bytes + " B";
  int exp = (int) (Math.log(bytes) / Math.log(unit));
  String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");
  return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
}

代码示例来源:origin: google/guava

/**
 * Computes m (total bits of Bloom filter) which is expected to achieve, for the specified
 * expected insertions, the required false positive probability.
 *
 * <p>See http://en.wikipedia.org/wiki/Bloom_filter#Probability_of_false_positives for the
 * formula.
 *
 * @param n expected insertions (must be positive)
 * @param p false positive rate (must be 0 < p < 1)
 */
@VisibleForTesting
static long optimalNumOfBits(long n, double p) {
 if (p == 0) {
  p = Double.MIN_VALUE;
 }
 return (long) (-n * Math.log(p) / (Math.log(2) * Math.log(2)));
}

代码示例来源:origin: apache/kafka

public ClusterConnectionStates(long reconnectBackoffMs, long reconnectBackoffMaxMs) {
  this.reconnectBackoffInitMs = reconnectBackoffMs;
  this.reconnectBackoffMaxMs = reconnectBackoffMaxMs;
  this.reconnectBackoffMaxExp = Math.log(this.reconnectBackoffMaxMs / (double) Math.max(reconnectBackoffMs, 1)) / Math.log(RECONNECT_BACKOFF_EXP_BASE);
  this.nodeState = new HashMap<>();
}

代码示例来源:origin: google/guava

/**
 * Computes the optimal k (number of hashes per element inserted in Bloom filter), given the
 * expected insertions and total number of bits in the Bloom filter.
 *
 * <p>See http://en.wikipedia.org/wiki/File:Bloom_filter_fp_probability.svg for the formula.
 *
 * @param n expected insertions (must be positive)
 * @param m total number of bits in Bloom filter (must be positive)
 */
@VisibleForTesting
static int optimalNumOfHashFunctions(long n, long m) {
 // (m / n) * log(2), but avoid truncation due to division!
 return Math.max(1, (int) Math.round((double) m / n * Math.log(2)));
}

代码示例来源:origin: hankcs/HanLP

private float computeEntropy(Map<Character, int[]> storage)
{
  float sum = 0;
  for (Map.Entry<Character, int[]> entry : storage.entrySet())
  {
    float p = entry.getValue()[0] / (float) frequency;
    sum -= p * Math.log(p);
  }
  return sum;
}

代码示例来源:origin: libgdx/libgdx

public BinarySearch (int min, int max, int fuzziness, boolean pot, boolean mod4) {
  if (pot) {
    this.min = (int)(Math.log(MathUtils.nextPowerOfTwo(min)) / Math.log(2));
    this.max = (int)(Math.log(MathUtils.nextPowerOfTwo(max)) / Math.log(2));
  } else if (mod4) {
    this.min = min % 4 == 0 ? min : min + 4 - (min % 4);
    this.max = max % 4 == 0 ? max : max + 4 - (max % 4);
  } else {
    this.min = min;
    this.max = max;
  }
  this.fuzziness = pot ? 0 : fuzziness;
  this.pot = pot;
  this.mod4 = mod4;
}

代码示例来源:origin: libgdx/libgdx

public BinarySearch (int min, int max, int fuzziness, boolean pot, boolean mod4) {
  if (pot) {
    this.min = (int)(Math.log(MathUtils.nextPowerOfTwo(min)) / Math.log(2));
    this.max = (int)(Math.log(MathUtils.nextPowerOfTwo(max)) / Math.log(2));
  } else if (mod4) {
    this.min = min % 4 == 0 ? min : min + 4 - (min % 4);
    this.max = max % 4 == 0 ? max : max + 4 - (max % 4);
  } else {
    this.min = min;
    this.max = max;
  }
  this.fuzziness = pot ? 0 : fuzziness;
  this.pot = pot;
  this.mod4 = mod4;
}

代码示例来源:origin: hankcs/HanLP

public double computeMutualInformation(String first, String second)
{
  return Math.log(Math.max(Predefine.MIN_PROBABILITY, getPairFrequency(first, second) / (totalPair / 2)) / Math.max(Predefine.MIN_PROBABILITY, (getTermFrequency(first) / totalTerm * getTermFrequency(second) / totalTerm)));
}

代码示例来源:origin: hankcs/HanLP

public double computeMutualInformation(PairFrequency pair)
{
  return Math.log(Math.max(Predefine.MIN_PROBABILITY, pair.getValue() / totalPair) / Math.max(Predefine.MIN_PROBABILITY, (CoreDictionary.getTermFrequency(pair.first) / (double) CoreDictionary.totalFrequency * CoreDictionary.getTermFrequency(pair.second) / (double) CoreDictionary.totalFrequency)));
}

代码示例来源:origin: prestodb/presto

@Test
public void testLog2()
{
  for (double doubleValue : DOUBLE_VALUES) {
    assertFunction("log2(" + doubleValue + ")", DOUBLE, Math.log(doubleValue) / Math.log(2));
  }
  assertFunction("log2(NULL)", DOUBLE, null);
}

代码示例来源:origin: prestodb/presto

@Description("natural logarithm")
@ScalarFunction
@SqlType(StandardTypes.DOUBLE)
public static double ln(@SqlType(StandardTypes.DOUBLE) double num)
{
  return Math.log(num);
}

代码示例来源:origin: google/guava

@GwtIncompatible
public void testResistsHashFloodingOnContains() {
 CallsCounter smallCounter = new CallsCounter();
 List<CountsHashCodeAndEquals> haveSameHashesSmall = createAdversarialInput(10, smallCounter);
 ImmutableSet<?> smallSet = ConstructionPathway.COPY_OF_LIST.create(haveSameHashesSmall);
 long worstCaseOpsSmall = worstCaseQueryOperations(smallSet, smallCounter);
 CallsCounter largeCounter = new CallsCounter();
 List<CountsHashCodeAndEquals> haveSameHashesLarge = createAdversarialInput(15, largeCounter);
 ImmutableSet<?> largeSet = ConstructionPathway.COPY_OF_LIST.create(haveSameHashesLarge);
 long worstCaseOpsLarge = worstCaseQueryOperations(largeSet, largeCounter);
 double ratio = (double) worstCaseOpsLarge / worstCaseOpsSmall;
 int smallSize = haveSameHashesSmall.size();
 int largeSize = haveSameHashesLarge.size();
 assertThat(ratio)
   .named(
     "ratio of equals/hashCode/compareTo operations to worst-case query an ImmutableSet "
       + "of size %s versus size %s",
     haveSameHashesLarge.size(), haveSameHashesSmall.size())
   .isAtMost(2 * Math.log(largeSize) / Math.log(smallSize));
 // We allow up to 2x wobble in the constant factors.
}

代码示例来源:origin: prestodb/presto

@InputFunction
public static void input(@AggregationState LongAndDoubleState state, @SqlType(StandardTypes.DOUBLE) double value)
{
  state.setLong(state.getLong() + 1);
  state.setDouble(state.getDouble() + Math.log(value));
}

相关文章