summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKore Nordmann <github@kore-nordmann.de>2007-01-24 12:22:57 +0000
committerKore Nordmann <github@kore-nordmann.de>2007-01-24 12:22:57 +0000
commit80e1738a8474b9761036b73a4cc0180e6cee080c (patch)
tree97748bfef9deed057c42504a9056f60fbd1a5757 /src
parent937d04f2094624af45c0241bc75f3cbef4182c70 (diff)
downloadzetacomponents-graph-80e1738a8474b9761036b73a4cc0180e6cee080c.zip
zetacomponents-graph-80e1738a8474b9761036b73a4cc0180e6cee080c.tar.gz
- Fixed issue #9950: Improved ezcGraphPolynom::__toString method for more
exact output # Based on a patch by Matthew Carroll
Diffstat (limited to 'src')
-rw-r--r--src/math/polynom.php54
1 files changed, 48 insertions, 6 deletions
diff --git a/src/math/polynom.php b/src/math/polynom.php
index bf9ca49..a824910 100644
--- a/src/math/polynom.php
+++ b/src/math/polynom.php
@@ -16,6 +16,8 @@ class ezcGraphPolynom
{
protected $values;
+ // @TODO: Introduce precision option for string output?
+
/**
* Constructor
*
@@ -156,17 +158,57 @@ class ezcGraphPolynom
{
continue;
}
- elseif ( $factor != 1 )
+
+ $string .= ( $factor < 0 ? ' - ' : ' + ' );
+
+ $factor = abs( $factor );
+ switch ( true )
{
- $string .= sprintf( '%.2f * ', $factor );
+ case abs( 1 - $factor ) < .0001:
+ // No not append, if factor is ~1
+ break;
+ case $factor < 1:
+ case $factor >= 1000:
+ $string .= sprintf( '%.3e ', $factor );
+ break;
+ case $factor >= 100:
+ $string .= sprintf( '%.0f ', $factor );
+ break;
+ case $factor >= 10:
+ $string .= sprintf( '%.1f ', $factor );
+ break;
+ default:
+ $string .= sprintf( '%.2f ', $factor );
+ break;
}
- $string .= ( $exponent > 1 ? sprintf( 'x^%d + ', $exponent ) :
- ( $exponent === 1 ? 'x + ' : '' )
- );
+ switch ( true )
+ {
+ case $exponent > 1:
+ $string .= sprintf( 'x^%d', $exponent );
+ break;
+ case $exponent === 1:
+ $string .= 'x';
+ break;
+ case $exponent === 0:
+ if ( abs( 1 - $factor ) < .0001 )
+ {
+ $string .= '1';
+ }
+ break;
+ }
+ }
+
+ if ( substr( $string, 0, 3 ) === ' + ' )
+ {
+ $string = substr( $string, 3 );
+ }
+ else
+ {
+ $string = '-' . substr( $string, 3 );
}
- return substr( $string, 0, -3 );
+ return trim( $string );
}
}
?>
OpenPOWER on IntegriCloud