Thread: kind of problem bothering me ( pow or * ? )

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    17

    kind of problem bothering me ( pow or * ? )

    ok i have this code

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(){
        
        float a,b,c;
        
        for (a=1;a<=12;a++){
            
            for (b=1;b<=12;b++){
                
                  for (c=1;c<=12;c++){
                      
                      if (a*a+b*b==c*c){
                      
                                  if (a+b+c==12){    
                                                        
                                  printf("%.0f = %.0f\n",a*a+b*b,c*c);
                                  printf("%.0f\n%.0f\n%.0f\n",a,b,c);
                                  printf("a+b+c = %.0f\n",a+b+c);
                                  printf("-----------------------\n");
                                  printf("Final Result: %.0f\n",a*b*c);
                                  
                                  }
                                  
                                }
                            }
                      }
                }
            
            getchar();
            
         return 0;
         
    }
    it shows up the answer i want:

    Code:
    25 = 25
    3
    4
    5
    a+b+c = 12
    -----------------------
    Final Result: 60
    25 = 25
    4
    3
    5
    a+b+c = 12
    -----------------------
    Final Result: 60
    but why doesnt it show anything when using pow instead of *?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Since pow() is not in your code, it's kind of hard to tell what you mean.

    If you are using gcc, you must compile "gcc -lm" (link math) to actually use math.h
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    17
    im using dev C++

    btw here is the code using pow

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(){
        
        float a,b,c;
    
        for (a=1;a<=12;a++){
            
            for (b=1;b<=12;b++){
                
                  for (c=1;c<=12;c++){
                      
                      if (pow(a,2)+pow(b,2)==pow(c,2)){
                      
                                  if (a+b+c==12){    
                                                        
                                  printf("%.0f = %.0f\n",pow(a,2)+pow(b,2),pow(c,2));
                                  printf("%.0f\n%.0f\n%.0f\n",a,b,c);
                                  printf("a+b+c = %.0f\n",a+b+c);
                                  printf("-----------------------\n");
                                  printf("Final Result: %.0f\n",a*b*c);
                                  
                                  }
                                  
                                }
                            }
                      }
                }
    
            getchar();
            
         return 0;
         
    }

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Are you compiling it as ".C" or ".CPP" coz the code you gave worked fine with my compiler.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    17
    as .C

    dunno, it s not working for me :S

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    What is the program output, simply a blank DOS screen or something else?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Apparently dev c++ uses gcc, so you could test this by compiling the program on the command-line:

    gcc -o myprog myprog.c -lm

    Now run "myprog" and see if it works...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    17
    it shows only a blank DOS screen

    also i used gcc -o myprog ...

    but it didnt work,

    'gcc' is not recognized as an internal or external command,
    operable program or batch file.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    It doesn't show anything because the code is wrong. Well, actually, it's not really wrong, it just won't do exactly what you expect.
    The reason is that both a*a and pow(a, 2) are not exact numbers when represented as a floating point number. The latter always is; the first only when a is a floating point number. So there may be (and in your second case, there is) a difference in what you expect the value to be and what the value really is.

    So to solve this, don't compare floating point numbers directly. Take the difference between two you want to compare and make sure it's close enough to the value you want. For instance, if you want to compare a and b, do something like:
    Code:
    if(abs(a - b) < 1e-12) {
      // Numbers are equal
    }
    I'll leave it up to you to do this in your program.

  10. #10
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    The reason is that both a*a and pow(a, 2) are not exact numbers when represented as a floating point number.
    I also think that's the problem but I don't understand how the multiplication version and the pow version differ given that both use float variables. Do you suppose the compiler is somehow knowing to do integer arithmetic?

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    As long as you're storing integers in 'float' types, then yes, simple multiplication by itself results in more integers. Whereas if you use pow() you're going to get some round-off issues where the results differ from integers by a few bits. pow() uses logarithms internally, not simple sequences of multiplies. After all, it can not predict if you're going to pass it some huge and/or non-integer exponential.

    I suppose if the library function writers felt it was worth it they could build in short-cuts for low exponentials so that simple multiplication is used when possible. But I figure checks for that and the overhead in time penalties aren't worth the effort.
    Last edited by nonoob; 06-27-2009 at 11:35 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with passing structs to a function.
    By darsh1120 in forum C Programming
    Replies: 7
    Last Post: 03-11-2008, 04:36 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  4. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM