Documentation is available at ScientificDouble.php
- <?php
- # Copyright (c) 2002-2005 Cunningham & Cunningham, Inc.
- # Released under the terms of the GNU General Public License version 2 or later.
- #
- # PHP5 translation by Luis A. Floreani <luis.floreani@gmail.com>
- class ScientificDouble implements Comparable {
- protected $value = 0.0;
- protected $precsion = 0.0;
- function __construct($value) {
- $this->value = $value;
- }
- /**
- * @param object o
- * @return boolean
- */
- function equals($o) {
- return $this->compareTo($o) == 0;
- }
- /**
- * look at interface Comparable
- */
- public function compareTo($other) {
- $other = floatval($other);
- $diff = $this->value - $other;
- if ($diff < -$this->precision) return -1;
- if ($diff > $this->precision) return 1;
- return 0;
- }
- /**
- * @param string s
- * @return ScientificDouble
- */
- public static function valueOf($s) {
- $result = new ScientificDouble(floatval($s));
- $result->precision = self::precision($s);
- return $result;
- }
- /**
- * @param string s
- * @return double
- */
- public static function precision($s) {
- $value = floatval($s);
- $bound = floatval(self::tweak($s));
- return abs($bound - $value);
- }
- /**
- * @param string s
- * @return string
- */
- public static function tweak($s) {
- $pos = strpos(strtolower($s), 'e');
- if ($pos !== false) {
- $start = substr($s, 0, $pos);
- $end = substr($s, $pos);
- return self::tweak($start) . $end;
- }
- if (strpos($s, '.') !== false)
- return $s . "5";
- return $s . ".5";
- }
- public function toString() {
- return strval($this->value);
- }
- }
- interface Comparable {
- /**
- * @param object other
- * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
- */
- public function compareTo($other);
- }
- ?>
Documentation generated on Sun, 02 Apr 2006 16:01:06 +0200 by phpDocumentor 1.3.0RC5