C++/C++ constructors and float vs double
I have two different questions regarding C++.
First, I wanted to know what are the main technical differences and advantages/disadvantages between this constructor declaration:
n = 0;
and this one:
Secondly, what is the technical difference between double and float. What should be considered when deciding which one to use.
The first form:
n = 0;
is in general less efficient. This is because n is first default initialised and then assigned to in the constructor body - i.e. there are two operation on n.
However the second form:
myclass() : n(0)
only requires that n be initialised with the value zero - a single operation on n. This latter form is also more idiomatic C++.
If n were a data member of myclass of a built in type such as an int then there is probably not much difference as default construction of such types is a nop (no operation).
On the other hand if n were a data member of myclass of a class type that had a constructor that took a single integer value (or pointer value - remember 0 is convertible to the null pointer value in C++) then the first form - default initialise and then assign - is likely to be less efficient as both operation are likely to involve work. It also requires that the class which is the type of n supports both default construction and assignment from an integer or pointer (by whatever means), whereas the second form only requires that this class support construction from a single integer or pointer.
The final case is if n were a base class of myclass that a myclass constructor needs to initialise other that by use of the class n default constructor. In this case only the second form is legal C++.
2/ Float vs double.
As with other basic types in C/C++ the exact format for float, double and long double types is left up to the implementation. A long double has to have at least the exponent range and precision as a double which has to have at least the exponent range and precision of a float. Or more precisely in the words of the C++ standard (3.9.1 Fundamental Types, paragraph 8):
"There are three floating point types: float, double,
and long double. The type double provides at least
as much precision as float, and the type long double
provides at least as much precision as double.
The set of values of the type float is a subset of
the set of values of the type double; the set of
values of the type double is a subset of the set of
values of the type long double. The value representation
of floating point types is implementation defined."
However typical implementations implement double to have considerably more precision than float and to cover a greater range of exponent values. Long double however may be implemented to be the same as double, or it may be larger.
So then the choice comes down to what precision you require and how efficient you need to be in space and/or time. While using the greatest precision and range that you can might seem an obvious choice, the down side of doing so is that operations using doubles take longer (and presumably a long double operation may take even longer!) than that on floats (assuming doubles and long doubles have greater precision and exponent range than floats). Doubles (and long doubles) will also take up more space (again assuming doubles and long doubles have greater precision and exponent range than floats), so even if you have sufficient performance using doubles (or long doubles) you might like to use floats if they are sufficient for your computational needs and you have to store floating point values efficiently. Or you could store values as floats and convert to doubles (or long doubles) to perform computations.
A typical size for a float is 32-bits and for a double 64-bits. I have used C++ implementations in which long double is 64-bits (same as double) and 80-bits on Intel/AMD platforms.
Typical precisions and exponent ranges for float and double are:
32-bit : precision: 23 bits (6 to 7 decimal digits)
exponent: 8-bits ( 2 to the -126 to +127,
or 10 to the -38 to +38 (approx.) )
64-bit : precision: 52 bits (15 to 16 decimal digits)
exponent: 11-bits ( 2 to the -1022 to +1023,
or 10 to the -308 to +308 (approx.) )
For more on floating point please see other articles such as: http://en.wikipedia.org/wiki/Floating_point
Hope this is of use.