Page 1 of 1
Floating Point Problem
Posted: Tue Jun 24, 2008 7:34 am
by agentcris
Hi all,
The problems that I am facing are:
1.) If the number is too big, PHP resorts to scientific notation. FE: 1E+15. How do I get rid of this and make it display the number in full?
2.) Again if the number is too big like 3434534543530000, the Number to URL conversion works fine, but the URL to number conversion for various numbers in that range produces the same result i.e. 3434534543530000. This is the case in the ranges of 3434534543520000,3434534543510000 and so on.
How do I come across these problems?
Help would be appreciated a lot

Thank you,
The PHP Noobe

Re: Floating Point Problem
Posted: Tue Jun 24, 2008 9:08 am
by Kieran Huggins
I think the problem is that it becomes a float when the number is higher than PHP's 32-bit limit. You could compile PHP and run it on a 64-bit machine, but 32 bits is probably big enough for your purposes.
In fact, you could probably just use base_convert() to convert the database id of the record from decimal to base32, 48 or 64. all should be valid for URLs, I think. Just as long as there isn't a "?" in the set you should be fine.
Re: Floating Point Problem
Posted: Tue Jun 24, 2008 9:17 am
by agentcris
Yeah,
I know I wouldn't be reaching this high a number in any practical case.
Just on an academical note, There must be a work-around for this right? I have been breaking my head on this.
In fact, you could probably just use base_convert() to convert the database id of the record from decimal to base32, 48 or 64. all should be valid for URLs, I think. Just as long as there isn't a "?" in the set you should be fine.
The thing is I am using an arbitrary encoding to convert from base10 to base62 as you can see here
viewtopic.php?f=1&t=84056
Any ideas for a work-around? Purely of academic interests.
Thanks,
PHP Noobe

Re: Floating Point Problem
Posted: Tue Jun 24, 2008 9:29 am
by Kieran Huggins
Academically, there is certainly a solution. It's sure to be extraordinarily math heavy, though.
Practically, you could just upgrade to a 64-bit system before it (eventually) becomes an issue

Re: Floating Point Problem
Posted: Tue Jun 24, 2008 11:38 am
by agentcris
Alright,
Is there a way by which I can avoid the exponential notation atleast?
FE: I want to avoid PHP displaying 1.0E12 for 1 billion.. How do I achieve that? I have been trying to do this as well, but no hope.
Re: Floating Point Problem
Posted: Tue Jun 24, 2008 12:33 pm
by Kieran Huggins
Re: Floating Point Problem
Posted: Tue Jun 24, 2008 12:35 pm
by Benjamin
The number_format function can render the number as well.
Re: Floating Point Problem
Posted: Wed Jun 25, 2008 4:06 pm
by agentcris
Alright,
A variant of number_format() fixed the problem
The new code is here: