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

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::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.
 
P

peter koch

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::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
 
J

Jim Langston

aaragon said:
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::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.
 
J

James Kanze

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::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.
 
I

Ian Collins

James said:
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.
 
B

BobR

Ian Collins wrote in message...
James said:
[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! <G> Mind yer manners or i'll
attach something! <G> ]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top