Help!!!!!! i am making a form that calculates this is the line that i am having problems with.
It works but when i add *.01+ the calculation is wrong. you can see the test page at
http://www.derolds.com/calc15.php

<?php
if (count($_POST) > 0 && isset($_POST["calc15TextArea2"])){
        $sum = floatval( $_POST["indexEditbox1"] ) + floatval( $_POST["indexEditbox2"] ) 
*.01+ floatval( $_POST["calc15Editbox3"] ) + floatval( $_POST["calc15Editbox4"] ) ;
     echo "Total is $ $sum";
}
?>

    First problem I see is this incorrect statement:

    Ok The total should be 100+100 .01 +100+100=402


      100 + 100 * .01 + 100 + 100
    = 100 + 1 + 100 + 100
    = 101 + 200
    = 301

      yes how do i fix it? 3 days i still cant get it. If i take *.01 out all adds fine. I need times 1 percent to be added to the first two. Then the last two added to that:bemused:

        Try using parentheses to group your calculation just to be on the safe side. You should also consider whitespace and indents to clarify your code. It's not very readable.

        Also, you might want to review your math. Last time I checked, 100.01 was exactly 1. If you want to "add 1%" to some value $x, you could have:

        $x = 100; // my value!
        $plus_one_percent = $x + (.01*$x);
        // which is the same as this:
        $plus_one_percent = $x*1.01;
        
          sneakyimp;11028455 wrote:

          Try using parentheses to group your calculation just to be on the safe side.

          Eh.. it's not really being on the "safe side" so much as it is being on the "correct side." The order of mathematical operations in PHP statements follows the same ones taught in primary school; multiplication occurs before addition, and expressions inside parentheses occurs before either.

          1 + 2 0 = 1
          (1 + 2)
          0 = 0

            chip;11028443 wrote:

            yes how do i fix it? 3 days i still cant get it. If i take *.01 out all adds fine. I need times 1 percent to be added to the first two. Then the last two added to that:bemused:

            So I think you're saying you want something like this?

            $sum = ( ( floatval($_POST["indexEditbox1"]) + floatval($_POST["indexEditbox2"]) ) * 1.01 ) + floatval($_POST["calc15Editbox3"]) + floatval($_POST["calc15Editbox4"]);
            

            I believe the " * 1.01 " to add 1 percent is the key change here, along with some parentheses to apply it to the sum of the first two values.

              Ok nogdog. Added parentheses I get error. Adding 1.01 I still get 100+1001.01=201 not 202. But that did fix it a little. Before .01 with all four fields filed in 100+100.01+100+100=301 After I Added 1.01 I get 401. OK I think you got me on the right path I changed 1.01 to 1.02 now the calculation is right. I think. 100+1001.02+100+100=402 I know this is probably not proper code. But it works for me.

                chip;11028481 wrote:

                I changed 1.01 to 1.02 now the calculation is right. I think.

                How many test cases did you try to determine this? (Or did you just play with the numbers until this one single example gave you the expected value and assumed the calculation was valid for all values?)

                Can you explain in words what it is you're trying to calculate? This:

                100+100*1.02+100+100

                is adding 2% to the second "100" before it is used in the summation. That is not the same as adding 1% to the sum of the first two numbers:

                  (100 + 100) * 1.01 + 100 + 100
                = 200 * 1.01 + 100 + 100
                = 202 + 100 + 100
                = 402

                EDIT: To illustrate the difference, replace the identical "100" values with "100", "200", "300", and "400" and try the two different expressions again:

                  100 + 200 * 1.02 + 300 + 400
                = 100 + 204 + 300 + 400
                = 10,004
                
                  (100 + 200) * 1.01 + 300 + 400
                = 300 * 1.01 + 300 + 400
                = 303 + 300 + 400
                = 10,003

                  Ok i got it now. I changed the line to this. Needed to get total of the first two first then add the second two.

                  $sum = (floatval($POST["indexEditbox1"]) + floatval($POST["indexEditbox2"])) * 1.01+ floatval($POST["calc15Editbox3"]) + floatval($POST["calc15Editbox4"]);

                    Yea kinda I had to change this ) ) 1.01 ) + floatval($POST to )) 1.01+ floatval($POST I had to remove ) after *1.01 and all worked so yep he got me there
                    Thank you nogdog

                      chip wrote:

                      Yea kinda I had to change this ) ) 1.01 ) + floatval($POST to )) 1.01+ floatval($POST I had to remove ) after *1.01 and all worked so yep he got me there

                      Sounds like you left off the second [font=monospace]([/font] (rather, the first) at the start of the line.

                        Write a Reply...