Jump to content

Dealing with invalid float numbers


Recommended Posts

Hello once again,I'm trying to come up with a way to deal with invalid float numbers. Specifically, the ones you get when you try to perform an operation that exceeds a floating-point numbers precision, like dividing one by 19000. I end up with a number that looks like this:

When I encounter this number, I just want to set it to zero - no reason for something like that to spam up my file, lol. However, code like this doesn't work:
if(is_nan($percent)  $percent = 0;

Neither does this:

if(is_infinite($percent)  $percent = 0;

Or this:

if(!is_finite($percent)  $percent = 0;

Is there any way I can conveniently detect and remove these numbers?

Link to comment
Share on other sites

None of those work, because the number is a number, it's not infinite, and it is finite. That number is shorthand for this:0.0000484730974309You are dealing with very small numbers, but they are still finite numbers. So, you can set a threshold. If the number is smaller then the threshold, you can set it to zero.

$threshold = 0.0001;if ($percent < $threshold)  $percent = 0;

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...