Precision of the Real Types

Precision of the Real Types


In mathematics the real numbers in a given range are countless (as opposed to the integers in that range) as between any two real numbers a and b there are countless other real numbers c where a < c < b. This requires real numbers to be stored in computer memory with a limited accuracy.
Since mathematics and physics mostly work with extremely large numbers (positive and negative) and with extremely small numbers (very close to zero), real types in computing and electronic devices must be stored and processed appropriately. For example, according to the physics the mass of electron is approximately 9.109389*10-31 kilograms and in 1 mole of substance there are approximately 6.02*1023 atoms. Both these values can be stored easily in float and double types.
Due to its flexibility, the modern floating-point representation of real numbers allows us to work with a maximum number of significant digits for very large numbers (for example, positive and negative numbers with hundreds of digits) and with numbers very close to zero (for example, positive and negative numbers with hundreds of zeros after the decimal point before the first significant digit).

Accuracy of Real Types – Example


The real types in C# we went over – float and double – differ not only by the range of possible values they can take, but also by their precision (the number of decimal digits, which they can preserve). The first type has a precision of 7 digits, the second – 15-16 digits.
Consider an example in which we declare several variables of the known real types, initialize them and print their values on the console. The purpose of the example is to illustrate the difference in their accuracy:


We see that the number π which we declared as float, is rounded to the 7-th digit, and the one we declared double – to 15-th digit. We can conclude that the real type double retains much greater precision than float, thus if we need a greater precision after the decimal point, we will use it.

Post a Comment

0 Comments