Saturday, January 21, 2012

3.2. Real data type

When we want to store data with decimal digits, we cannot use the "int" data type. We need another data type to hold "real" (with decimals) numbers. In computing, there are two ways to handle real numbers:
  • Fixed point: the maximum number of decimal places is set beforehand, and the number of integer digits too. For example, if we use 3 digits for the integer part and 4 decimal places, the number 3.75 would be stored correctly (as 003.7500), and also the number 970.4361, but 5.678642 would be stored as 5.6786 (after the fourth decimal would be lost) and 1010 could not be stored (it has more than 3 integer digits). 
  • Floating Point: The allowed number of decimals and integer digits is variable, as the restriction is about the number of "significant figures" (starting from the last 0). For example, if we use 5 significant figures, we would be able to store correctly numbers as 0.0000007349 or 13405000000, but 12.0000034 would not be saved correctly, and it would be rounded to the nearest acceptable number.

No comments:

Post a Comment