Re: Correct C++ tutorial part 3 "Intro to loops" available (Windows, mingw/msvc/std)
From: Alf P. Steinbach (alfps_at_start.no)
Date: 01/27/05
- Next message: teejbee_at_hotmail.com: "Re: class definition forgets values"
- Previous message: Attila Feher: "Re: C++ won't survive the next 10 years"
- In reply to: Attila Feher: "Re: Correct C++ tutorial part 3 "Intro to loops" available (Windows, mingw/msvc/std)"
- Next in thread: Attila Feher: "Re: Correct C++ tutorial part 3 "Intro to loops" available (Windows, mingw/msvc/std)"
- Reply: Attila Feher: "Re: Correct C++ tutorial part 3 "Intro to loops" available (Windows, mingw/msvc/std)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 27 Jan 2005 08:49:46 GMT
* Attila Feher:
> Alf P. Steinbach wrote:
> > * Bogdan Sintoma:
> >> Hi Alf,
> >>
> >> In the chapter 8 maybe: "Always use an integral type such as int for
> >> a loop control variable" is 'to strong'. I think to something like:
> >> "chose the desired precision when dealing with floats and avoid
> >> 'equal' operators".
> >
> > Well, here's my reasoning:
> >
> > * If you use e.g. 'double' as a loop control variable (counter), you
> > either use integer update (e.g. increment) or non-integer update.
>
> Yeah. And you use either C++ or not to write it. ;-)
That's the context: a numeric loop control variable, with two mutually
excluding and exhaustive possibilities then discussed.
> > * If you use integer update you should use integer type for the
> > variable.
>
> Why? You have introduced your lines with the word reasoning, but I see no
> reason told for this statement.
It seems to boil down to two issues: why it's not a good idea to use a
misleading type (maintainability), and whether a floating point type can
always represent all values of an integral type exactly (technical).
> > * If you use non-integer update you're in trouble, and/or are
> > doing things in a needlessly complicated way; either way the
> > solution is to use integer type for the loop control variable.
>
> Again, no reasoning. Using floating point numbers where floating point
> operations are needed is (IMHO) not needlessly complicated for two reasons:
> 1.) Read the beginning of the sentence, 2.) using floating point is only
> complicated until you learn it.
Yes, to the latter two sentences. I wonder whether you are happy with
the resolution of that issue as it turned out in the rest of the thread?
That is, adding the parenthetical expression "unless you really know
what you're doing"?
> > Additionally, it would take much text to explain how to compare
> > floating point values "safely", and what degree of safety can be
> > achieved (it's an advanced topic one can write a whole book about...).
>
> That is the only reasoning in your text. Which suggests that instead of
> making up a rule which is not supported by any real arguments (original
> says: "Always use an integral type such as int for a loop control variable",
> so it effectively rules out iterators as well as a billion other things) why
> not tell simply what you have said above: floating point arithmetics is a
> topic of its own, not something which belongs to a beginner's tutorial?
Iterators and pointers as loop control variables is a good point,
thanks.
The discussion above was trapped in the implicit context of numeric loop
control variables.
Possibly I should add the word "numeric" in the advice. But I think it
would then hang in thin air, not related to the rest of the discussion.
Suggestions?
-- A: Because it messes up the order in which people normally read text. Q: Why is it such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail?
- Next message: teejbee_at_hotmail.com: "Re: class definition forgets values"
- Previous message: Attila Feher: "Re: C++ won't survive the next 10 years"
- In reply to: Attila Feher: "Re: Correct C++ tutorial part 3 "Intro to loops" available (Windows, mingw/msvc/std)"
- Next in thread: Attila Feher: "Re: Correct C++ tutorial part 3 "Intro to loops" available (Windows, mingw/msvc/std)"
- Reply: Attila Feher: "Re: Correct C++ tutorial part 3 "Intro to loops" available (Windows, mingw/msvc/std)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]