written by: Noel Kalicharan•edited by: J. F. Amprimoz•updated: 10/14/2008
In this article, we introduce the basic floating-point types – float and double – in C. We show how to write floating-point constants, how to write floating-point expressions and how expressions are evaluated.
slide 1 of 3
Floating-point numbers – the types float and double
A floating-point number is one which may have a fractional part. A floating-point constant can be written in one of two ways:
•the normal way, with an optional sign, and including a decimal point;for example,-3.75, 0.537, 81.792, 47.0;
•using scientific notation, with an optional sign, including a decimal point and including an ‘exponent’ part; for example, -0.375E1 meaning “-0.375 multiplied by 10 to the power 1", that is, -3.75. Similarly, 0.537 can be written as 5.37e-1, that is, “5.37 x 10". The exponent can be specified using either e or E.
Note that there are several ways to write the same number. For example, the following all represent the same number 27.96:
In C, we can declare a floating-point variable using either float or double. A float value is normally stored as a 32-bit floating-point number, giving about 6 or 7 significant digits. A double value is stored as a 64-bit floating-point number, giving about 15 significant digits.
A floating-point constant is of type double unless it is followed by f or F, in which case it is of type float. Thus 3.75 is of type double but 3.75f (or 3.75F) is of type float. Most calculations are done using double precision. The type float is useful if you need to store lots of floating-point numbers and you wish to use as little storage as possible (and can live with 6 or 7 digits of precision).
In this series, we will mostly use double when working with floating-point numbers.
slide 2 of 3
Floating-point expressions can be written using the following operators:
These operate as expected; in particular, division is performed in the usual way so that, for example, 19.0/5.0 gives the value 3.8.
Assume op1 and op2 are the two operands of an operator (in the expression 2+5: op1=2 and op2=5 are operands; + is the operator), the following shows the type of calculation performed:
op1 is float and op2 is float, type of caculation is float
op1 is double and op2 is float, type of caculation is double
op1 is float and op2 is double, type of caculation is double
op1 is double and op2 is double, type of caculation is double
Thus float is performed only if both operands are float; otherwise double is performed.