views:

646

answers:

4

I have the following equation

1 - ((.5 * 0.83333333333333) ^ 2 + (.5 * 0.83333333333333) ^ 2 + (.5 * (1 - 0.83333333333333)) ^ 2 + (.5 * (1 - 0.83333333333333)) ^ 2)

In Php5, this results in an answer of 1 as opposed to .63 (on two machines, OSx and Centos). Should I be exclusively using the bc math functions of Php to do equations like this?

+4  A: 

I think maybe you should be using pow() instead of the xor operator (^) :)

Greg
A: 

Shoot me now. Please. :)

Thanks!

+1  A: 

Not really an equation, but thats semantics. also, I doubt you mean xor, so I'll assume that isn't what you want. Anyway, can you use rational arithmetic?

0.83333 can be converted to a fraction (assuming the 3 is a repeating decimal):

 83.3333333 = 100x
  8.3333333 = 10x
 -----------------
         75 = 90x
     x = 75 / 90 = 0.83333...

That way you are dealing with integers only, and as long as both don't overflow (you can reduce by GCD prior to and after operations) then you should be fine.

nlucaroni
+1  A: 
<?php

$hugeDamnEquation = pow(1 - ((.5 * 0.83333333333333), 2) + pow((.5 * 0.83333333333333), 2) + pow((.5 * (1 - 0.83333333333333)), 2) + pow((.5 * (1 - 0.83333333333333)), 2));

echo $hugeDamnEquation;

?>
John T