03.12.2012 Views

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

2.2. VARIABLES 21<br />

2.2.1 Constants<br />

Syntactically, constants are like special variables in C ++ with the additional attribute of immutability.<br />

const int integer1 = 2;<br />

const int integer3; // Error<br />

const float pi = 3.14159;<br />

const char mycharacter = ’a’;<br />

const bool cmp = integer1 < pi;<br />

As they cannot be changed, it is mandatory to set the value in the definition. The second<br />

constant definition violates this rule and the compiler will complain about it.<br />

Constants can be used where ever variables are allowed — as long as they are not modified, of<br />

course. On the other hand, constants like those above are already known during compilation.<br />

This enables many kinds of optimizations and the constants can be even used as arguments of<br />

types (we will come back to this later).<br />

2.2.2 Literals<br />

Literals like “2” or “3.14” have types as well. Simply spoken, integral numbers are treated as<br />

int, long or unsigned long depending on the number of digits. Every number with a dot or an<br />

exponent (e.g. 3e12 ≡ 3 · 10 12 ) is considered a double.<br />

Usually this does not matter much in practice since C ++ has implicit conversation between<br />

built-in numeric types and most programs work well without explicitly specifying the type of<br />

the literals. There are however three major reasons why paying attention to the types of literals:<br />

• Availability;<br />

• Ambiguity and<br />

• Accuracy.<br />

Without going into detail here, the implicit conversation is not used with template functions<br />

(<strong>for</strong> good reasons). The standard library provides a type <strong>for</strong> complex numbers where the type<br />

<strong>for</strong> the real and imaginary part can be parametrized by the user:<br />

std::complex z(1.3, 2.4), z2;<br />

These complex numbers provide of course the common operations. However, when we write:<br />

z2= 2 ∗ z; // error<br />

z2= 2.0 ∗ z; // error<br />

we will get an error message that the multiplication is not available. More specifically, the<br />

compiler will tell us that there is no operator∗() <strong>for</strong> int and std::complex respectively <strong>for</strong><br />

double and std::complex. 2 The library provides a multiplication <strong>for</strong> the type that we use<br />

<strong>for</strong> the real and imaginary part, here float. There are two ways to ascertain that “2” is float:<br />

z2= float(2) ∗ z;<br />

z2= 2.0f ∗ z;<br />

2 It is however possible to implement std::complex in a fashion such that these expressions work [Got11].

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!