3

We can write a simple Rational Number class using two integers representing A/B with B != 0.

If we want to represent an irrational number class (storing and computing), the first thing came to my mind is to use floating point, which means use IEEE 754 standard (binary fraction). This is because irrational number must be approximated.

Is there another way to write irrational number class other than using binary fraction (whether they conserve memory space or not) ?

I studied jsbeuno's solution using Python: Irrational number representation in any programming language?

He's still using the built-in floating point to store.

This is not homework.

Thank you for your time.

Community
  • 1
  • 1
CppLearner
  • 16,273
  • 32
  • 108
  • 163
  • You can use a power of 2 for `B` to represent any IEEE floating point number. – Mark Ransom Dec 20 '11 at 18:39
  • But isn't that already 754 (even if we store A as integer for crazy reason...)? Since the decimal part will be converted to power of 2 (negative) ? – CppLearner Dec 20 '11 at 18:46
  • Once you have them represented in memory, what are you planning to do with these numbers? Choosing the right solution depends almost entirely on your answer to this question. – Sergey Kalinichenko Dec 20 '11 at 18:52

3 Answers3

0

You can always use symbolic math, where items are stored exactly as they are and calculations are deferred until they can be performed with precision above some threshold.

For example, say you performed two operations on a non-irrational number like 2, one to take the square root and then one to square that. With limited precision, you may get something like:

  (√2)²
= 1.414213562²
= 1.999999999

However, storing symbolic math would allow you to store the result of √2 as √2 rather than an approximation of it, then realise that (√x)² is equivalent to x, removing the possibility of error.

Now that obviously involves a more complicated encoding that simple IEEE754 but it's not impossible to achieve.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

With a cardinality argument, there are much more irrational numbers than rational ones. (and the number of IEEE754 floating point numbers is finite, probably less than 2^64).

You can represent numbers with something else than fractions (e.g. logarithmically).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • But among those irrational numbers, only a countable infinity is computable (sounds like tautological), so it ain't gonna be a problem worse than rational. – aka.nice Jul 01 '12 at 21:05
0

jsbeuno is storing the number as a base and a radix and using those when doing calcs with other irrational numbers; he's only using the float representation for output.

If you want to get fancier, you can define the base and the radix as rational numbers (with two integers) as described above, or make them themselves irrational numbers.

To make something thoroughly useful, though, you'll end up replicating a symbolic math package.

antlersoft
  • 14,636
  • 4
  • 35
  • 55