1

I have a random float number and I have to determine if it is an irrational number like √2 or a fraction like 123/321. Both of them are represented like an endless set of numbers anywhere but is there any way to definitely say whether a number is a fraction or it's irrational?

Thank you!

mskfisher
  • 3,291
  • 4
  • 35
  • 48
Knodel
  • 4,359
  • 8
  • 42
  • 66

2 Answers2

6

All floating point numbers are rational because the mantissa has a fixed length. Irrational numbers stored in floating point are truncated into rational numbers.

If you have a specific list of numbers you need to match, you can compare the random number to numbers on the list at a set floating point precision, but do keep in mind that you will get false positives due to truncation or rounding.

Leo
  • 1,493
  • 14
  • 27
  • Unfortunately, I don't have the list of numbers - they are random – Knodel Feb 23 '11 at 19:35
  • 1
    You can create a list of floating point representations of some interesting irrational numbers (√2, √3, √5, ...) and then compare your random number to the numbers on the list. More generally, the question breaks down because for every terminating rational number with a fixed length mantissa, you can find a non-terminating irrational number that truncates to it. – Leo Feb 23 '11 at 19:41
  • Of course the list won't precisely because there are an infinite number of non-irrational numbers which are arbitrarily close to each irrational number. Thus, the chances of mislabeling are quite high (infinite, even). – Richard Jun 01 '12 at 20:45
2

All (finite, non-NaN) floating point values are rational, because all finite (binary) floating-point numbers are of the form f*2^e for integers f and e.

Stephen Canon
  • 103,815
  • 19
  • 183
  • 269