...

View Full Version : Simple maths not correct outcome.



sir pannels
03-15-2007, 10:41 AM
Morning everyone,

Ok I'm doing quite a long formula in PHP and it seems to be giving me strange results.
The below code does a mathmatical calculation on some numbers.. I have done the formula using my head aswell as a calculator and all works fine yet bad results when PHP is left doing it. So I'm wondering if PHP is getting confused by the brackets or if I have missed something I must do for this to work.

If anyone has any thoughts or advice/links it be much apperciated.

Many thanks,
Sir P :D


$unitvalue2 - ( $unitvalue1 / $unitvalue2 * (( $opponentvalue/2 ) / ( $unitvalue1/$unitvalue1 + $unitvalue1/$unitvalue2 + $unitvalue1/$unitvalue3 + $unitvalue1/$unitvalue4 + $unitvalue1/$unitvalue5 + $unitvalue1/$unitvalue6 + $unitvalue1/$unitvalue7 + $unitvalue1/$unitvalue8 + $unitvalue1/$unitvalue9 + $unitvalue1/$unitvalue10 + $unitvalue1/$unitvalue11)));

SpinCode
03-15-2007, 11:27 AM
$unitvalue2 - ( $unitvalue1 / $unitvalue2 * (( $opponentvalue/2 ) / ( $unitvalue1/$unitvalue1 + $unitvalue1/$unitvalue2 + $unitvalue1/$unitvalue3 + $unitvalue1/$unitvalue4 + $unitvalue1/$unitvalue5 + $unitvalue1/$unitvalue6 + $unitvalue1/$unitvalue7 + $unitvalue1/$unitvalue8 + $unitvalue1/$unitvalue9 + $unitvalue1/$unitvalue10 + $unitvalue1/$unitvalue11)));


Incorrect brackets used:

Each instance of $unitvalue1/$unitvalue1 + $unitvalue1/$unitvalue2
should be changed to

($unitvalue1/$unitvalue1) + ($unitvalue1/$unitvalue2)

That's the best practice method to follow, and a sure way of getting it right all the time. Use brackets wherever ambiguous.

sir pannels
03-15-2007, 11:36 AM
Hi Spin,

Cheers for your reply.

Did as you said, works great. I thought that many brackets would confuse PHP but is all good.

Thanks

koyama
03-15-2007, 02:39 PM
I suggest you try to simplify your formula. It appears that $unitvalue1 will cancel out many places.

I don't believe that the added parenthesis were necessary. The division operator '/' should bind stronger that the '+'. Just like in ordinary math.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum