A
aaragon
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<int> test(100);
randomize(test);
unsigned long x = 0;
for(int i=0; i<test.size(); i++)
x += (unsigned long)(allele(i)*std:ow((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:ow 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.
I wrote a very simple function to try to understand the casting of
variables in C++. The function is
function foo()
{
std::vector<int> test(100);
randomize(test);
unsigned long x = 0;
for(int i=0; i<test.size(); i++)
x += (unsigned long)(allele(i)*std:ow((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:ow 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.