java default parameter_JAVA菜鸟入门(7) default parameter , float/double vs BigDecimal

1  java的允许函数的默认参数吗?

java不支持类似C++那样,为函数设定默认参数,所以需要做的事情是,自己用函数重载的方式进行模拟。如下

public class FFOverload {

public String getName(String givenName,String familyName){

return givenName+"."+familyName;

}

public String getName(String givenName){

return getName(givenName,"BBB");

}

public static void main(String[] args) {

FFOverload demoDefaultPara=new FFOverload();

System.out.println(demoDefaultPara.getName("AAA"));

System.out.println(demoDefaultPara.getName("AAA", "CCC"));

}

}

输出:

AAA.BBB

AAA.CCC

2.  为什么floating point number不准确

首先来验证一下

public class FloatTest {

public static void main(String[] args) {

float a = 1.01f; //don't forget the trailing 'f' , else it will be treated as a double.

float b = 1.002f;

float c = 1.0000009f;

float d = a + b + c;

System.out.println("expected: 3.0120009, actua: "+ d);

}

}

输出:

expected: 3.0120009, actua: 3.012001

然后来看看原因:

In most programming languages, floating point numbers are represented a lot like scientific notation:

with an exponent and

a mantissa (also called the significand).

A very simple number, say 9.2, is actually this fraction:

5179139571476070 * 2 -49

Where the exponent is -49 and the mantissa is 5179139571476070.

The reason it is impossible to represent some decimal numbers this way is that both the exponent and the mantissa must be integers. In other words, all floats must be an integer multiplied by an integer power of 2.

Floating point numbers only have 32 or 64 bits of precision, so the digits are cut off at some point, and the resulting number is 0.199999999999999996 in decimal, not 0.2.

3. 如何尽可能准确地表示Floating Point Numbers?

3.1 使用BigDecimal Class

but currently there is a small unsolved issue in the code below. I did not see the difference between HALF_UP and HALF_DOWN in the code below.

import java.math.*;

public class FFBigDecimal {

public static void main(String[] args) {

BigDecimal bigDecimal1 = new BigDecimal(1.001);

BigDecimal bigDecimal2 = new BigDecimal(1.0005);

BigDecimal bigDecimal3 = new BigDecimal(1.000007);

BigDecimal bigDecimaBase = new BigDecimal(2.52150);

BigDecimal bigDecimal4 = bigDecimaBase.setScale(3, RoundingMode.HALF_DOWN);

System.out.println("Big Decimal is " + bigDecimal4.toString());

BigDecimal bigDecimal5 = bigDecimaBase .setScale(3, RoundingMode.HALF_UP);

System.out.println("Big Decimal is " + bigDecimal5.toString());

}

}

输出

Big Decimal is 2.521

Big Decimal is 2.522

why????  I expected:

2.521

2.522

HALF_UP的定义是这样的:

“Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up. Behaves as for RoundingMode.UP if the discarded fraction is ≥ 0.5; otherwise, behaves as for RoundingMode.DOWN. Note that this is the rounding mode commonly taught at school.”

后来终于知道了原因,算是比较坑爹了。

BigDecimal的构造函数需要用String作为参数,否则将会出现一些比较奇怪的结果。所以上面的程度如果修改为:

import java.math.*;

public class FFBigDecimal {

public static void main(String[] args) {

BigDecimal bigDecimal1 = new BigDecimal("1.001");

BigDecimal bigDecimal2 = new BigDecimal("1.0005");

BigDecimal bigDecimal3 = new BigDecimal("1.000007");

//test 1

//bigDeciimal3 is immutable, so

// WRONG: bigDecimal3.add(bigDecimal1).add(bigDecimal2);

// CORRECT: bigDecimal3 = bigDecimal3.add(bigDecimal1).add(bigDecimal2);

bigDecimal3 = bigDecimal3.add(bigDecimal1).add(bigDecimal2);

BigDecimal bigDecimaBase = new BigDecimal("2.52150");

BigDecimal bigDecimal4 = bigDecimaBase.setScale(3, RoundingMode.HALF_DOWN);

System.out.println("Big Decimal is " + bigDecimal4.toString());

BigDecimal bigDecimal5 = bigDecimaBase .setScale(3, RoundingMode.HALF_UP);

System.out.println("Big Decimal is " + bigDecimal5.toString());

}

}

就是符合预期的,得到的输出结果将是:

Big Decimal is 2.521

Big Decimal is 2.522

3.2 如果在Double和Float中二选一,选择Double.

Double (8 位)

Float (4 位)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java BigDecimalJava中用于高精度计算的类。它允许我们进行任意精度的数字计算,而不会出现舍入误差。 在Java中,基本数据类型(如int、double等)的计算是有限制的。例如,double类型只能存储15位有效数字,而且在计算过程中可能会出现舍入误差。这在需要精确计算的场合下是不可接受的,这就需要使用BigDecimal类。 以下是一些Java BigDecimal的常用方法: 1. 实例化BigDecimal对象 可以使用BigDecimal的构造函数来实例化一个对象,例如: ``` BigDecimal num1 = new BigDecimal("1234.5678"); BigDecimal num2 = new BigDecimal(9876.5432); ``` 2. 加法、减法、乘法和除法 可以使用add()、subtract()、multiply()和divide()方法进行加、减、乘和除运算,例如: ``` BigDecimal result1 = num1.add(num2); BigDecimal result2 = num1.subtract(num2); BigDecimal result3 = num1.multiply(num2); BigDecimal result4 = num1.divide(num2, 2, RoundingMode.HALF_UP); // 保留两位小数 ``` 3. 取反、取绝对值、取反余弦等 可以使用negate()、abs()、acos()等方法进行相应的计算,例如: ``` BigDecimal result5 = num1.negate(); // 取反 BigDecimal result6 = num1.abs(); // 取绝对值 BigDecimal result7 = new BigDecimal(Math.PI).acos(); // 取反余弦 ``` 4. 比较大小 可以使用compareTo()方法进行大小比较,例如: ``` int cmp = num1.compareTo(num2); if (cmp > 0) { System.out.println("num1 > num2"); } else if (cmp < 0) { System.out.println("num1 < num2"); } else { System.out.println("num1 = num2"); } ``` 以上是Java BigDecimal的一些常用方法,使用BigDecimal类可以很方便地进行高精度计算。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值