Stop PHP 5 from Rounding

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jackson Peebles

    #1

    Stop PHP 5 from Rounding

    Hey everyone! I'm a complete newbie to PHP, and am trying to teach
    myself how to make some scripts. So far I've done pretty good, but
    even after searching through all my books, articles, manuals, and
    search engines on PHP, I can't figure out how to stop PHP from
    rounding. I am trying to create a calculator that calculates a
    diameter * pi, and I got it, but I can't stop it from rounding to a few
    digits, which is very annoying. I'm looking at calculating like ten
    thousand digits and printing all of them (I know this doesn't sound
    convenient, but it will not be used for public use, just for some
    clients of mine that say they want accuracy). So far my calculator
    looks like this:


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
    />
    <title>Pi Calculator</title>
    </head>
    <?php
    include("variab les.php");
    ?>
    <body>
    <form action="" method="post">
    <input name="diameter" type="text" size="10" maxlength="500" />
    <select name="method1">
    <option value="add" selected="selec ted">+</option>
    <option value="sub">-</option>
    <option value="multi">* </option>
    <option value="div">/</option>
    </select>
    <input name="submit1" type="submit" value="Submit" />
    <input name="reset1" type="reset" value="Reset" />
    </form>
    <?php
    // Get variables from POST:
    $submit1 = $_POST['submit1'];
    $submit2 = $_POST['submit2'];
    $method1 = $_POST['method1'];
    $method2 = $_POST['method2'];
    $diameter = $_POST['diameter'];
    $second = $_POST['second'];
    $number = $_POST['number'];

    // Simple
    if ($submit1 == true) {
    switch ($method1) {
    case add: $ans = $first + $second;
    break;
    case sub: $ans = $first - $second;
    break;
    case multi: $ans = $diameter * $pi;
    break;
    case div: $ans = $first / $second;
    break;
    }

    $ans = number_format($ ans, 500, '.', ',');
    echo "<p>$ans</p>";
    }
    ?>
    </body>
    </html>


    I know it isn't finished yet and that there is a lot of unneeded code,
    but I will fix that once I stop it from rounding. variables.php just
    has pi in it. If you can help me out I'd really appreciate it!

  • scuniverse@gmail.com

    #2
    Re: Stop PHP 5 from Rounding

    I dont see anything wrong with it... actually when I try your script on
    my editor I get a really big number like 100 or so digits.

    Did you try to do the same without the line $ans = number_format($ ans,
    500, '.', ','); ?

    Comment

    • tim

      #3
      Re: Stop PHP 5 from Rounding


      Jackson Peebles wrote:[color=blue]
      > Hey everyone!
      > I am trying to create a calculator that calculates a
      > diameter * pi, and I got it, but I can't stop it from rounding to a few
      > digits, which is very annoying.[/color]

      Hi there

      Floating point numbers are usually only accurate to about 14 decimal
      places and only close approximations of the number you expected.



      [color=blue]
      >I'm looking at calculating like ten thousand digits and printing all of them[/color]

      Check out the bcmath library, it is accurate to any number of digits
      you need but it comes at the expense of speed.

      The user comments say the last few digits are approximate so if you
      need accuracy to a 100 digits use 104 digits and ignore the last 4...



      Tim

      Comment

      • lorento

        #4
        Re: Stop PHP 5 from Rounding

        >From the php manual :
        The returned float has a precision based on the precision directive in
        php.ini, which defaults to 14.

        Checkout your php.ini configuration :

        ; The number of significant digits displayed in floating point numbers.
        precision = 14


        To configure on the fly use this function :

        <?php

        ini_set('precis ion', '14');

        ?>


        Regards,

        Lorento
        --

        Buy the domain name Immersivelounge.com and launch your business with a premium domain and a high quality logo.


        Comment

        • tim

          #5
          Re: Stop PHP 5 from Rounding


          lorento wrote:[color=blue][color=green]
          > >From the php manual :[/color]
          > The returned float has a precision based on the precision directive in
          > php.ini, which defaults to 14.
          >[/color]

          The precision setting alters the number of digits displayed in echo
          statements but it does not affect the precision of floating points
          calculations.
          [color=blue]
          >From the manual[/color]

          The size of a float is platform-dependent, although a maximum of
          ~1.8e308 with a precision of roughly 14 decimal digits is a common
          value (that's 64 bit IEEE format).

          For higher accuracy a software math library is needed. Bcmath does
          this, slowly...

          Regards

          Tim

          Comment

          Working...