Irrational numbers

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
afbase
Forum Contributor
Posts: 113
Joined: Tue Aug 15, 2006 1:29 pm
Location: SoCAL!!!!

Irrational numbers

Post by afbase »

is there a way to store irrational numbers in memory? I mean I don't want it saved as a double or a long, because the number simply gets rounded off eventually. So how does one overcome that?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

What do you mean by 'the number simply gets rounded off eventually?' Have you done any testing to that extent? PHP is not strict with typing, so I'd assume that it's generous with memory allocation.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Post by kaszu »

You can store irrational numbers by creating your own class for that, but PHP like most other languages doesn't support that (as i know), because how often do you need them?
I haven't had need for them yet, that would be an answer to why.

superDezign: with 'the number simply gets rounded off eventually' probably he means that numbers saved as floating number are limited to used memory (not sure how many PHP allows for floating numbers) for them and saving for example 10/3 is impossible without rounding them.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

superdezign wrote:What do you mean by 'the number simply gets rounded off eventually?' Have you done any testing to that extent? PHP is not strict with typing, so I'd assume that it's generous with memory allocation.
Not with integers and floating point. They have finite limits natively. As ~ole mentioned, bcmath is something to look at.
afbase
Forum Contributor
Posts: 113
Joined: Tue Aug 15, 2006 1:29 pm
Location: SoCAL!!!!

Post by afbase »

thanks ole and feyd
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

feyd wrote:Not with integers and floating point. They have finite limits natively. As ~ole mentioned, bcmath is something to look at.
Oh. Okay.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

BCMath also has finite precision. It just lets you specify how far you need to go. By definition it's impossible to write a decimal number that expresses all the digits of an irrational number.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

You may save the expression which result is the irrational number. But still can't figure out - how will you use them in fixed length environment - i.e. the PHP math operations?
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

In symbolic math systems such as Mathematica, the approach your mention, simply not attempting to evaluate the irrational number, is exactly what happens. "Pi + 1" stays "Pi + 1", but Sin[Pi] becomes 0. Such a system is, however, out of the scope of PHP.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Ambush Commander wrote:In symbolic math systems such as Mathematica
That was exactly what I had in mind :)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Ruby does it, I think.
Post Reply