From 80e1738a8474b9761036b73a4cc0180e6cee080c Mon Sep 17 00:00:00 2001 From: Kore Nordmann Date: Wed, 24 Jan 2007 12:22:57 +0000 Subject: - Fixed issue #9950: Improved ezcGraphPolynom::__toString method for more exact output # Based on a patch by Matthew Carroll --- src/math/polynom.php | 54 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 6 deletions(-) (limited to 'src') 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 ); } } ?> -- cgit v1.1