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
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值