Thread: Maximum Power Transfer Theorem

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    3

    Maximum Power Transfer Theorem

    Hi im new to this community but i am quite ok in c programming. im using a
    Code:
    while(condition){}
    so it will test if a value is equal/greater/less if not adds/subtracts what it needs till its condition is no longer true.
    im using a while loop cause while loops will hold on to the statement till its false this will edit untill its where i want it to be.

    This is in fact a peace of HW/Project but what i dont understand is why for exaple if i entered 2000 for ResLD why will the Neon Green while condition be true and add 10 more to R2 if its so clear that.
    100 + 1900 < 2000 is not True. Jump to Neon Green
    After that Blue while loop does its thing cause
    100 + 1910 > 2000 its true till its false.
    then this code should of ended right? wrong!!! it goes in to an Endless loop.
    this is what make no sence as well while loop dark Red is tested @
    100 + 1900 != 2000 whitch is true: so its false. (end Code)
    it dosnt end hear. it just loops. again.

    now if you ented 2222 for ResLD. then the code will end flawlessly. so code does work right. in this case.

    im using Pelles C for Window XP to run/compile code. plz help and thanks in advance.

    Code:
    #include <stdio.h>
    
    #define R1 100
    
    /*----------------------------------------------------*/
    double main(){
        double ResLD;
        printf("Enter ResLD to find R2 ");
        scanf("%lf", &ResLD);
        printf("you entered %lf\n", ResLD);
        // Test 2000 for ResLD then Test 2222 ResLD
        // i dont know why this is.
        double R2 = 0;
        while((R1 + R2) != ResLD){
            while((R1 + R2) < ResLD) {R2 += 1000; printf("%lf\n",R2);}
    
            while((R1 + R2) > ResLD) {R2 -= 100; printf("%lf\n",R2);}
    
            while((R1 + R2) < ResLD) {R2 += 10; printf("%lf\n",R2);}
    
            while((R1 + R2) > ResLD) {R2 -= 1; printf("%lf\n",R2);}
        
        }
    printf(" i found your R2 Value for ResLD :-)");
    return(0);
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your program works correctly with gcc, so it appears to be a Pelles C problem (I've encountered other problems with it). gcc is much better. You can use an IDE like Codeblocks with it (or others).

    If you want you can contact Pelles C and let them know about the problem.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Never use direct equality comparison with floats or doubles, that gives precision errors like you're seeing here.
    Change "(R1 + R2) != ResLD" to test a range instead, like as follows.
    "(R1+R2)>ResLD+.001 &&(R1+R2) <ResLD -.001)"

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Also, main should be declared to return an int, not a double.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by anduril462 View Post
    Also, main should be declared to return an int, not a double.
    `Can't believe that it actually compiled with double ..
    Isn't it supposed to cause problems at the lower levels ?(Guess: corrupting stack when returning a big fat double instead of an int..?)
    Last edited by manasij7479; 02-23-2012 at 07:07 PM.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by manasij7479 View Post
    Never use direct equality comparison with floats or doubles, that gives precision errors like you're seeing here.
    That's certainly true about floating point values generally, but it's not the problem in this case since the numbers are all integral (albeit contained in a double).

    I forgot to mention that main was returning a double! I fixed that when I tested it in Pelles C, and it still didn't work correctly. It does work with gcc, though.

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by oogabooga View Post
    but it's not the problem in this case since the numbers are all integral (albeit contained in a double).
    The numbers being integral does not stop them from being represented by say.. 5.0000001, which may not be computed equal to 5.0000101.
    Maybe gcc does something internally to prevent that, but it still, is undefined bahaviour.

  8. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    Quote Originally Posted by manasij7479 View Post
    Never use direct equality comparison with floats or doubles, that gives precision errors like you're seeing here.
    Change "(R1 + R2) != ResLD" to test a range instead, like as follows.
    "(R1+R2)>ResLD+.001 &&(R1+R2) <ResLD -.001)"
    i like to thank oogabooga, manasij7479, anduril462, for the help.
    i appreciate the fast replies.

    i cant use a Rang of && cause the user is not entering R2 for the while loop. program has to find R2 automatically. so its declared as double R2=0; and if i use the && operators it will never be true. so i have to use || operators or !=. to get out/in (of) the code.

    i never seen "((R1+R2) > ResLD+.001 && (R1+ R2) < ResLD -.001)"
    whats in Dark Red idk what that did to the while loop.

    the only changes that were made are in Dark Green.
    and it stopped endless loop.

    but its still the same. for whats in while loop neon Green
    its still saying that 100 + 1900 < 2000 and adds 10 to R2

    Code:
    #include <stdio.h>
    
    #define R1 100
    /*--------------------------------------------------------*/
    
    double main(){
        double ResLD;
        printf("Enter ResLD to find R2 ");
        scanf("%lf", &ResLD);
        printf("you entered %lf\n", ResLD);
        // Test 1000 for ResLD then Test 1111 ResLD
        // i dont know why this is.
        double R2 = 0;
        while((R1+R2) > ResLD +.001 || (R1+R2) < ResLD -.001){
            while((R1 + R2) < ResLD) {R2 += 1000; printf("%lf\n",R2);}
    
            while((R1 + R2) > ResLD) {R2 -= 100; printf("%lf\n",R2);}
    
            while((R1 + R2) < ResLD) {R2 += 10; printf("%lf\n",R2);}
    
            while((R1 + R2) > ResLD) {R2 -= 1; printf("%lf\n",R2);}
        
        }
    printf(" i found your R2 Value for ResLD :-)");
    return(0);
    }

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    @manasij: Now that I think about it, I see that you're right. I wonder what the difference is between the two compilers.

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I still think this is an error in the Pelles C compiler.
    Code:
    This program:
    
    #include <stdio.h>
    
    void printDoubleBits(double d) {
        int i, j, cnt = 0;
        char *c = (char*)&d;
        // Count down for little-endian
        // (change to up for big-endian).
        for (i = 7; i >= 0; i--)
            for (j = 7; j >= 0; j--) {
                if (cnt++ == 1 || cnt == 13)
                    printf(" ");
                printf("%d", !!(c[i] & (1 << j)));
            }
        printf("\n");
    }
    
    int main() {
    	double d = 2000.0;
    	printDoubleBits(d);
    	return 0;
    }
    
    Prints this for gcc and MSVC:
      sign exponent    mantissa
         0 10000001001 1111010000000000000000000000000000000000000000000000
    and this for Pelles:
      sign exponent    mantissa
         0 10000001001 1111010000000000000000000000000000000000000000000001
    Surely Pelles is, if by some quirk in the standard not "wrong", at least the lesser implementation.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  11. #11
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    Thanks oogabooga.

    like manasij7479 said repetitively and i didn't listen. that floats/double can't be used for conditions/comparisons.
    only Int and Char can be used.

    the solution to my problem was to convert all my doubles to int. for the comparisons.
    so change while((R1 + R2) < ResLD) to while((int)(R1 + R2) < (int)ResLD)
    (int)number...


    Problem solved.
    her is the code if you guys want to use the idea, i came up with it on my own but im sure some one. did it first somewhere.

    -----------------------------------Find My number-----------------------------------------
    Code:
    #include <stdio.h>
    
    #define R1 100
    
    /*--------------------------------------------------*/
    
    int main(){
        double ResLD;
        printf("Enter ResLD to find R2 ");
        scanf("%lf", &ResLD);
        printf("you entered %lf
    ", ResLD);
        // Test 1000 for ResLD then Test 1111 ResLD
        // i dont know why this is.
        double R2 = 0;
        while((R1+R2) > ResLD +.001 || (R1+R2) < ResLD -.001){
            while((int)(R1 + R2) < (int)ResLD) {R2 += 1000; printf("%lf
    ",R2);}
    
            while((int)(R1 + R2) > (int)ResLD) {R2 -= 100; printf("%lf
    ",R2);}
    
            while((int)(R1 + R2) < (int)ResLD) {R2 += 10; printf("%lf
    ",R2);}
    
            while((int)(R1 + R2) > (int)ResLD) {R2 -= 1; printf("%lf
    ",R2);}
        
        }
    printf(" i found your R2 Value for ResLD :-)");
    return(0);
    }


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fermat's Last Theorem
    By Branden Holmes in forum C++ Programming
    Replies: 16
    Last Post: 01-27-2012, 03:02 PM
  2. Futurama Theorem
    By Ferreres93 in forum C Programming
    Replies: 48
    Last Post: 12-05-2011, 11:27 AM
  3. Separating Axis Theorem
    By Kenki in forum Game Programming
    Replies: 20
    Last Post: 05-22-2005, 11:49 AM
  4. Pythagorans theorem
    By Dummies102 in forum C++ Programming
    Replies: 2
    Last Post: 02-20-2002, 01:46 PM
  5. Galois Theorem
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-06-2002, 06:20 PM

Tags for this Thread