Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts

    Simple maths not correct outcome.

    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
    PHP Code:
    $unitvalue2 - ( $unitvalue1 $unitvalue2 * (( $opponentvalue/) / ( $unitvalue1/$unitvalue1 $unitvalue1/$unitvalue2 $unitvalue1/$unitvalue3 $unitvalue1/$unitvalue4 $unitvalue1/$unitvalue5 $unitvalue1/$unitvalue6 $unitvalue1/$unitvalue7 $unitvalue1/$unitvalue8 $unitvalue1/$unitvalue9 $unitvalue1/$unitvalue10 $unitvalue1/$unitvalue11))); 

  • #2
    New Coder
    Join Date
    Jan 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    $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.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    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

  • #4
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •