Effective Java Item 48: Avoid float and double if exact answers are required
对于精确计算不提倡使用float,double,因为会丢失精度,这是为什么呢? 让我细细说来
1. 什么是浮点数?
表示一个数字如100.2,可以是Fixed point也就是100.2,也可以是Floating point(也就是科学计数法scientific notation)1.002 × 102.
通常是以第一个非零数字作为小数点前的数,也被称为normalized form,也就是说上面的数字不会表示成100.2 × 100
或0.1002 × 103
浮点数的优点是能表示Fixed point无法表示的超大和超小数值
2. IEEE Standard 754 关于floating number在计算机中表示的定义。
Java中float(单精度浮点),double(双精度浮点)也遵照次标准设计。
- The sign bit is 0 for positive, 1 for negative. 符号位0是正,1是负
- The exponent(幂值)'s base is two. 幂值是2
- The exponent field contains 127 plus the true exponent for single-precision(单精度),
or 1023 plus the true exponent for double precision(双精度).
- The first bit of the mantissa is typically assumed to be 1.f, where f is the field of fraction bits.
尾数中第一位任何情况下都是1(因为binary中只有0,1),所以不用占空间,所以fraction bits都用来存储.f


11110000 11001100 10101010 00001111 // 32-bit integer
= +1.1110000 11001100 10101010 x 231 // Single-Precision Float
= 11110000 11001100 10101010 00000000 // Corresponding Value
就是用long来做internal value存储amount的数值的。
则会提示不能将double转化成float 这是窄型转化。