Thread: casting int to double in pow()

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    1

    casting int to double in pow()

    I'm fairly new to c so forgive me if this is glaringly obvious answer to this.

    val += (unsigned int)(pow(2.0, (double)m));

    m is an int loop counter. It is involved in some other equations; basically it would be easier for me to cast it to a double in this one case than change it to a double in the declaration.

    val += (unsigned int)(pow(2.0, ((double)m) ));

    I've tried using adding more brackets, but I still get the error.

    Thanks

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Code:
    val += (1 << m);
    The above avoids a ridiculous trip into floating-point land just to set the m'th bit.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    brewbuck's solution is the way to go...

    but why even cast m? int is a legal 2nd parameter for pow().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. casting Rand () to generate double, float etc.
    By melodia in forum C Programming
    Replies: 1
    Last Post: 11-17-2010, 12:00 AM
  2. trouble casting char to double
    By deprekate in forum C Programming
    Replies: 16
    Last Post: 03-03-2009, 03:18 PM
  3. casting double to singed int
    By patiobarbecue in forum C++ Programming
    Replies: 3
    Last Post: 02-27-2009, 04:24 PM
  4. Casting unsigned long division to a double?
    By smoothdogg00 in forum C Programming
    Replies: 5
    Last Post: 12-22-2006, 09:22 AM
  5. Rounding error when casting double to short
    By thetinman in forum C Programming
    Replies: 7
    Last Post: 10-25-2006, 12:48 PM