Thursday, January 30, 2014

Floating Point vs Integer








Ints are faster simply because the instructions to do integer operations are faster. Floats and doubles require more cycles. Ints are used when you don't need decimals. Doubles are used for floating point arithmetic. They work fine for some values but for money, you should use BigDecimal or ints and maintain the money is cents (US currency). Otherwise, rounding error will occur. Also doubles have a larger exponents and mantissas than floats. I presume when you said you read about it you checked out IEEE 754.








Also, ints are 32 bits wide which, if unsigned, would be about 4.3 billion. But the high order bit for both floating point values and integers is the sign bit. So the large positive value an integer can store is (2^31)-1 where ^ is exponentiation.








Regards,







Jim















No comments:

Post a Comment