SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist tangledman's Avatar
    Join Date
    Sep 2005
    Location
    Puerto de Mazarron, Murcia, Spain
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Division by 0 problem

    hello everyone,

    I am generating a variable which is the result of the following, however code there are instances when there is no third party commission and the variable $NEGOTIATIONSthirdpartypercentcommission should equal zero.

    $NEGOTIATIONSthirdpartypercentcommission = $NEGOTIATIONSthirdpartyfinalcommission /$NEGOTIATIONSthirdpartyoriginalcommission*100;

    How do I fix this error

  2. #2
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Validate the variables before you do the calculation?
    Ian Anderson
    www.siteguru.co.uk

  3. #3
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,175
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)

    division by zero

    Maybe you could assign a default value that "approximates" infinity and then round the result down before you use it. eg.
    20/40*100 = 50%
    20/0*100 = not possible
    20/10000*100 = .2%
    floor(20/10000*100) = 0%
    btw. Just my personal preference, but I enclose expressions in parentheses for clarity. It helps when expressions get more involved eg.
    (20/40)*100 = 50%

  4. #4
    SitePoint Evangelist tangledman's Avatar
    Join Date
    Sep 2005
    Location
    Puerto de Mazarron, Murcia, Spain
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wrote this to try and validate the variable, but as you can see it's not quite right, as it simply assigns a value of 0 to $NEGOTIATIONSthirdpartypercentcommission

    <?

    $NEGOTIATIONSthirdpartyfinalcommission = 50;
    $NEGOTIATIONSthirdpartyoriginalcommission = 100;


    if ( $$NEGOTIATIONSthirdpartyoriginalcommission == "0" )


    {
    $NEGOTIATIONSthirdpartypercentcommission =$NEGOTIATIONSthirdpartyfinalcommission / $NEGOTIATIONSthirdpartyoriginalcommission*100;

    }

    $NEGOTIATIONSthirdpartypercentcommission = 0;


    echo "$NEGOTIATIONSthirdpartypercentcommission";

    ?>

  5. #5
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,175
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)

    assign default

    Try
    PHP Code:
    $NEGOTIATIONSthirdpartyfinalcommission 50;
    $NEGOTIATIONSthirdpartyoriginalcommission 0;

    $NEGOTIATIONSthirdpartyoriginalcommission = ($NEGOTIATIONSthirdpartyoriginalcommission != 0)?$NEGOTIATIONSthirdpartyoriginalcommission:10000;

    $NEGOTIATIONSthirdpartypercentcommission =floor($NEGOTIATIONSthirdpartyfinalcommission $NEGOTIATIONSthirdpartyoriginalcommission*100);

    echo 
    $NEGOTIATIONSthirdpartypercentcommission

  6. #6
    SitePoint Evangelist tangledman's Avatar
    Join Date
    Sep 2005
    Location
    Puerto de Mazarron, Murcia, Spain
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Solved it.

    switch ($NEGOTIATIONSthirdpartyoriginalcommission){
    case "0":
    $NEGOTIATIONSthirdpartypercentcommission= 0;
    break;
    default:
    $NEGOTIATIONSthirdpartypercentcommission=$NEGOTIATIONSthirdpartyfinalcommission/$NEGOTIATIONSthirdpartyoriginalcommission*100;

    break;
    }

  7. #7
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It's a one-liner if you use the ternary operator:

    PHP Code:
    $NEGOTIATIONSthirdpartypercentcommission = ($NEGOTIATIONSthirdpartyoriginalcommission) ? $NEGOTIATIONSthirdpartyfinalcommission $NEGOTIATIONSthirdpartyoriginalcommission 100 0


Bookmarks

Posting Permissions

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