A casting problem?

This is a discussion on A casting problem? within the C Programming forums, part of the General Programming Boards category; Hello there, I am having some strange results when trying to compute a simple math calculation. Can anyone please have ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    A casting problem?

    Hello there,

    I am having some strange results when trying to compute a simple math calculation. Can anyone please have a look at this code?

    Code:
    	for (i=N_DISPLAY-1 ; i>=0 ; i--)
    	{
    		digitos[i] = v_digito/(int)pow(10, (double)i);
    		
    		v_digito = v_digito - digitos[i]*(int)pow(10, (double)i);
    	}
    digitos[], i and v_digito are int types. pow is:

    double pow (double a, double b);

    What I am trying to do is to extract each of the algarisms of a number.

    Debuging I found out the firs expression seens to be Ok, but the second simple ads 1 or 2 to the desired number to be stored at v_digito.

    I suspect it is a casting problem as when I "play" with then I get diferent results, but none good..

    Can anyone help? Thank you!

  2. #2
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126
    Code:
            int N_DISPLAY = 4;
    	int digitos[4];
    	int v_digito = 7653;
    	int i;
    	
    	for (i=N_DISPLAY-1 ; i>=0 ; i--)
        {
    		digitos[i] = v_digito/(int)pow(10.0, (double)i);
    		printf("%ld\n", digitos[i]);
    		
    		v_digito = v_digito - digitos[i]*(int)pow(10, (double)i);
    		printf("%ld\n",v_digito);
        }
    This is what I run and the answer seems to be fine...
    Could you explain further what is the problem?

    Code:
    My answer:
    7
    653
    6
    53
    5
    3
    3
    0
    also tried the following variations and everything seems ok.
    Code:
    digitos[i] = (int) ((double)v_digito/pow(10.0, (double)i));
    v_digito = v_digito - digitos[i]*pow(10, (double)i);
    ;

    In the second expression, there is no necessity of casting.

    Regards
    Mac OS 10.6 Snow Leopard : Darwin

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    Quote Originally Posted by nacho4d View Post
    In the second expression, there is no necessity of casting.

    Regards
    Hello there, I removed this casting and it is working now!

    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. type casting problem?
    By lackofcolour in forum C Programming
    Replies: 6
    Last Post: 01-30-2006, 04:29 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 06:18 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21