zoukankan      html  css  js  c++  java
  • Math.round()

    cost = Math.round(101.783F);
    cost = 102  
    java.lang.Math
     

    Math

    The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.

    Unlike some of the numeric methods of class StrictMath, all implementations of the equivalent functions of class Math are not defined to return the bit-for-bit same results. This relaxation permits better-performing implementations where strict reproducibility is not required.

    By default many of the Math methods simply call the equivalent method in StrictMath for their implementation. Code generators are encouraged to use platform-specific native libraries or microprocessor instructions, where available, to provide higher-performance implementations of Math methods. Such higher-performance implementations still must conform to the specification for Math.

    The quality of implementation specifications concern two properties, accuracy of the returned result and monotonicity of the method. Accuracy of the floating-point Math methods is measured in terms of ulps, units in the last place. For a given floating-point format, an ulp of a specific real number value is the distance between the two floating-point values bracketing that numerical value. When discussing the accuracy of a method as a whole rather than at a specific argument, the number of ulps cited is for the worst-case error at any argument. If a method always has an error less than 0.5 ulps, the method always returns the floating-point number nearest the exact result; such a method is correctly rounded. A correctly rounded method is generally the best a floating-point approximation can be; however, it is impractical for many floating-point methods to be correctly rounded. Instead, for the Math class, a larger error bound of 1 or 2 ulps is allowed for certain methods. Informally, with a 1 ulp error bound, when the exact result is a representable number, the exact result should be returned as the computed result; otherwise, either of the two floating-point values which bracket the exact result may be returned. For exact results large in magnitude, one of the endpoints of the bracket may be infinite. Besides accuracy at individual arguments, maintaining proper relations between the method at different arguments is also important. Therefore, most methods with more than 0.5 ulp errors are required to be semi-monotonic: whenever the mathematical function is non-decreasing, so is the floating-point approximation, likewise, whenever the mathematical function is non-increasing, so is the floating-point approximation. Not all approximations that have 1 ulp accuracy will automatically meet the monotonicity requirements.

    The platform uses signed two's complement integer arithmetic with int and long primitive types. The developer should choose the primitive type to ensure that arithmetic operations consistently produce correct results, which in some cases means the operations will not overflow the range of values of the computation. The best practice is to choose the primitive type and algorithm to avoid overflow. In cases where the size is int or long and overflow errors need to be detected, the methods addExact, subtractExact, multiplyExact, and toIntExact throw an ArithmeticException when the results overflow. For other arithmetic operations such as divide, absolute value, increment, decrement, and negation overflow occurs only with a specific minimum or maximum value and should be checked against the minimum or maximum as appropriate.

    Since:
    JDK1.0
     
    int java.lang.Math.round(float a)
    • round

      public static int round(float a)
      Returns the closest int to the argument, with ties rounding to positive infinity.

      Special cases:

      • If the argument is NaN, the result is 0.
      • If the argument is negative infinity or any value less than or equal to the value of Integer.MIN_VALUE, the result is equal to the value of Integer.MIN_VALUE.
      • If the argument is positive infinity or any value greater than or equal to the value of Integer.MAX_VALUE, the result is equal to the value of Integer.MAX_VALUE.
      Parameters:
      a - a floating-point value to be rounded to an integer.
      Returns:
      the value of the argument rounded to the nearest int value.
      See Also:
      Integer.MAX_VALUE, Integer.MIN_VALUE
    如有错误,恳求读者指出,发送到wu13213786609@outlook.com。
  • 相关阅读:
    01 drf源码剖析之restful规范
    08 Flask源码剖析之flask拓展点
    07 flask源码剖析之用户请求过来流程
    06 flask源码剖析之路由加载
    05 flask源码剖析之配置加载
    04 flask源码剖析之LocalStack和Local对象实现栈的管理
    03 flask源码剖析之threading.local和高级
    02 flask源码剖析之flask快速使用
    01 flask源码剖析之werkzurg 了解wsgi
    MVC之Filter
  • 原文地址:https://www.cnblogs.com/WLCYSYS/p/13740413.html
Copyright © 2011-2022 走看看