PDA

View Full Version : Strange math handling: var*var != var^2



Felixz
December 7th, 2007, 08:42 PM
It's a question concernig that:
http://www.kirupa.com/forum/showthread.php?t=282936

var date=new Date(2007,11,1,12);
trace((date.getHours() * date.getHours()) / 144 - date.getHours() / 6 + .75);
trace((date.getHours() ^ 2) / 144 - date.getHours() / 6 + .75);
Gives:

-0.25
-1.1527777777777777
First is the right one, but why it is so different?

Gathan
December 7th, 2007, 11:03 PM
In actionscript the ^ is the bitwise xor operator.
Not to be confused with raising a number to a power which you are trying to do with it,
instead you have to use Math.pow to a raise a number to a power like


trace((Math.pow(date.getHours(), 2)) / 144 - date.getHours() / 6 + .75);

Though using Math.pow can get a bit too expensive depending on how much your actually use it. In retrospect doing date.getHours()*date.getHours() is actually faster.

Felixz
December 7th, 2007, 11:10 PM
Ah, thanx for enlightment