Novocaine
Novocaine

Reputation: 4786

php operator precedence

I'm having trouble understanding how php calculates standard math functions. In a specific example I have this calculation:

225 + 154 * 256 + 138 * 256 * 256 + 81 * 256 * 256 * 256 (thats correct, no brackets)

which when executed with php produces this number: 1,368,038,113

Now when I look at this logically and work through the sum from left to right, this number doesn't even come close. using a cheap simple calculator, it gives up trying to calculate it before the last two multiplications of 256 because the number gets too big.

How is it possible to end up with such a relatively small number from a calculation with 6 multiplications by 256?

A breakdown of how php actually would work out this answer would be great.

p.s. i read through this page: http://www.homeandlearn.co.uk/php/php2p8.html which still didn't help me with the above.

Upvotes: 1

Views: 2887

Answers (5)

Mark Baker
Mark Baker

Reputation: 212412

225 + 154 * 256 + 138 * 256 * 256 + 81 * 256 * 256 * 256

=>

225 
+ 
154 * 256 = 39,424
+ 
138 * 256 * 256 = 9,043,968
+ 
81 * 256 * 256 * 256 = 1,358,954,496

=>

225 + 39,424 + 9,043,968 + 1,358,954,496

=>

225 
+ 
39,424 
+ 
9,043,968 
+ 
1,358,954,496

=>

1,368,038,113

EDIT

Why do I think you're doing:

225 + 154 * 256 + 138 * 256 * 256 + 81 * 256 * 256 * 256

=>

225 + 154 = 379
* 
256 = 97,024
+ 
138 = 97,162
* 
256 = 24,873,472
* 
256 = 6,367,608,832
+ 
81 = 6,367,608,913
* 
256 = 1,630,107,881,728
* 
256 = 417,307,617,722,368
* 
256 = 106,830,750,136,926,208

Upvotes: 6

flowfree
flowfree

Reputation: 16462

The result is correct. PHP will perform the multiplication first and then the addition. It looks like this:

$res = 225 + (154 * 256) + (138 * 256 * 256) + (81 * 256 * 256 * 256)
$res = 225 + 39424       + 9043968           + 1358954496
$res = 1368038113

Upvotes: 7

Kyborek
Kyborek

Reputation: 1511

as usual multiplying has precedence over adding. I have counted the equation and its correct so i dont see what is your problem...?

"this number doesnt even come close" <== thats right, because its exact:

225 + 154 * 256 + 138 * 256 * 256 + 81 * 256 * 256 * 256
=225 + 39424 + 9 043 968 +1 358 954 496
=1 368 038 113

What number did you expect? how did you count it?

Upvotes: 1

Nick
Nick

Reputation: 6346

PHP follows the rules of mathematics:

BODMAS (Brackets, Orders, Division, Multiplication, Add, Subtract)

So in your case it would evaluate like this:

225 + 154 * 256 + 138 * 256 * 256 + 81 * 256 * 256 * 256
225 + 39424 + 9043968 + 1358954496
1368038113

If you want to do it in a different order, use brackets.

Upvotes: 2

kevin628
kevin628

Reputation: 3526

Basic order of operations from math is: multiplication/division then addition/subtraction.

Using a super simple calculator, it does operations one at a time. So a simple calculator would probably treat your input like this messy string:

(((((((((225 + 154) * 256) + 138) * 256) * 256) + 81) * 256) * 256) * 256).

Upvotes: 1

Related Questions