From a4f3027e7e01c5bdce8790bc92f1c1b6d47b780f Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Sat, 7 Feb 2015 15:54:22 +1100 Subject: [PATCH] Fix parentheses mismatch error in zero-denominator check when multiple divisions close #119 --- includes/sp-core-functions.php | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/includes/sp-core-functions.php b/includes/sp-core-functions.php index ab312b00..186432fe 100644 --- a/includes/sp-core-functions.php +++ b/includes/sp-core-functions.php @@ -1085,12 +1085,22 @@ if ( !function_exists( 'sp_solve' ) ) { // Remove space between equation parts $equation = str_replace( ' ', '', $equation ); - // Check if denominator is zero - $pos = strpos( $equation, '/' ); - if ( $pos ): - if ( $eos->solveIF( substr( $equation, $pos + 1 ), $vars ) == 0 ) - return 0; - endif; + // Initialize subequation + $subequation = $equation; + + // Check each subequation separated by division + while ( $pos = strpos( $subequation, '/' ) ) { + $subequation = substr( $subequation, $pos + 1 ); + + // Make sure paretheses match + if ( substr_count( $subequation, '(' ) === substr_count( $subequation, ')' ) ) { + + // Return zero if denominator is zero + if ( $eos->solveIF( $subequation, $vars ) == 0 ) { + return 0; + } + } + } // Return solution return number_format( $eos->solveIF( str_replace( ' ', '', $equation ), $vars ), $precision, '.', '' );