Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C++ Programming

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


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.

On 7 Jun., 03:58, aaragon <alejandro.ara@gmail.com> wrote:

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

"aaragon" <alejandro.ara@gmail.com> wrote in message

news:1181181519.657669.250120@q69g2000hsb.googlegroups.com...

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.

On Jun 7, 3:58 am, aaragon <alejandro.ara@gmail.com> wrote:

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

--
James Kanze (GABI Software)             email:james.ka@gmail.com
Conseils en informatique oriente objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34

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.

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! <G> Mind yer manners or i'll
attach something! <G> ]
--
Bob R
POVrookie

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc