elemelas
elemelas

Reputation: 143

PHP Division results in automatically rounded up number

I have a simple division:

72 / 193

Excel is giving me result of

0.373056994818653

While PHP round it up to

0.373056994819

My question is - how can I make PHP to give me the exact number instead of a rounded up one?

Upvotes: 0

Views: 1989

Answers (2)

scrblnrd3
scrblnrd3

Reputation: 7416

The exact number for that is an infinite decimal. You can never get a complete absolute value for that, because that would require infinite memory, something that we sadly do not have access to.

Take a look at bcdiv, which allows for very long ints and floating point numbers

For all practical intents and purposes, however, if two numbers are within 1*10-8 or so, you should be able to consider them equal

You can do this like this:

if(abs($num1-$num2)<=1e-8){
    echo "They are equal";
}

Upvotes: 1

afuzzyllama
afuzzyllama

Reputation: 6548

It isn't a question of being 'exact' or not, it is about precision. Wolfram Alpha has a much larger precision for your answer.

You need to change the precision of your php configuration to suit how many units you need after the decimal. I cannot find the largest precision PHP has to offer, but I'm sure there is a limit.

Upvotes: 1

Related Questions