Weird math problem, PHP thinks a calculation is a String |
[eluser]Fabdrol[/eluser]
Hi guys, I've got a problem with PHP. Some how, I keep getting an error with this code: Code: $ANS = ($L1 - (( 1.0 ‐ $G1 ) * ( $L1 + $M1 + $A1 + 1.0 ))) / ( $G1 * (2.0 * $K1 + $M1 )); The error reads: Code: Parse error: syntax error, unexpected T_STRING Which is weird, since it's not a String, neither is one of the variables. To be sure I've added + 0.0 to each variable, to make sure they're floats. Does anybody know what to do with this? Fabian
[eluser]mdpauley[/eluser]
Can you provide example data for the formula so I can try it?
[eluser]mdpauley[/eluser]
I just tried the forumla with some fake data and it ran fine for me on 5.2.13.
[eluser]diego6616[/eluser]
Be aware that PHP it's saying it can't PARSE the file. It means that somewere there is a syntax error. Check your code ahead and behind the formula, since the formula with fake values apparently run prefectly alone.
[eluser]Fabdrol[/eluser]
Yeah, I know. However, visually, there's no issue, so I figured it had to be something else.. In a bright moment I decided to rewrite the formula, and that turned out to do the trick. Apparently there was something gone wrong when copying it from the Excel file I used to design the formula..
[eluser]intractve[/eluser]
May not be applicable here, but just putting this here so people looking for it may find it. PHP generally fails at basic math using regular operands like + - etc. with floating point numbers (1.222 + 1.435) etc. For Eg: Code: var_dump(0.7 + 0.1 == 0.8); //outputs bool(false) Excerpt from php.net Quote:Floating point numbers have limited precision. Although it depends on the system, PHP typically uses the IEEE 754 double precision format, which will give a maximum relative error due to rounding in the order of 1.11e-16. Non elementary arithmetic operations may give larger errors, and, of course, error progragation must be considered when several operations are compounded. Solution is to use bcmath - a precision math extension installed in most php installations. Hope this is useful to someone.
[eluser]Fabdrol[/eluser]
Thanks George. However, for most applications, the 'default' precision is okay, you just don't compare them. In stead, define a 'ceil' value and a 'floor' value, and check if the outcome is between them. Of course, if you need the precision, you could use the precision math functions.
[eluser]Eric Cope[/eluser]
Usually when I get that error, I missed a $ for a variable. Had one of your variables been a string, it would have converted it to 0.0 and proceeded with the math. |
Welcome Guest, Not a member yet? Register Sign In |