Number class in PHP

I’ve always wanted a class that can hold floats, and then simplify the values if possible, for my other Math program.

Well, I’ve finally gotten around to doing something:

<?php

class Number {
	private $num_one;
	private $operator;
	private $num_two;

	public function __construct ($num_one,$operator='*',$num_two=1) {
		$this->num_one = $num_one;

		if (is_string($operator)) {
			$this->operator = $operator;
			$this->num_two = $num_two;
		} else if (is_numeric($operator)&&$num_two=1) {
			// allow for new Number (5,6) to equal new Number (5,'*',6)
			$this->operator = '*';
			$this->num_two = $num_two;
		} else {
			trigger_error('Number->__construct() called with invalid arguments.');
		}
	}

	// PUBLIC //
	public function toFloat () {
		return (float) $this->calculate_value();
	}

	public function toInt () {
		return (int) $this->calculate_value();
	}

	public function get_num_one () {
		return $num_one;
	}

	public function get_num_two () {
		return $num_two;
	}

	public function get_simplified_string () {
		$str = '';

		if ($this->is_expression()) {
			$str .= '(';		
			$str .= $this->make_Number($this->num_one)->get_simplified_string();
			$str .= $this->get_operator();
			$str .= $this->make_Number($this->num_two)->get_simplified_string();
			$str .= ')';
		} else if ($this->make_Number($this->num_one)->is_expression()) {
			$str .= $this->num_one->get_simplified_string();
		} else {
			$str .= $this->get_symbol($this->make_float($this->num_one));
		}
		
		return $str;
	}

	public function get_operator () {
		// Always use '*' for multiplication
		switch ($this->operator) {
			case '*':
			case 'x':
			case 'X':
				return '*';
			default:
				return $this->operator;
		}
	}

	public function multiply ($number) {
		$this->num_two = new Number ($this->num_one,$this->get_operator(),$this->num_two);
		$this->num_one = $number;
		$this->operator = '*';
	}

	public function divide ($number) {
		$this->num_one = new Number ($this->num_one,$this->get_operator(),$this->num_two);
		$this->num_two = $number;
		$this->operator = '/';
	}

	public function add ($number) {
		$this->num_two = new Number ($this->num_one,$this->get_operator(),$this->num_two);
		$this->num_one = $number;
		$this->operator = '+';
	}
	
	public function subtract ($number) {
		$this->num_one = new Number ($this->num_one,$this->get_operator(),$this->num_two);
		$this->num_two = $number;
		$this->operator = '-';
	}

	public function is_expression () {
		return !($this->operator=='*'&&$this->num_two==1);
	}
	
	// PRIVATE //
	private function calculate_value () {
		switch ($this->operator) {
			case '*':
			case 'x':
			case 'X':
				$val = $this->make_float($this->num_one) * $this->make_float($this->num_two);
				break;
			case '/':
				$val = $this->make_float($this->num_one) / $this->make_float($this->num_two);
				break;
			case '+':
				$val = $this->make_float($this->num_one) + $this->make_float($this->num_two);
				break;
			case '-':
				$val = $this->make_float($this->num_one) - $this->make_float($this->num_two);
				break;
			case '^':
				$val = $this->make_float($this->num_one) ^ $this->make_float($this->num_two);
				break;
			default: // Use multiplication
				$val = $this->make_float($this->num_one) * $this->make_float($this->num_two);
				break;
		}

		return $val;
	}

	private function make_float ($number) {
		if ($number instanceof Number) {
			return $number->toFloat();
		} else {
			return (float) $number;
		}
	}

	private function make_Number ($number) {
		return ($number instanceof Number) ? $number : new Number($number);
	}

	private function get_symbol ($number) {
		if ($number == M_PI) {
			return 'PI';
		} else {
			return $number;
		}
	}
}

?>

You can use it like this:

<?php
$num = new Number (new Number(5,'*',M_PI),'+',new Number(2,'-',43.6));
echo $num->toFloat().'<br \>'.$num->get_simplified_string();

echo '<br />';
echo '<br />';

$num->subtract(8);
echo $num->toFloat().'<br \>'.$num->get_simplified_string();
?>

I’ll add sqrts, cube_roots, etc, but anything else I might add? I’m only in Geometry, so I’m not that high, but this should be really useful.

Thanks. :slight_smile: