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 位)