日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2025/3/11 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java default parameter_JAVA菜鸟入门(7) default parameter , float/double vs BigDecimal 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1 ?java的允許函數(shù)的默認(rèn)參數(shù)嗎?

java不支持類似C++那樣,為函數(shù)設(shè)定默認(rèn)參數(shù),所以需要做的事情是,自己用函數(shù)重載的方式進(jìn)行模擬。如下

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不準(zhǔn)確

首先來(lái)驗(yàn)證一下

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

然后來(lái)看看原因:

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. 如何盡可能準(zhǔn)確地表示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.”

后來(lái)終于知道了原因,算是比較坑爹了。

BigDecimal的構(gòu)造函數(shù)需要用String作為參數(shù),否則將會(huì)出現(xiàn)一些比較奇怪的結(jié)果。所以上面的程度如果修改為:

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());

}

}

就是符合預(yù)期的,得到的輸出結(jié)果將是:

Big Decimal is 2.521

Big Decimal is 2.522

3.2 如果在Double和Float中二選一,選擇Double.

Double (8 位)

Float (4 位)

總結(jié)

以上是生活随笔為你收集整理的java default parameter_JAVA菜鸟入门(7) default parameter , float/double vs BigDecimal的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。