To whom it may concern,
We used to take advantage of type promotion rather than both with an explicit cast, if we could state the function in such a way.
According to the text from Victor Shtern Core C++: A Software Engineering Approach 2000, typecasting is a little bit of an awkward topic...he put it back on page 1174, and he mentions that "...cast operators and conversion constructors weaken the strong typing system of C++." And that it can make it a little more troublesome during maintenance and integration.
So to take advantage of such a promotion, rather than intentionally circumventing strong typing, using the most recent piece of code:
Code:
val += (double) 1 / ( (double) ( 2*i - 1 ) );
Which should really read:
Code:
val += double(1) / ( double( 2*i - 1 ) );
Or even better:
Code:
val += static_cast<double>(1) / ( static_cast<double>( 2*i - 1 ) );
We would do something like:
Code:
val += 1.0 / (2.0*i - 1.0);
When the integer variable 'i' is multiplied by a double the result is a double. It was derived from the membership of the operators, a topic in set theory. N < I < R < C or natural are a subset of integers a subset of real numbers a subset of complex numbers. Of course, I've included rational and irrational numbers in the collection of reals -- it is sufficient for this example.
Shtern mentioned that these typecast operators, static_cast, reinterpret_cast, const_cast, dynamic_cast and the typeid operators are relatively new to C++, add to possible type conversions and are intentionally verbose (read-ability).
As far as the logic in the code segments given is concerned, I would draw a flow chart and do a correctness proof. But I'm not the beginner here. Have you examined the material here, and the material resulting from your internet search, Fyoung? What sort of tools are you using to evaluate these code segments? Just throwing it at the compiler is a great way to learn the compiler's warning messages and language syntax, but the semantics (meaning) of a collection of statements takes some finesse sometimes.
Best Regards,
New Ink -- Henry