Page 1 of 1

Irrational numbers

Posted: Fri Aug 17, 2007 6:23 pm
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?

Posted: Fri Aug 17, 2007 6:32 pm
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.

Posted: Fri Aug 17, 2007 6:37 pm
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.

Posted: Fri Aug 17, 2007 6:49 pm
by Ollie Saunders

Posted: Fri Aug 17, 2007 7:44 pm
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.

Posted: Fri Aug 17, 2007 9:14 pm
by afbase
thanks ole and feyd

Posted: Sat Aug 18, 2007 8:52 am
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.

Posted: Sun Aug 19, 2007 9:47 am
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.

Posted: Sun Aug 19, 2007 9:53 am
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?

Posted: Sun Aug 19, 2007 9:55 am
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.

Posted: Sun Aug 19, 2007 10:00 am
by VladSun
Ambush Commander wrote:In symbolic math systems such as Mathematica
That was exactly what I had in mind :)

Posted: Sun Aug 19, 2007 10:31 am
by Ollie Saunders
Ruby does it, I think.