From f624eaf0d66dc8a4266aa5cf1df46112f9c3ac70 Mon Sep 17 00:00:00 2001 From: Ashley Kitson Date: Wed, 3 Sep 2014 08:17:08 +0100 Subject: [PATCH] remove hard coded tolerance levels for calls to RationalTypeFactory::fromFloat() --- README.md | 9 +++++++-- src/chippyash/Type/Number/Complex/ComplexType.php | 4 ++-- src/chippyash/Type/Number/Complex/ComplexTypeFactory.php | 4 ++-- src/chippyash/Type/Number/FloatType.php | 2 +- .../chippyash/Type/Number/Complex/ComplexTypeTest.php | 8 ++++---- 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index ac9fccd..fc342a2 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,10 @@ this, but using it directly may give you finer grain control in some circumstanc $r = RationalTypeFactory::fromString('2/3'); +NB. the RationalTypeFactory::fromFloat() method will use a default tolerance level of 1e-15. As in the +example above, you can set a different level, although on a 64 bit system, 1e-17 is about +the limit. + All types support the TypeInterface: * get() - return the value as a PHP native type (if possible) @@ -224,7 +228,7 @@ Install [Composer](https://getcomposer.org/) add
-    "chippyash/strong-type": ">=1.1.0"
+    "chippyash/strong-type": "~1.1.1"
 
to your composer.json "requires" section @@ -282,4 +286,5 @@ V1.0.11 Ensure isolation of type parts in as... methods V1.1.0 Add Polar form complex number support - move interfaces to separate folder \ No newline at end of file + move interfaces to separate folder +V1.1.1 Remove hard coded tolerance levels to fromFloat. Use default 1e-15 instead. diff --git a/src/chippyash/Type/Number/Complex/ComplexType.php b/src/chippyash/Type/Number/Complex/ComplexType.php index 57e9d46..abba6cb 100644 --- a/src/chippyash/Type/Number/Complex/ComplexType.php +++ b/src/chippyash/Type/Number/Complex/ComplexType.php @@ -125,8 +125,8 @@ public function modulus() // rN = RationaType(sqrt(num)) // rD = RationalType(sqrt(den)) // mod = rN/1 * 1/rD - $rN = RationalTypeFactory::fromFloat(sqrt($num), 1e-17); - $rD = RationalTypeFactory::fromFloat(sqrt($den), 1e-17); + $rN = RationalTypeFactory::fromFloat(sqrt($num)); + $rD = RationalTypeFactory::fromFloat(sqrt($den)); $modN = $rN->numerator()->get() * $rD->denominator()->get(); $modD = $rN->denominator()->get() * $rD->numerator()->get(); diff --git a/src/chippyash/Type/Number/Complex/ComplexTypeFactory.php b/src/chippyash/Type/Number/Complex/ComplexTypeFactory.php index b6a409e..05c33bb 100644 --- a/src/chippyash/Type/Number/Complex/ComplexTypeFactory.php +++ b/src/chippyash/Type/Number/Complex/ComplexTypeFactory.php @@ -135,11 +135,11 @@ protected static function convertType($t) return new RationalType(new IntType($t), new IntType(1)); } if (is_float($t)) { - return RationalTypeFactory::fromFloat($t, 1e-17); + return RationalTypeFactory::fromFloat($t); } } if ($t instanceof FloatType) { - return RationalTypeFactory::fromFloat($t(), 1e-17); + return RationalTypeFactory::fromFloat($t()); } if ($t instanceof IntType) { return new RationalType(new IntType($t()), new IntType(1)); diff --git a/src/chippyash/Type/Number/FloatType.php b/src/chippyash/Type/Number/FloatType.php index 2c7ce19..06b9725 100644 --- a/src/chippyash/Type/Number/FloatType.php +++ b/src/chippyash/Type/Number/FloatType.php @@ -55,7 +55,7 @@ public function asComplex() */ public function asRational() { - return RationalTypeFactory::fromFloat($this->value, 1E-17); + return RationalTypeFactory::fromFloat($this->value); } /** diff --git a/test/src/chippyash/Type/Number/Complex/ComplexTypeTest.php b/test/src/chippyash/Type/Number/Complex/ComplexTypeTest.php index cdd0ad4..5a7f3f3 100644 --- a/test/src/chippyash/Type/Number/Complex/ComplexTypeTest.php +++ b/test/src/chippyash/Type/Number/Complex/ComplexTypeTest.php @@ -491,13 +491,13 @@ public function polars() { return [ //quadrant 1 - [new ComplexType($this->createRationalType(5), $this->createRationalType(2)),'192119201/35675640','15238812/40048769', 1], + [new ComplexType($this->createRationalType(5), $this->createRationalType(2)),'73997555/13741001','15238812/40048769', 1], //quadrant 2 - [new ComplexType($this->createRationalType(-5), $this->createRationalType(2)),'192119201/35675640','266613702/96561163', 2], + [new ComplexType($this->createRationalType(-5), $this->createRationalType(2)),'73997555/13741001','266613702/96561163', 2], //quadrant 3 - [new ComplexType($this->createRationalType(-5), $this->createRationalType(-2)),'192119201/35675640','-266613702/96561163', 3], + [new ComplexType($this->createRationalType(-5), $this->createRationalType(-2)),'73997555/13741001','-266613702/96561163', 3], //quadrant 4 - [new ComplexType($this->createRationalType(5), $this->createRationalType(-2)),'192119201/35675640','-15238812/40048769', 4], + [new ComplexType($this->createRationalType(5), $this->createRationalType(-2)),'73997555/13741001','-15238812/40048769', 4], ]; }