You are here:

- Home
- Computing/Technology
- C/C++
- C++
- C++ constructors and float vs double

Advertisement

Good day,

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:

myclass() {

n = 0;

}

and this one:

myclass():n(0)

{

}

Secondly, what is the technical difference between double and float. What should be considered when deciding which one to use.

Thanks!

1/ Constructors

---------------

The first form:

myclass()

{

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, http://en.wikipedia.org/wiki/IEEE_754 etc.

Hope this is of use.

- Add to this Answer
- Ask a Question

Rating(1-10) | Knowledgeability = 10 | Clarity of Response = 10 | Politeness = 10 |

Comment | No Comment |

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines.
Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is).
When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD.
My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes.
I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.**Education/Credentials**