Forum Moderators: coopster

Message Too Old, No Replies

Math errors in PHP5.2?

Upgraded to PHP5 and now my scripts can't add...

         

erikcw

4:19 pm on Jan 17, 2007 (gmt 0)

10+ Year Member



Hi all,

I upgraded my server over the weekend from php4.4 to php5.2. Since then, I've had a heck of a time getting math formulas to work correctly.

Are there any known changes to how php5 handles arithmetic operations?

Thanks!
Erik

coopster

4:37 pm on Jan 17, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



None of which I am aware. Can you offer an example?

barns101

6:29 pm on Jan 17, 2007 (gmt 0)

10+ Year Member



Any maths errors that I have ever encountered (not specifically with PHP5) have been because I wasn't using number_format() [php.net] correctly.

mcavic

6:38 pm on Jan 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I seem to recall having some floating point errors in PHP not too long ago. I don't remember what, but I solved it by rounding.

At first glance, I don't see anything here [php.net] that looks like a problem.

erikcw

2:44 pm on Jan 18, 2007 (gmt 0)

10+ Year Member



Edit: double checking code before I post...

erikcw

4:04 pm on Jan 18, 2007 (gmt 0)

10+ Year Member



Well, I found my problem.

I was try to use "^" for exponents (ie 4^2 instead of pow(4,2))

I could have sworn that ^ was the exponent operator in php...

mcavic

4:22 pm on Jan 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm, the ^ works for me in PHP 5.2.0.

[Oops, no it doesn't. It runs, but 4^2 gives 6].

coopster

12:43 pm on Jan 22, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



That is because the caret is a Bitwise Operator [php.net]. Don't worry, you aren't the first person to make this incorrect assumption! Here are some more helpful links:
Arithmetic Operators [php.net]
Mathematical Functions [php.net]
BCMath Arbitrary Precision Mathematics Functions [php.net]