Thread: Integer and decimal

  1. #1
    Registered User Bitojis's Avatar
    Join Date
    Jun 2005
    Posts
    20

    Integer and decimal

    Hello, I have a little problem with "float" and "double". The problem is that in "resultado", always appears a integral number, also when the division or the root result is decimal. I have tried to use float and double, and the result is the same. I donīt know why is happening that, in theory, "float" and "double" work with decimals.

    Code:
    int main(int argc, char* argv[]) 
    { 
    int a,b,c; 
    double raiz, resultado; 
    //[..] 
    
    if(c==0 & b!=0 & a!=0){ 
    printf("x=0"); 
    resultado = ((-b)/a); 
    printf("x=%d", resultado); 
    } 
    
    if (b==0 & a!=0){ 
    raiz = ((-c)/a); 
    if (raiz<0){ 
    printf("Sin solucion (raiz negativa)"); 
    } 
    else{ 
    resultado=sqrt(raiz); 
    printf("El resultado es: %d", resultado); 
    } 
    }
    Thanks,

  2. #2
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    printf("El resultado es: %f", resultado);

  3. #3
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    a, b and c are all integers, so when you do math when them you get integer math. Either cast them to float/double when doing your math or just change the datatype to float/double.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  4. #4
    Registered User Bitojis's Avatar
    Join Date
    Jun 2005
    Posts
    20
    Yes, the problem was that a, b, c had to be float or double. Thanks !!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  3. Checking Menu Items
    By Olidivera in forum Windows Programming
    Replies: 5
    Last Post: 06-12-2005, 01:12 PM
  4. All u wanted to know about data types&more
    By SAMSAM in forum Windows Programming
    Replies: 6
    Last Post: 03-11-2003, 03:22 PM
  5. Replies: 4
    Last Post: 01-29-2002, 02:42 AM