Results 1 to 7 of 7

Thread: X = Ax^2 + Bx + C

  1. #1
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default X = Ax^2 + Bx + C

    I am creating a Quadratic equation script, and i have this so far..
    <?php
    $a = $_GET["a"];
    $b = $_GET["b"];
    $c = $_GET["c"];
    $negb = -$b;
    $2a = 2 * $a;
    $square = sqrt(pow($b,2) -4 * $a * $c);
    if is_nan($square) == 1 {echo(No Roots!)};
    else {
    $add = $negb + $square;
    $sub = $negb - $square;
    $x1 = $add / $2a;
    $x2 = $sub / $2a;
    echo($x1 ."<br />". $x2)};
    ?>
    i hope noone got hurt... please excuse my overuse of variables...

    and im also trying to figure out how to create a guest page on my site... IE bulliten board where people can write what they want, delete what they want, etc. but with less restrictions.
    all i need to do is figure out how to change <> into ampersand characters and back when posting, then dump the written junk into a php file.
    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

  2. #2
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by boxxertrumps
    figure out how to change <> into ampersand characters and back when posting, then dump the written junk into a php file.
    What do you mean?

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <?php
    function quadraticRoots($a, $b, $c) {
      $d = $b * -1;
      $e = sqrt(($b * $b) - (4 * $a * $c)) / (2 * $a);
      return array($d + $e, $d - $e);
    }
    ?>
    ... if I remember correctly.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    X= (-B +and- squareroot(b^2-4ac))/2a
    the script is okay, right?

    im not sure yours is correct unless you divide the entire thing by 2a...
    didn't think to create a function....
    Last edited by boxxertrumps; 12-01-2006 at 04:01 PM.
    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

  5. #5
    Join Date
    Dec 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by boxxertrumps View Post
    X= (-B +and- squareroot(b^2-4ac))/2a
    the script is okay, right?

    im not sure yours is correct unless you divide the entire thing by 2a...
    didn't think to create a function....

    There is a major problem everyone is missing with this function and that is there are speacial cases using the QF. (ie b^2-4*a*c == 0) there are others too. But you will experience errors if you do not address theses speacial cases.

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Oh, you are right. I apologise.
    Code:
    <?php
    function quadraticRoots($a, $b, $c) {
      $d = $b * -1;
      $e = sqrt(($b * $b) - (4 * $a * $c));
      return $e === 0 and array(NAN, NAN) or array(($d + $e) / (2 * $a), ($d - $e) / (2 * $a));
    }
    ?>
    Last edited by Twey; 12-01-2006 at 11:00 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    modified the script to accept input then echo results

    <?php
    $a = $_GET["a"];
    $b = $_GET["b"];
    $c = $_GET["c"];
    function quadraticRoots($a, $b, $c) {
    $d = $b * -1;
    $e = sqrt(($b * $b) - (4 * $a * $c));
    return $e === 0 and array(NAN, NAN) or array(($d + $e) / (2 * $a), ($d - $e) / (2 * $a));
    };
    echo $e[1] ."<br/>". $e [2];
    ?>
    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

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
  •