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. class ScientificDouble implements Comparable {
  9. protected $value = 0.0;
  10. protected $precsion = 0.0;
  11. function __construct($value) {
  12. $this->value = $value;
  13. }
  14. /**
  15. * @param object o
  16. * @return boolean
  17. */
  18. function equals($o) {
  19. return $this->compareTo($o) == 0;
  20. }
  21.  
  22. /**
  23. * look at interface Comparable
  24. */
  25. public function compareTo($other) {
  26. $other = floatval($other);
  27. $diff = $this->value - $other;
  28. if ($diff < -$this->precision) return -1;
  29. if ($diff > $this->precision) return 1;
  30. return 0;
  31. }
  32. /**
  33. * @param string s
  34. * @return ScientificDouble
  35. */
  36. public static function valueOf($s) {
  37. $result = new ScientificDouble(floatval($s));
  38. $result->precision = self::precision($s);
  39. return $result;
  40. }
  41. /**
  42. * @param string s
  43. * @return double
  44. */
  45. public static function precision($s) {
  46. $value = floatval($s);
  47. $bound = floatval(self::tweak($s));
  48. return abs($bound - $value);
  49. }
  50.  
  51.  
  52. /**
  53. * @param string s
  54. * @return string
  55. */
  56. public static function tweak($s) {
  57. $pos = strpos(strtolower($s), 'e');
  58. if ($pos !== false) {
  59. $start = substr($s, 0, $pos);
  60. $end = substr($s, $pos);
  61. return self::tweak($start) . $end;
  62. }
  63. if (strpos($s, '.') !== false)
  64. return $s . "5";
  65. return $s . ".5";
  66. }
  67. public function toString() {
  68. return strval($this->value);
  69. }
  70.  
  71. }
  72.  
  73.  
  74. interface Comparable {
  75.  
  76.  
  77. /**
  78. * @param object other
  79. * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
  80. */
  81. public function compareTo($other);
  82. }
  83.  
  84. ?>

Documentation generated on Sun, 02 Apr 2006 16:01:06 +0200 by phpDocumentor 1.3.0RC5