Floating Point Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
agentcris
Forum Newbie
Posts: 12
Joined: Thu Jun 12, 2008 8:27 am

Floating Point Problem

Post 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 :)
Last edited by agentcris on Wed Jun 25, 2008 4:10 pm, edited 2 times in total.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Floating Point Problem

Post 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.
agentcris
Forum Newbie
Posts: 12
Joined: Thu Jun 12, 2008 8:27 am

Re: Floating Point Problem

Post 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 :)
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Floating Point Problem

Post 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 ;-)
agentcris
Forum Newbie
Posts: 12
Joined: Thu Jun 12, 2008 8:27 am

Re: Floating Point Problem

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Floating Point Problem

Post by Kieran Huggins »

User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Floating Point Problem

Post by Benjamin »

The number_format function can render the number as well.
agentcris
Forum Newbie
Posts: 12
Joined: Thu Jun 12, 2008 8:27 am

Re: Floating Point Problem

Post by agentcris »

Alright,

A variant of number_format() fixed the problem

The new code is here:

Code: Select all

 
    $no=number_format($no,0,".","");
 
Post Reply