Tabnine Logo
Math
Code IndexAdd Tabnine to your IDE (free)

How to use
Math
in
java.lang

Best Java code snippets using java.lang.Math (Showing top 20 results out of 185,139)

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))));
}
java.langMath

Javadoc

Class Math provides basic math constants and operations such as trigonometric functions, hyperbolic functions, exponential, logarithms, etc.

Most used methods

  • min
    Returns the smaller of two long values. That is, the result is the argument closer to the value of L
  • max
    Returns the greater of two long values. That is, the result is the argument closer to the value of L
  • abs
    Returns the absolute value of a long value. If the argument is not negative, the argument is returne
  • round
    Returns the closest int to the argument, with ties rounding up. Special cases: * If the argument is
  • pow
    Returns the value of the first argument raised to the power of the second argument. Special cases: *
  • sqrt
    Returns the correctly rounded positive square root of a double value. Special cases: * If the argume
  • ceil
    Returns the smallest (closest to negative infinity) double value that is greater than or equal to th
  • floor
    Returns the largest (closest to positive infinity) double value that is less than or equal to the ar
  • random
    Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returne
  • sin
    Returns the trigonometric sine of an angle. Special cases: * If the argument is NaN or an infinit
  • cos
    Returns the trigonometric cosine of an angle. Special cases: * If the argument is NaN or an infin
  • log
    Returns the natural logarithm (base e) of a doublevalue. Special cases: * If the argument is NaN
  • cos,
  • log,
  • exp,
  • toRadians,
  • atan2,
  • log10,
  • acos,
  • tan,
  • toDegrees,
  • atan

Popular in Java

  • Reactive rest calls using spring rest template
  • runOnUiThread (Activity)
  • compareTo (BigDecimal)
  • getOriginalFilename (MultipartFile)
    Return the original filename in the client's filesystem.This may contain path information depending
  • FileOutputStream (java.io)
    An output stream that writes bytes to a file. If the output file exists, it can be replaced or appen
  • Runnable (java.lang)
    Represents a command that can be executed. Often used to run code in a different Thread.
  • Vector (java.util)
    Vector is an implementation of List, backed by an array and synchronized. All optional operations in
  • ImageIO (javax.imageio)
  • IOUtils (org.apache.commons.io)
    General IO stream manipulation utilities. This class provides static utility methods for input/outpu
  • Join (org.hibernate.mapping)
  • Top plugins for WebStorm
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now