Arithmetic limitations
Expect the unexpected:
byte count = 91; // o.K.
int i = 91;
byte count2 = i; // Compile error: Incompatible types
// Required: byte Found: int
byte points = 130; // Compile error: Incompatible types
// Required: byte Found: int
Why not generally using float
/ double
in favour of seemingly more limited byte
,
short
,
int
,
long
for arithmetics?
float float2Power31 = Integer.MAX_VALUE + 1f; // 2^31 float floatDoubleMAX_VALUE = 2 * float2Power31 * float2Power31 - 1f; // 2^63 - 1 System.out.format( " Float value: %f\n", floatDoubleMAX_VALUE); System.out.println("Expected value: " + Long.MAX_VALUE);
Result:
Float value: 9223372036854776000.000000 Expected value: -9223372036854775807 ------------------------------------------ Difference: 193
//Print 15 fractional digits DecimalFormat df = new DecimalFormat("#.###############"); System.out.println(df.format(Float.intBitsToFloat( 0b0_10000000_00001100110011001100110 ))); // The smallest possible step above previous value System.out.println(df.format(Float.intBitsToFloat( 0b0_10000000_00001100110011001100111 ))); |
2.099999904632568 2.100000143051147 |
Figure 102.
FloatConverter