SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: division

  1. #1
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    division

    I am trying to make a code that performs a division however I am not having success and donīt really know how to correct the problem, for example division of 1000 by 10 gives me 10.00 which is wrong, can someone help me by taking a look at my code:

    Code PHP:
    <?php
    $dividend = 1000;
    $divisor = 10;
    $quotient = null;
    $reminder = $dividend;
    $multiplicator = 2;
    $decimal = false;
    while (strlen(substr(strrchr($quotient, "."), 1)) < 2) {
    	if (strlen($dividend) <= strlen($divisor)) {
    		if ($decimal === false) {
    			$dividend = str_pad($dividend, strlen($divisor) + 2, '.0');
    			$decimal = true;
    			} else {
    			$dividend = str_pad($dividend, strlen($divisor) + 1, 0);
    		}
    		if (strlen(strstr($dividend, '.')) > 1) {
    			if (strlen(strstr($quotient, '.')) <= 0) {
    				$quotient = $quotient . '.';
    			}
    		}
    	}
    	$x = strlen($dividend) - strlen($divisor);
    	$x = $x -($x * 2);
    	$dividend_ = substr($dividend, 0, $x);
    	if ($dividend_ < $divisor) {
    		$quotient = $quotient . '0';
    	} else {
    		while ($divisor < $dividend_) {
    			$divisor * $multiplicator;
    			$multiplicator ++;
    		}
    		$multiplicator --;
    		$quotient = '' . $quotient . $multiplicator;
    	}
    	//
    	echo '<br/>' . $dividend;
    	echo '<br/>' . $quotient;
    	$dividend = substr($dividend, 1);
    	$multiplicator = 2;
    }
    echo '<br/>' . $dividend_;
    echo '<br/>' . $quotient;
    ?>

    I know I can do 1000/10 but I am studying and was asked to do it this way is just that I canīt figure out how to correctly place the decimal point, I either end up with no decimal point, the decimal point being 10.00 or 1000.00 but cant get it to be 100.00 I get those by changing line 16 strlen(strstr($dividend, '.')) > 1 I change the last number from 1 through 3 1 gives me 10.00 2 gives me 1000.00 and 3 goes to an eternal loop because no decimal point is placed
    Do you get bothered because I do the same thing every day?
    Do you question why I do it?
    Then find something that you actually like doing!!!

    Stop thinking on what I do.

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Okay. I think some rethinking needs to happen.
    Code:
    while (strlen(substr(strrchr($quotient, "."), 1)) < 2) {
    ... what?
    You find the last occurance of a period.
    Then take a substring of length 1 from that point.
    Then take the strlen of...a... substring where you've defined the length...
    Then check if it's less than 2, which it always will be, because you've specified a length of 1. (and if it fails, you get 0)

    So... yeah. That... makes no sense to me. Moving on!

    If I divide 10 by 10000, then your strpad is going to make my 10 into "10.0.0." ... which... isnt really very good for a number to be.

    PHP Code:
        $x $x -($x 2); 
    You mean $x *= -1; (x - 2x = -x)

    PHP Code:
               $divisor $multiplicator
    this line doesnt do anything. What were you trying to do here?


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
  •