Re: Newbie Questions: variable, operators
From: Karl Heinz Buchegger (kbuchegg_at_gascad.at)
Date: 01/05/05
- Next message: Chris \( Val \): "Re: VC6 Internal Error, My Fault?"
- Previous message: Chris \( Val \): "Re: exception handling .. more"
- In reply to: Jochen Rundholz: "Newbie Questions: variable, operators"
- Next in thread: Jochen Rundholz: "Re: Newbie Questions: variable, operators"
- Reply: Jochen Rundholz: "Re: Newbie Questions: variable, operators"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 05 Jan 2005 13:17:12 +0100
Jochen Rundholz wrote:
>
> Hi,
>
> while reading my C++ tutorial, three question came up I couldn't get an
> answer to. May be one of you can give me a quick idea:
>
> 1. float f=1.2; vs. float f=1.2F;
> I read that a literal type can be specified for example with the character F
> after the value. But the type of that literal is already specified by the
> expression float. So what is the difference between the two expressions
> above, or if this is not a valid question, what is the F good for? Any
> example?
But the above is not an expression.
It is a declaration. And it says:
There is a variable called 'f'. Its type is float. And it will
be initialized with the value of the expression of the right side of '='
In version one, the type of the expression used for the initialization is
double, while in version 2 it is float.
So in version 1, you initialize a float variable with a double value (which
of course gets converted to float), while in version 2 you initialize a float
variable with a float value.
By the way: Why are you using 'float'?
You should not use float, unless:
* you know what you do
* you know what problems may await you, and you are willing and have the
knowledge to fight that beast
* you have a very, very, very, very good reason to use float
In all other cases use double.
>
> 2. x = x + 10 vs. x += 10
> The book stated that x +=10 is faster in execution and thus is more often
> used by professional developers (okay I am still far away from
> professional, but...). I just wonder why the compiler treats both
> expressions differently if they work the same way?
I would expect most newer compilers to generate equal code in both cases.
It is a relatively trivial optimization in this specific case.
However:
There is an upper level of what compiler writers build into their
compilers. Not all optimizations that you or I would make are known
to a compiler or else they would need much tougher mathematical
analysis stages. The compiler writer is creating a compiler, not
a mathematical tool.
Eg. while you and I know, that
i = sin(angle)*sin(angle) + cos(angle)*cos(angle)
can be simplified to
i = 1.0
I would not expect any compiler to actually recognize this optimization.
So when C started (the predecessor of C++) it was a relatively simple
compiler. It relayed heavily on the programmer to give some hints about
possible optimizations. Those hints are still in the language. Some of
them have lost their usefulness over time (such as eg. 'register'), some
of them are still used, because they not only help the compiler in optimizing
but are also a neat way of saving a few keystrokes for the programmer.
>
> 3. Boolean true = 1
> The boolean value for true is 1, is there any reason for that? I would
> expect that true has the value 0, similiar to return code where 0 is a good
> result. And true is good...
First: There is no type 'Boolean' in C++. The type is called 'bool'
Second: There are historical reasons for that. In C++ ancestor, C, it
was the case that 0 denoted a boolean false, while every non-zero
was treated as true. C++ adopted that (note that there was no 'bool'
type in C) and introduced the type 'bool'. In order to stay compatible
with the old definition, the definitions for true and false are what they
are today.
All in all, it is just a convention. But it seems that having 0 for false
is somewhat more universal, as it generalizes for pointers easily, where
0 denotes a 'pointer to nothing'.
-- Karl Heinz Buchegger kbuchegg@gascad.at
- Next message: Chris \( Val \): "Re: VC6 Internal Error, My Fault?"
- Previous message: Chris \( Val \): "Re: exception handling .. more"
- In reply to: Jochen Rundholz: "Newbie Questions: variable, operators"
- Next in thread: Jochen Rundholz: "Re: Newbie Questions: variable, operators"
- Reply: Jochen Rundholz: "Re: Newbie Questions: variable, operators"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|