java.lang.Math's floor(),ceil() and round()的区别:
Just 贴 JDK:
引用
public static double floor(double a)
Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer.
floor返回的是不大于(小于or等于)参数的最大整数的floating-point value表示。
public static double ceil(double a)
Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.
ceil返回的是不小于(大于or等于)参数的最小整数的floating-point value表示。
public static long round(double a)
Returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. In other words, the result is equal to the value of the expression:
(long)Math.floor(a + 0.5d)
在做四舍五入运算时,我们可以使用round;但当参数为 -xxx.5 时,四舍五入后得到的结果不是我们期待的,解决办法是先对参数取绝对值,然后再用round方法
Sources:
http://blog.csdn.net/foart/archive/2009/06/24/4295645.aspx
使用BigDecimal做精确的数值计算:
Java精确计算 -
http://blog.csdn.net/xiaosu_521/article/details/2065854
关于round方法结合面试题再多说说:
Math.round(11.5) = 12
Math.round(-11.5) = -11
Math.round(-11.3) = -11
Math.round(-11.8) = -12
析:
引用
Math.round(11.5)
= (long)Math.floor(11.5 + 0.5)
= (long)Math.floor(12.0)
= (long)12.0
= 12
Math.round(-11.5)
= (long)Math.floor(-11.5 + 0.5)
= (long)Math.floor(-11.0)
= (long)-11.0
= -11
Math.round(-11.3)
= (long)Math.floor(-11.3 + 0.5)
= (long)Math.floor(-10.8)
= (long)-11.0
= -11
Math.round(-11.8)
= (long)Math.floor(-11.8 + 0.5)
= (long)Math.floor(-11.3)
= (long)-12.0
= -12
分享到:
相关推荐
Java数值计算算法编程
共有数值计算中常用的Java方法近200个.内容包括:解线性代数方程组、插值、数值积分、特殊函数、函数逼近、随机数、排序、特征值问题、数据拟合、方程求根和非线性方程组求解、函数的极值和最优化、数据的统计描述...
java数值计算算法编程一书的示例程序,学习研究之用,支持正版图书
java数值计算算法编程原代码
java数值计算java数值计算java数值计算
java 数值计算的工具包,NASA数值计算工程师写的书 Java Number Crunchyer里面的源代码,和jar包
周长发 Java 数值计算算法 编程 附带 源代码 包含复数,矩阵,线性方程,非线性方程,插值,数值积分等,非常全面
java 数值计算(英文版) chm文件
一个关于Java数值计算的开发包,内含SDKJar包,API文档,以及代码示例,涵盖小学,初中,高中,大学所有数学相关的数学逻辑运算。 此资源特别适合教育教辅行业的相关人员以及做教育系统的开发人员使用。也适合需要有...
常用的java数值计算方法.docx
数值计算中的全选主元,可以设置最小允许的主元。java编写
比较好的数值计算的实验题目,通过练习,不仅有利于提高自己的算法能力,也有利于编程的提高
java 常用的数值算法例子和Numerical Analysis for Statisticians数值分析英文版pdf
数值计算的实验课自己编写的实验程序,供参考学习
数值计算方法--插值函数(java实现) 数值计算方法--插值函数(java实现) 数值计算方法--插值函数(java实现) 数值计算方法--插值函数(java实现) 数值计算方法--插值函数(java实现) 数值计算方法--插值函数(java实现) ...
牛顿插值 数值计算 java 代码 画函数图象 牛顿插值 数值计算 java 代码 画函数图象 牛顿插值 数值计算 java 代码 画函数图象 牛顿插值 数值计算 java 代码 画函数图象
变步长的 Java,梯形公式 数值计算方法 Romber公式 辛普森公式 cotes公式
包含了数值计算课上所有实验的源代码,包括(Cholesky)普列斯基分解法解方程,Guass(高斯法程),HouseHolder.Interpolation,NormalEquations,Iteration 等各种数值计算课上的算法。