Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

float multiplication

Status
Not open for further replies.

kafine

Programmer
Aug 17, 2000
3
US
hi

i've seen this problem when i took a java class:

$x = 1.8;
$y = 1.2;
if($x>$y*1.5) exit();

the above code will exit even though $x = $y*1.5
the difference between the two is : 2.2204460492503E-16
does anyone know why this happens? and more importantly,
any ideas on how to deal with this?
thanks!
keith
 
yeah, sorry. this just happened in php3. I just meant that I remember having the same problem in Java and wasn't too sure how to deal with it then. the big problem is that I have these checks all over my production site. I guess the only thing to do is to round the variable unless you can think of a better solution.
But it seems like whenever you multiply a float by another float, the answer is not precise. In my case the difference was trivial (like 2E-16) but that's enough to return true in the above if statement.
thanks,
keith
 
What do you get if you set the output of the calculation to a variable and print it?

I had a word with one of the c programmers and they said that it was possible that the numbers were just too large and the variables couldn't hold so many decimal places.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top