FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


fraction function





badai
can anyone figure this out. how to use this class?

i just want to add 1/2 + 1/3 + 1/6 and get the result in x/y.

Code:
<?php

   /**
    * Trida Fraction pro praci se zlomky.
    *
    * Trida Fraction obsahuje vsechny dulezite procedury pro praci se zlomky :
    *  * Kraceni zlomku na zakladni tvar
    *  * Predavedeni realnych cisel na zlomky
    *  * Zakladni matematické operace
    *    * Scitani
    *    * Odecitani
    *    * Nasobeni
    *    * Deleni
    *
    * @author Tomas Lang <tomas.lang@gmail.com>
    * @copyright Copyright (c) 2008+, Tomas Lang
    * @version 1.2
    * @category Mathematic
    * @package Basic
    */
   class Fraction {
      /**
       * @var int $numerator Citatel zlomku
       */
      private $numerator;
      /**
       * @var int $denominator Jmenovatel zlomlu
       */
      private $denominator;

      /**
       * Konstruktor
       *
       * Vytvori novou instanci tridy zlomek, nacte citatel a jmenovatel a
       * nakonec zlomek zkrati do zakladniho tvaru.
       *
       * @param float $numerator Citatel zlomku
       * @param float $denominator Jmenovatel zlomlu
       *
       * @todo: $number | $denominator = object Fraction
       */
      public function __construct( $numerator = 1, $denominator = 1 ) {
         $numerator = $this->realToFraction( $numerator );
         $denominator = $this->realToFraction( $denominator );
         
         $this->numerator = (int)( $numerator[ 0 ] * $denominator[ 1 ] );
         $this->denominator = (int)( $denominator[ 0 ] * $numerator[ 1 ] );
         
         if ( $this->denominator == 0 ) {
            throw new Exception( 'Denominator can\'t be 0!' );
         }
         
         $this->reduction();
      }

      /**
       * Funkce prevede realne cislo do tvaru zlomku
       *
       * @param float $number
       *
       * @return array Pole o dvou prvcich (citatel, jmenovatel zlomku)
       */
      private function realToFraction( $number ) {
         $endOfNumber = $number - (int)$number;
         if ( $endOfNumber != 0 ) {
            $mul = bcpow( 10, strlen( $endOfNumber ) - 2 );
            return array( $number * $mul, $mul );
         } else {
            return array( $number, 1 );
         }
      }

      /**
       * Funkce vraci citatel zlomku
       *
       * @return float $this->numerator Citatel zlomku
       */
      public function getNumerator() {
         return $this->numerator;
      }

      /**
       * Funkce vraci jmenovatel zlomku
       *
       * @return float $this->denominator Jmenovatel zlomku
       */
      public function getDenominator() {
         return $this->denominator;
      }

      /**
       * Funkce zkrati zlomek do zakladniho tvaru
       *
       * Pokud je citatel i jmenovatel zaporny, pak je zlomek preveden na
       * kladny, pokud je zaporny pouze jmenovatel, je zapornost prenesena
       * na citatele a jmenovatel je preveden na kladny, nasledne pak je
       * zlomek zkracen do zakladniho tvaru na zaklade nalezeného nejvysiho
       * spolecného jmenovatele.
       */
      public function reduction() {
         if ( ( $this->numerator < 0 && $this->denominator < 0 ) || ( $this->denominator < 0 ) ) {
            $this->expansion( -1 );
         }
         $hcd = $this->highestCommonDivisor( $this->numerator, $this->denominator );
         $this->numerator /= $hcd;
         $this->denominator /= $hcd;
      }

      /**
       * Funkce vraci hodnotu zlomku v realném cisle
       *
       * @return float
       */
      public function getRealValue() {
         return $this->numerator / $this->denominator;
      }

      /**
       * Funkce vraci nejvysiho spolecného delitele cilel $a a $b
       *
       * @param int $a
       * @param int $b
       */
      public function highestCommonDivisor( $a, $b ) {
         $a = abs( $a );
         while ( $a != $b ) {
            if ( $a > $b ) {
               $a = $a - $b;
            }else {
               $b = $b - $a;
            }
         }
         return $a;
      }

      /**
       * Funkce vraci nejmensi spolecny nasobek cisel $a a $b
       *
       * @param float $a
       * @param float $b
       */     
      private function leastCommonMultiple( $a, $b ) {
         return ( $a * $b ) / $this->highestCommonDivisor( $a, $b );
      }

      /**
       * Funkce prevede tento a zadany zlomek, na zlomly se spolecnym
       * jmenovatelem
       *
       * @param object Fraction
       */
      public function commonDenominator( &$fraction ) {
         $lcm = $this->leastCommonMultiple( $this->denominator, $fraction->denominator );
         $this->numerator = $this->numerator * ( $lcm / $this->denominator );
         $fraction->numerator = $fraction->numerator * ( $lcm / $fraction->denominator );
         $this->denominator = $fraction->denominator = $lcm;
      }

      /**
       * Funkce rozsiri zlomek o hodnotu zlomku ci cisla $num
       *
       * @param object|float Fraction
       */
      private function expansion( $num ) {
         $this->numerator *= $num;
         $this->denominator *= $num;
      }

      /**
       * Funkce zkrati zlomek o hodnotu zlomku ci cisla $parametr
       *
       * @param object|float
       */
      private function contraction( $parametr ) {
         $this->numerator /= $num;
         $this->denominator /= $num;
      }

      /**
       * Pricte zadany zlomek ci cislo k tomuto zlomku a zkrati jej na
       * zakladni tvar
       *
       * @param object|float Fraction
       */
      public function add( $parametr ) {
         if ( !( $parametr instanceOf Fraction ) && is_numeric( $parametr ) ) {
            $parametr = new Fraction( $parametr );
         } else {
            throw new Exception( 'Parametr must be fraction or number!' );
         }

         $this->commonDenominator( $parametr );
         $this->numerator = $this->numerator + $parametr->numerator;

         $this->reduction();
      }

      /**
       * Odecte zadany zlomek ci cislo od tohoto zlomku a zkrati jej na
       * zakladni tvar
       *
       * @param object|float Fraction
       */
      public function sub( $parametr ) {
         if ( !( $parametr instanceOf Fraction ) && is_numeric( $parametr ) ) {
            $parametr = new Fraction( $parametr );
         } else {
            throw new Exception( 'Parametr must be fraction or number!' );
         }
         
         $this->commonDenominator( $parametr );
         $this->numerator = $this->numerator - $parametr->numerator;
         
         $this->reduction();
      }

      /**
       * Vynasobi tento zlomek zadanym zlomkem ci cislem a zkrati jej na
       * zakladni tvar
       *
       * @param object|float Fraction
       */
      public function multiplication( $parametr ) {
         if ( $parametr instanceOf Fraction ) {
            $numerator = $parametr->numerator;
            $denominator = $parametr->denominator;
         } elseif ( is_numeric( $parametr ) ) {
            $parametr = $this->realToFraction( $parametr );
            $numerator = $parametr[ 0 ];
            $denominator = $parametr[ 1 ];
         } else {
            throw new Exception( 'Parametr must be fraction or number!' );
         }
         
         $this->numerator *= (int)$numerator;
         $this->denominator *= (int)$denominator;
         
         $this->reduction();
      }

      /**
       * Vydeli tento zlomek zadanym zlomkem ci cislem a zkrati jej na
       * zakladni tvar
       *
       * @param object|float Fraction
       */
      public function division( $parametr ) {
         if ( $parametr instanceOf Fraction ) {
            $numerator = $parametr->numerator;
            $denominator = $parametr->denominator;
         } elseif ( is_numeric( $parametr ) ) {
            $parametr = $this->realToFraction( $parametr );
            $numerator = $parametr[ 0 ];
            $denominator = $parametr[ 1 ];
         } else {
            throw new Exception( 'Parametr must be fraction or number!' );
         }

         $this->numerator *= (int)$denominator;
         $this->denominator *= (int)$numerator;

         $this->reduction();
      }
   }

?>
jmraker
I'm sorry that I don't know the math terms...

I'm guessing it involves finding that 6 is the largest "something" the 2nd part of the fraction can all be multiplied into and multiply the 1st part of the fraction by what it takes to multiply to the 2nd part of the number. (for 1/2) it multiplies 1*3 because 2*3=6) where it's (3/6)+(2/6)+(1/6) and 3+2+1=6 so it would be 6/6 or 1.

Yup. I'll never be able to teach math.
noah
This shouldn't be too dificult to do.

1.Multiple all bottom numbers by themselves
2.Adjust type numbers
3.Add all numbers together
4. Reduce fractions

This is not the BEST way, but it is one trivially easy way to do it.
badai
oh.. for god sake.

i know how to add, subtract, multiply and divide fraction.

this is php and mysql board, not basic elementary mathematics.

you want to talk mathematics, go request for new board.

can someone translate the comment for me? thanks.
noah
I understand. I just told you algorithm to use to create a function so you wouldn't have to use someone elses. The functions are labeled well enough that if you played around with it and treated each function like a blackbox you could probably figure it out within an hour.
badai
i already have my own function. i even have that function call another function to get the fraction common divisor so i can "simpilfy" my fraction

it just that i found this nice php class (do you even know what class is. no, it's not about a group of students attending a specific course or lesson), and i really like to use it.

that's all.

no, i don't need you to teach me about fraction, my php function does work perfectly. it just not well written as this class.

oh, by the way, nobody need to write their own function, just google for it and you can get hundreds of it out there. that's how i wrote my own function. and among the search result is this one nice looking class.

now you know what's really going on here?
noah
haha, okay.

Sorry, I couldn't have been of more help.

In the future you shouldn't be so abusive towards people who are trying to help you.
badai
no problem, and sorry for my abusive language. i'll be more polite next time.

noah wrote:
haha, okay.

Sorry, I couldn't have been of more help.

In the future you shouldn't be so abusive towards people who are trying to help you.
jmraker
This might work

Code:
$f = array();
$f[] = new Fraction(1, 2);
$f[] = new Fraction(1, 3);
$f[] = new Fraction(1, 6);
$maxDenom = 1;
foreach($f as $frac)
   $maxDenom *= $frac->getDenominator();
$maxNum = 0;
foreach($f as $frac)
   $maxNum += $frac->getNumerator() * ($maxDenom / $frac->getDenominator());
$result = new Fraction($maxNum, $maxDenom);
var_dump($result);
Helios
badai wrote:
no problem, and sorry for my abusive language. i'll be more polite next time.

noah wrote:
haha, okay.

Sorry, I couldn't have been of more help.

In the future you shouldn't be so abusive towards people who are trying to help you.


Please be, because someone reported you and even asked me to give you a warning. I won't warn you, but watch your posts...
Related topics
PHP Mail Function
PHP function(); &file=""?
Search function
ftp function
F. Nietzsche
Function "Rotate90", how do do it guys?
javascript function not being seen
(server 2) php function exif_read_data() not supported ?
ScanDir function problem
date() function
Paste Function
PHP mail function ?
Php mail function
Lock table function?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.