当前位置:首页 > 开发 > 编程语言 > Java > 正文

Java的浮点精确计算

发表于: 2015-02-28   作者:小熊座   来源:转载   浏览次数:
摘要: Java在做浮点运算的时候由于float和double容易丢失精度, 所以引入了BigDecimal来做精确的运算,它是不可变的任意精度的10进制数。 /** * BigDecimal * public BigDecimal(double val) 不推荐,可能不准确 * Translates a double into a BigDecimal which is the exa
Java在做浮点运算的时候由于float和double容易丢失精度, 所以引入了BigDecimal来做精确的运算,它是不可变的任意精度的10进制数。
/**
 * BigDecimal
 * public BigDecimal(double val) 不推荐,可能不准确
 *	Translates a double into a BigDecimal which is the exact decimal representation of the double's binary floating-point value. The scale of the returned BigDecimal is the smallest value such that (10scale × val) is an integer.
 *  Notes:
	
 *	The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the value that is being passed in to the constructor is not exactly equal to 0.1, appearances notwithstanding.
 *	The String constructor, on the other hand, is perfectly predictable: writing new BigDecimal("0.1") creates a BigDecimal which is exactly equal to 0.1, as one would expect. Therefore, it is generally recommended that the String constructor be used in preference to this one.
 *	When a double must be used as a source for a BigDecimal, note that this constructor provides an exact conversion; it does not give the same result as converting the double to a String using the Double.toString(double) method and then using the BigDecimal(String) constructor. To get that result, use the static valueOf(double) method.
 *	Parameters:
 *	val - double value to be converted to BigDecimal.
 *  
 *  public BigDecimal(String val) 推荐,准确

 */
package math;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class BigDecimalDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		// float和double在计算时有时不准确,要准确计算应该使用BigDecimal类
		// System.out.println(0.01 + 0.09);//0.09999999999999999
		// System.out.println(1 - 0.32);//0.6799999999999999
		// System.out.println(1.015 * 100);//101.49999999999999
		// System.out.println(1.301 / 100);//0.013009999999999999

		// 使用BigDecimal类
		BigDecimal bd1 = new BigDecimal("0.01");
		BigDecimal bd2 = new BigDecimal("0.09");
		System.out.println("add:" + bd1.add(bd2));
		System.out.println("--------------------");

		BigDecimal bd3 = new BigDecimal("1");
		BigDecimal bd4 = new BigDecimal("0.32");
		System.out.println("subtract:" + bd3.subtract(bd4));
		System.out.println("--------------------");

		BigDecimal bd5 = new BigDecimal("1.015");
		BigDecimal bd6 = new BigDecimal("100.0");
		System.out.println("multiply:" + bd5.multiply(bd6));
		System.out.println("--------------------");

		BigDecimal bd7 = new BigDecimal("1301");
		BigDecimal bd8 = new BigDecimal("100.0");
		System.out.println("divide:" + bd7.divide(bd8));
		System.out.println("--------------------");

		BigDecimal bd9 = new BigDecimal("1301.00");
		BigDecimal bd10 = new BigDecimal("100.0");
		System.out.println("divide,round mode ceiling:"
				+ bd9.divide(bd10, 1, RoundingMode.HALF_UP));
		System.out.println("--------------------");
	}

}
 
结果:
add:0.10
--------------------
subtract:0.68
--------------------
multiply:101.5000
--------------------
divide:13.01
--------------------
divide,round mode ceiling:13.0
--------------------

Java的浮点精确计算

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
虽然几乎每种处理器和编程语言都支持浮点运算,但大多数程序员很少注意它。这容易理解 ― 我们中大
今天继续学习《C语言的科学和艺术》一书,今晚终于结束了第六章算法部分的课后习题,同时我也非常高
今天继续学习《C语言的科学和艺术》一书,今晚终于结束了第六章算法部分的课后习题,同时我也非常高
今天继续学习《C语言的科学和艺术》一书,今晚终于结束了第六章算法部分的课后习题,同时我也非常高
Javascript精确计算时的bug JS无法进行精确计算的bug 在做CRM,二代审核需求审核详情页面时。需要按
新一代原子钟的误差约为每370亿年1秒。也就是说,经过从宇宙诞生到现在的时间,这个原子钟才会产生0
人类世界的小数的表示形式 1、我们最习惯的小数表示形式是十进制,形式为:   它的值为: 2、小数
浮点数在计算机中存储方式 转自:HTTP://WWW.CNBLOGS.COM/JILLZHANG/ARCHIVE/2007/06/24/793901.HTM
同事在工作中遇到了一个与浮点数运算相关的奇怪问题,值得一记,具体代码摘要如下: 1 #include <
许多程序员在其整个开发生涯中都不曾使用定点或浮点数,可能的例外是,偶尔在计时测试或基准测试程
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号