java.lang.Math类的使用及代码示例

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

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

Math介绍

[英]Class Math provides basic math constants and operations such as trigonometric functions, hyperbolic functions, exponential, logarithms, etc.
[中]数学类提供基本的数学常数和运算,如三角函数、双曲函数、指数、对数等。

代码示例

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

private static byte[] combineBuffers(Deque<byte[]> bufs, int totalLen) {
 byte[] result = new byte[totalLen];
 int remaining = totalLen;
 while (remaining > 0) {
  byte[] buf = bufs.removeFirst();
  int bytesToCopy = Math.min(remaining, buf.length);
  int resultOffset = totalLen - remaining;
  System.arraycopy(buf, 0, result, resultOffset, bytesToCopy);
  remaining -= bytesToCopy;
 }
 return result;
}

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

jQuery.fn.center = function () {
  this.css("position","absolute");
  this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) + 
                        $(window).scrollTop()) + "px");
  this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) + 
                        $(window).scrollLeft()) + "px");
  return this;
}

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

/** Returns the distance between the given line and point. Note the specified line is not a line segment. */
public static float distanceLinePoint (float startX, float startY, float endX, float endY, float pointX, float pointY) {
  float normalLength = (float)Math.sqrt((endX - startX) * (endX - startX) + (endY - startY) * (endY - startY));
  return Math.abs((pointX - startX) * (endY - startY) - (pointY - startY) * (endX - startX)) / normalLength;
}

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

value = 5.5

Math.floor(value) //  5
Math.ceil(value)  //  6
Math.round(value) //  6
Math.trunc(value) //  5
parseInt(value)   //  5
~~value           //  5
value | 0         //  5
value >> 0        //  5
value >>> 0       //  5
value - value % 1 //  5

代码示例来源: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: netty/netty

@Override
  public EventExecutor next() {
    return executors[Math.abs(idx.getAndIncrement() % executors.length)];
  }
}

代码示例来源: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: libgdx/libgdx

/** Ratio of circumradius to shortest edge as a measure of triangle quality.
 * <p>
 * Gary L. Miller, Dafna Talmor, Shang-Hua Teng, and Noel Walkington. A Delaunay Based Numerical Method for Three Dimensions:
 * Generation, Formulation, and Partition. */
static public float triangleQuality (float x1, float y1, float x2, float y2, float x3, float y3) {
  float length1 = (float)Math.sqrt(x1 * x1 + y1 * y1);
  float length2 = (float)Math.sqrt(x2 * x2 + y2 * y2);
  float length3 = (float)Math.sqrt(x3 * x3 + y3 * y3);
  return Math.min(length1, Math.min(length2, length3)) / triangleCircumradius(x1, y1, x2, y2, x3, y3);
}

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

/** Calls {@link #cone(float, float, float, float, float, int)} by estimating the number of segments needed for a smooth
 * circular base. */
public void cone (float x, float y, float z, float radius, float height) {
  cone(x, y, z, radius, height, Math.max(1, (int)(4 * (float)Math.sqrt(radius))));
}

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

var date1 = new Date("7/13/2010");
var date2 = new Date("12/15/2010");
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
alert(diffDays);

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

public float apply (float a) {
    if (a <= 0.5f) return (float)Math.pow(a * 2, power) / 2;
    return (float)Math.pow((a - 1) * 2, power) / (power % 2 == 0 ? -2 : 2) + 1;
  }
}

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

public static double round(double value, int places) {
  if (places < 0) throw new IllegalArgumentException();

  long factor = (long) Math.pow(10, places);
  value = value * factor;
  long tmp = Math.round(value);
  return (double) tmp / factor;
}

代码示例来源:origin: spring-projects/spring-framework

int calculateCapacity(CharSequence sequence, Charset charset) {
  float maxBytesPerChar = this.charsetToMaxBytesPerChar
      .computeIfAbsent(charset, cs -> cs.newEncoder().maxBytesPerChar());
  float maxBytesForSequence = sequence.length() * maxBytesPerChar;
  return (int) Math.ceil(maxBytesForSequence);
}

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

@Override
public void update (int screenWidth, int screenHeight, boolean centerCamera) {
  Vector2 scaled = scaling.apply(getWorldWidth(), getWorldHeight(), screenWidth, screenHeight);
  int viewportWidth = Math.round(scaled.x);
  int viewportHeight = Math.round(scaled.y);
  // Center.
  setScreenBounds((screenWidth - viewportWidth) / 2, (screenHeight - viewportHeight) / 2, viewportWidth, viewportHeight);
  apply(centerCamera);
}

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

@GwtIncompatible // #trueLog2, Math.ulp
public void testLog2Accuracy() {
 for (double d : POSITIVE_FINITE_DOUBLE_CANDIDATES) {
  double dmLog2 = DoubleMath.log2(d);
  double trueLog2 = trueLog2(d);
  assertTrue(Math.abs(dmLog2 - trueLog2) <= Math.ulp(trueLog2));
 }
}

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

private static int sqrtFloor(int x) {
 // There is no loss of precision in converting an int to a double, according to
 // http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.2
 return (int) Math.sqrt(x);
}

代码示例来源:origin: spring-projects/spring-framework

public String getServerId() {
  if (this.serverId == null) {
    this.serverId = String.valueOf(Math.abs(getUuid().getMostSignificantBits()) % 1000);
  }
  return this.serverId;
}

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

value = Number.MAX_SAFE_INTEGER/10 * -1 // -900719925474099.1

Math.floor(value) // -900719925474100
Math.ceil(value)  // -900719925474099
Math.round(value) // -900719925474099
Math.trunc(value) // -900719925474099
parseInt(value)   // -900719925474099
value | 0         // -858993459
~~value           // -858993459
value >> 0        // -858993459
value >>> 0       //  3435973837
value - value % 1 // -900719925474099

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

/** Ratio of circumradius to shortest edge as a measure of triangle quality.
 * <p>
 * Gary L. Miller, Dafna Talmor, Shang-Hua Teng, and Noel Walkington. A Delaunay Based Numerical Method for Three Dimensions:
 * Generation, Formulation, and Partition. */
static public float triangleQuality (float x1, float y1, float x2, float y2, float x3, float y3) {
  float length1 = (float)Math.sqrt(x1 * x1 + y1 * y1);
  float length2 = (float)Math.sqrt(x2 * x2 + y2 * y2);
  float length3 = (float)Math.sqrt(x3 * x3 + y3 * y3);
  return Math.min(length1, Math.min(length2, length3)) / triangleCircumradius(x1, y1, x2, y2, x3, y3);
}

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

/** Calls {@link #cone(float, float, float, float, float, int)} by estimating the number of segments needed for a smooth
 * circular base. */
public void cone (float x, float y, float z, float radius, float height) {
  cone(x, y, z, radius, height, Math.max(1, (int)(4 * (float)Math.sqrt(radius))));
}

相关文章