By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 1,005 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

is it ok to cast variables? simple function using std::pow

P: n/a
Hi everyone,

I wrote a very simple function to try to understand the casting of
variables in C++. The function is

function foo()
{
std::vector<inttest(100);
randomize(test);
unsigned long x = 0;
for(int i=0; i<test.size(); i++)
x += (unsigned long)(allele(i)*std::pow((double)k,
(double)i));
}

The function randomize just creates random values inside the vector.
This was the only way I made it to work without having any warnings
from the compiler. Since the std::pow takes two doubles as parameters,
I needed to cast the integer values into doubles. Also, I needed to
cast those double results into an unsigned long integer. I don't
really like using casting (I have a bad feeling every time I use it)
but I couldn't find a better way to do this. Does anyone have a better
way to do it? Is there anything wrong with casting variables into
different types?

Thank you all.

Jun 7 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 7 Jun., 03:58, aaragon <alejandro.ara...@gmail.comwrote:
Hi everyone,

I wrote a very simple function to try to understand the casting of
variables in C++. The function is

function foo()
{
std::vector<inttest(100);
randomize(test);
unsigned long x = 0;
for(int i=0; i<test.size(); i++)
x += (unsigned long)(allele(i)*std::pow((double)k,
(double)i));
}

The function randomize just creates random values inside the vector.
This was the only way I made it to work without having any warnings
from the compiler. Since the std::pow takes two doubles as parameters,
I needed to cast the integer values into doubles. Also, I needed to
cast those double results into an unsigned long integer. I don't
really like using casting (I have a bad feeling every time I use it)
but I couldn't find a better way to do this. Does anyone have a better
way to do it? Is there anything wrong with casting variables into
different types?

Thank you all.
It is difficult to give a definite reply when I can't see your code
(allele is undefined), but if the problem is a warning when converting
from double to unsigned long, I'd put in a static_cast. The casts to
double are not needed.
I would never allow a C-style cast to creep into my code, so get rid
of those.

/Peter

Jun 7 '07 #2

P: n/a
"aaragon" <al**************@gmail.comwrote in message
news:11**********************@q69g2000hsb.googlegr oups.com...
Hi everyone,

I wrote a very simple function to try to understand the casting of
variables in C++. The function is

function foo()
{
std::vector<inttest(100);
randomize(test);
unsigned long x = 0;
for(int i=0; i<test.size(); i++)
x += (unsigned long)(allele(i)*std::pow((double)k,
(double)i));
}

The function randomize just creates random values inside the vector.
This was the only way I made it to work without having any warnings
from the compiler. Since the std::pow takes two doubles as parameters,
I needed to cast the integer values into doubles. Also, I needed to
cast those double results into an unsigned long integer. I don't
really like using casting (I have a bad feeling every time I use it)
but I couldn't find a better way to do this. Does anyone have a better
way to do it? Is there anything wrong with casting variables into
different types?
Bascially, that's how I also use pow with intergers, although I use
static_cast rather than a c-style cast as it's explicit what I'm doing. (
static_cast<double>k, static_cast<double>i )

I put a static_cast in when I get a warning and I know that what I'm doing
is what I want to do and I either won't be getting over/underflow or I'm
handling it myself somewhere.
Jun 7 '07 #3

P: n/a
On Jun 7, 3:58 am, aaragon <alejandro.ara...@gmail.comwrote:
I wrote a very simple function to try to understand the casting of
variables in C++. The function is
function foo()
{
std::vector<inttest(100);
randomize(test);
unsigned long x = 0;
for(int i=0; i<test.size(); i++)
x += (unsigned long)(allele(i)*std::pow((double)k,
(double)i));
}
The function randomize just creates random values inside the vector.
This was the only way I made it to work without having any warnings
from the compiler. Since the std::pow takes two doubles as parameters,
I needed to cast the integer values into doubles.
Nonsense. Int's convert implicitly to double, and it's a
non-lossy conversion, so there's not even the slightest excuse
for a warning.
Also, I needed to
cast those double results into an unsigned long integer.
That's also an implicit conversion, although since it is a lossy
one, a warning is somewhat understandable. I would use a cast
here, if only to tell the reader that I meant it.
I don't
really like using casting (I have a bad feeling every time I use it)
but I couldn't find a better way to do this. Does anyone have a better
way to do it? Is there anything wrong with casting variables into
different types?
Well, there's certainly nothing wrong with converting a value to
a different type, when that's what you want. Sometimes, it's
even necessary:
std::cout << instanceCount / totalCount * 100 << " percent" ;
won't give the desired results unless you convert at least one
of instanceCount or totalCount to a floating point type. And
surely you've written things like:
f( std::string( a ) + ".txt" ) ;
, casting a C style string to an std::string.

These are conversion casts, involving values, and there's
generally nothing wrong with them. If the conversion is "lossy"
(the target type can't hold all of the information present in
the orginal type), then some care should be exercised,
particularly in cases like converting to an integral value from a
floating point (where there are several different rounding rules
you might want to apply), but otherwise, I see no problem with
them.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 7 '07 #4

P: n/a
James Kanze wrote:
On Jun 7, 3:58 am, aaragon <alejandro.ara...@gmail.comwrote:
>I wrote a very simple function to try to understand the casting of
variables in C++. The function is
>function foo()
{
std::vector<inttest(100);
randomize(test);
unsigned long x = 0;
for(int i=0; i<test.size(); i++)
x += (unsigned long)(allele(i)*std::pow((double)k,
(double)i));
}
>The function randomize just creates random values inside the vector.
This was the only way I made it to work without having any warnings
from the compiler. Since the std::pow takes two doubles as parameters,
I needed to cast the integer values into doubles.

Nonsense. Int's convert implicitly to double, and it's a
non-lossy conversion, so there's not even the slightest excuse
for a warning.
Just curious, is it guaranteed to be non-lossy? I'd have thought it
depends on the size of int whether all value of int are exactly
representable as a double.

--
Ian Collins.
Jun 7 '07 #5

P: n/a

Ian Collins wrote in message...
James Kanze wrote:
[snip]
Nonsense. Int's convert implicitly to double, and it's a
non-lossy conversion, so there's not even the slightest excuse
for a warning.
Just curious, is it guaranteed to be non-lossy? I'd have thought it
depends on the size of int whether all value of int are exactly
representable as a double.
[ being in a 'scatter-brained' mood and only haveing a single-brain-cell,
I'd read the following with great caution! <G]

Since the standard only qualifies an 'int' type to 16 bits, I'd say "yes"
(IMHO).
If a system increases an 'int' to 32 bits (one bit being the sign bit), and
does not increase the double's bits, I'd NOT buy that system!!
[ I think you are thinking about the fractional part of the double (which
will be zero(s) for an int conversion). See below (IntMax).]

Test your own system:

#include <limits// and <iostream>
{
using std::cout // for NG post
cout<<" dbl max() ="
<< std::numeric_limits<double>::max()<<std::endl;
cout<<" int max() ="<< std::numeric_limits<int>::max()<<std::endl;
cout <<" dbl digits ="
<<(std::numeric_limits<double>::digits)<<std::endl ;
cout <<" int digits ="
<<(std::numeric_limits<int>::digits)<<std::endl;
double IntMax( std::numeric_limits<int>::max() );
cout <<" double IntMax ="<<IntMax<<std::endl;

// assumes your system has type 'long long' ( like GNU GCC).
cout<<" LL max() ="
<< std::numeric_limits<long long>::max()<<std::endl;
cout <<" LL digits ="
<<(std::numeric_limits<long long>::digits)<<std::endl;
double LLMax( std::numeric_limits<long long>::max());
cout <<" double LLMax ="<<LLMax<<std::endl;
}
/* - output (win98se, P4) -
dbl max() =1.79769e+308
int max() =2147483647
dbl digits =53
int digits =31
double IntMax =2147483647.000000
LL max() =9223372036854775807
LL digits =63
double LLMax =9223372036854775800.000000
*/
Note the loss in 'LLMax'. (trying to stuff 63bits into 53bits)

If you are not sure, use a 'long double'! <G>
cout<<" LD digits ="
<<(std::numeric_limits<long double>::digits)<<std::endl;
// LD digits =64

Or, were you asking if the C++ standards committee "guarantees it"?

[ Ok, the laugh is NOT affecting me, blah! <GMind yer manners or i'll
attach something! <G]
--
Bob R
POVrookie
Jun 8 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.