Source for file ScientificDouble.php

Documentation is available at ScientificDouble.php

  1. <?php
  2.  
  3. # Copyright (c) 2002-2005 Cunningham & Cunningham, Inc.
  4. # Released under the terms of the GNU General Public License version 2 or later.
  5. #
  6. # PHP5 translation by Luis A. Floreani <luis.floreani@gmail.com>
  7.  
  8. include 'PHPFIT/Comparable.php';
  9.  
  10. class PHPFIT_ScientificDouble implements PHPFIT_Comparable {
  11. protected $value = 0.0;
  12. protected $precsion = 0.0;
  13. function __construct($value) {
  14. $this->value = $value;
  15. }
  16. /**
  17. * @param object o
  18. * @return boolean
  19. */
  20. function equals($o) {
  21. return $this->compareTo($o) == 0;
  22. }
  23.  
  24. /**
  25. * look at interface Comparable
  26. */
  27. public function compareTo($other) {
  28. $other = floatval($other);
  29. $diff = $this->value - $other;
  30. if ($diff < -$this->precision) return -1;
  31. if ($diff > $this->precision) return 1;
  32. return 0;
  33. }
  34. /**
  35. * @param string s
  36. * @return ScientificDouble
  37. */
  38. public static function valueOf($s) {
  39. $result = new PHPFIT_ScientificDouble(floatval($s));
  40. $result->precision = self::precision($s);
  41. return $result;
  42. }
  43. /**
  44. * @param string s
  45. * @return double
  46. */
  47. public static function precision($s) {
  48. $value = floatval($s);
  49. $bound = floatval(self::tweak($s));
  50. return abs($bound - $value);
  51. }
  52.  
  53.  
  54. /**
  55. * @param string s
  56. * @return string
  57. */
  58. public static function tweak($s) {
  59. $pos = strpos(strtolower($s), 'e');
  60. if ($pos !== false) {
  61. $start = substr($s, 0, $pos);
  62. $end = substr($s, $pos);
  63. return self::tweak($start) . $end;
  64. }
  65. if (strpos($s, '.') !== false)
  66. return $s . "5";
  67. return $s . ".5";
  68. }
  69. public function toString() {
  70. return strval( $this->value );
  71. }
  72. }
  73. ?>

Documentation generated on Sun, 02 Apr 2006 23:20:52 +0000 by phpDocumentor 1.3.0RC5