Skip to content

Commit

Permalink
remove hard coded tolerance levels for calls to RationalTypeFactory::…
Browse files Browse the repository at this point in the history
…fromFloat()
  • Loading branch information
akitson-fu committed Sep 3, 2014
1 parent c181021 commit f624eaf
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
9 changes: 7 additions & 2 deletions README.md
Expand Up @@ -136,6 +136,10 @@ this, but using it directly may give you finer grain control in some circumstanc
$r = RationalTypeFactory::fromString('2/3');
</pre>

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)
Expand Down Expand Up @@ -224,7 +228,7 @@ Install [Composer](https://getcomposer.org/)
add

<pre>
"chippyash/strong-type": ">=1.1.0"
"chippyash/strong-type": "~1.1.1"
</pre>

to your composer.json "requires" section
Expand Down Expand Up @@ -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
move interfaces to separate folder
V1.1.1 Remove hard coded tolerance levels to fromFloat. Use default 1e-15 instead.
4 changes: 2 additions & 2 deletions src/chippyash/Type/Number/Complex/ComplexType.php
Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions src/chippyash/Type/Number/Complex/ComplexTypeFactory.php
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/chippyash/Type/Number/FloatType.php
Expand Up @@ -55,7 +55,7 @@ public function asComplex()
*/
public function asRational()
{
return RationalTypeFactory::fromFloat($this->value, 1E-17);
return RationalTypeFactory::fromFloat($this->value);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions test/src/chippyash/Type/Number/Complex/ComplexTypeTest.php
Expand Up @@ -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],
];
}

Expand Down

0 comments on commit f624eaf

Please sign in to comment.