# Maximum Power Transfer Theorem

Printable View

• 02-23-2012
Sudndeth
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); }```
• 02-23-2012
oogabooga
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.
• 02-23-2012
manasij7479
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)"
• 02-23-2012
anduril462
Also, main should be declared to return an int, not a double.
• 02-23-2012
manasij7479
Quote:

Originally Posted by anduril462
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..?)
• 02-23-2012
oogabooga
Quote:

Originally Posted by manasij7479
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.
• 02-23-2012
manasij7479
Quote:

Originally Posted by oogabooga
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.
• 02-23-2012
Sudndeth
Quote:

Originally Posted by manasij7479
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); }```
• 02-23-2012
oogabooga
@manasij: Now that I think about it, I see that you're right. :) I wonder what the difference is between the two compilers.
• 02-23-2012
oogabooga
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.
• 02-23-2012
Sudndeth
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); }```