EchoFool Posted January 19, 2008 Share Posted January 19, 2008 I have a while loop which is meant to multiply a number by itself only it gave me a strange ending result... This is what i got: $Level = 10; $Check = 0; $Power = 1; While($Check < $Level){ $Check = $Check + 1; If($Power == 1){ $Power = $Power + 1; }Else{ $Power = $Power * $Power; } } Echo $Power; And the response is: 1.34078079299E+154 Why is that? What did i do wrong.... its meant to loop 10 times. So the $Power should be 1028.. if my maths is correct. Hope you can help. Quote Link to comment https://forums.phpfreaks.com/topic/86794-solved-while-loop-issue/ Share on other sites More sharing options...
Daniel0 Posted January 19, 2008 Share Posted January 19, 2008 In this case a for loop would be better: $level = 10; $power = 1; for($i = 0; $i < $level; $i++) { if($power == 1) { $power += 1; } else { $power *= $power; } } Why not just use pow() though? Quote Link to comment https://forums.phpfreaks.com/topic/86794-solved-while-loop-issue/#findComment-443613 Share on other sites More sharing options...
EchoFool Posted January 19, 2008 Author Share Posted January 19, 2008 In this case a for loop would be better: $level = 10; $power = 1; for($i = 0; $i < $level; $i++) { if($power == 1) { $power += 1; } else { $power *= $power; } } Why not just use pow() though? I've never heard of pow() ? What is the $i coming from ? Also what is "$i++" ? Is that just to add 1 to its self? Quote Link to comment https://forums.phpfreaks.com/topic/86794-solved-while-loop-issue/#findComment-443616 Share on other sites More sharing options...
revraz Posted January 19, 2008 Share Posted January 19, 2008 Also, do the math. The first multiply is 2*2=4 Second is 4*4 Then 16*16 Then 256*256 So you are already over 1028 by the 4th round. Quote Link to comment https://forums.phpfreaks.com/topic/86794-solved-while-loop-issue/#findComment-443619 Share on other sites More sharing options...
Daniel0 Posted January 19, 2008 Share Posted January 19, 2008 $i is set by the for loop ($i = 0;). $i++ increments $i with 1 (it's equal to $i += 1 which is similar to $i = $i + 1). pow() is a function for exponential expressions. I.e. you can achieve what you want like this: $power = pow(2, 10); Quote Link to comment https://forums.phpfreaks.com/topic/86794-solved-while-loop-issue/#findComment-443622 Share on other sites More sharing options...
EchoFool Posted January 19, 2008 Author Share Posted January 19, 2008 So $power = pow(2, $level); would also work? I tried your for loop and got: 1.34078079299E+154 Which is what i got with my while loop.. what is going wrong ? Quote Link to comment https://forums.phpfreaks.com/topic/86794-solved-while-loop-issue/#findComment-443623 Share on other sites More sharing options...
revraz Posted January 19, 2008 Share Posted January 19, 2008 You are not doing it right, you want 1x1 2x2 3x3 4x4 But you are multiplying the result by itself instead. Quote Link to comment https://forums.phpfreaks.com/topic/86794-solved-while-loop-issue/#findComment-443624 Share on other sites More sharing options...
EchoFool Posted January 19, 2008 Author Share Posted January 19, 2008 You are not doing it right, you want 1x1 2x2 3x3 4x4 But you are multiplying the result by itself instead. That won't work cos 1x1 = 1 so if that was the result, then the result in a loop will always end up as 1. Quote Link to comment https://forums.phpfreaks.com/topic/86794-solved-while-loop-issue/#findComment-443626 Share on other sites More sharing options...
Daniel0 Posted January 19, 2008 Share Posted January 19, 2008 I tried your for loop and got: 1.34078079299E+154 Which is what i got with my while loop.. what is going wrong ? That's because I simply wrote the for loop as a replacement for your while loop. The problem still persists with the for loop as explained by revraz. I suppose I should have pointed that out. So $power = pow(2, $level); would also work? Yes. Quote Link to comment https://forums.phpfreaks.com/topic/86794-solved-while-loop-issue/#findComment-443632 Share on other sites More sharing options...
EchoFool Posted January 19, 2008 Author Share Posted January 19, 2008 1 + 1 = 2 2x2 = 4 4x4 = 8 8x8 = 16 Thats what im aiming to get. Not what revraz showed me. How would i get it to give me a number result not : 1.34078079299E+154 Cos i cannot work with 1.34078079299E+154 from that point on, its unusable. Quote Link to comment https://forums.phpfreaks.com/topic/86794-solved-while-loop-issue/#findComment-443633 Share on other sites More sharing options...
revraz Posted January 19, 2008 Share Posted January 19, 2008 Not sure how much more I can try to explain it. You are trying to multiply the RESULT by itself, when you need to multiply the step of the LEVEL by itself. Quote Link to comment https://forums.phpfreaks.com/topic/86794-solved-while-loop-issue/#findComment-443634 Share on other sites More sharing options...
Daniel0 Posted January 19, 2008 Share Posted January 19, 2008 8 * 8 ≠ 16 8 * 8 = 64 Also, 1.34078079299E+154 is a number (cf. http://en.wikipedia.org/wiki/Scientific_notation#E_notation). Quote Link to comment https://forums.phpfreaks.com/topic/86794-solved-while-loop-issue/#findComment-443635 Share on other sites More sharing options...
revraz Posted January 19, 2008 Share Posted January 19, 2008 And 4 X 4 = 16, not 8. Do you want the Power or do you want to add them? Quote Link to comment https://forums.phpfreaks.com/topic/86794-solved-while-loop-issue/#findComment-443637 Share on other sites More sharing options...
EchoFool Posted January 19, 2008 Author Share Posted January 19, 2008 I am suppose to multiply the result by itself ...not the step of the level... Example: If i multiply the step of the level by the result the numbers are too low... If result is 12044 then it needs to be 12044 X 12044 not: 12044 x 8 (as as example of the step of the level). My maths is poor i been mainly thinking in letters whilst making my scripts as formula's represent any numbers. Daniel - Is that still usable as integer in maths etc or will it crash out due to letters and decimal point ? Quote Link to comment https://forums.phpfreaks.com/topic/86794-solved-while-loop-issue/#findComment-443638 Share on other sites More sharing options...
Daniel0 Posted January 19, 2008 Share Posted January 19, 2008 Daniel - Is that still usable as integer in maths etc or will it crash out due to letters and decimal point ? Take this code for instance: <?php echo 1e5; ?> It will output: 100000 Quote Link to comment https://forums.phpfreaks.com/topic/86794-solved-while-loop-issue/#findComment-443639 Share on other sites More sharing options...
EchoFool Posted January 19, 2008 Author Share Posted January 19, 2008 Ok is there a way to force : 1.34078079299E+154 to come out as a normal huge number ? Like using number_format($power) ? Quote Link to comment https://forums.phpfreaks.com/topic/86794-solved-while-loop-issue/#findComment-443641 Share on other sites More sharing options...
Daniel0 Posted January 19, 2008 Share Posted January 19, 2008 <?php printf('%.0f', 1.34078079299E+154); ?> Output: 13407807929900000303292285556480489173702166434470022712912188298402309057064132476718872964460351551886434939009092324761906381094962708370334962038079488 Quote Link to comment https://forums.phpfreaks.com/topic/86794-solved-while-loop-issue/#findComment-443645 Share on other sites More sharing options...
EchoFool Posted January 19, 2008 Author Share Posted January 19, 2008 Ok thankyou Got it working Quote Link to comment https://forums.phpfreaks.com/topic/86794-solved-while-loop-issue/#findComment-443660 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.