How can I check a number for irrationality? We enter an irrational number and use only the standard std library.
-
6You need to wonder what's the definition of "irrational number" when the precision is limited. Your question is not accuurate enough. Please show at least some examples of input and expected output. – Jabberwocky Nov 21 '19 at 11:09
-
12All floating point values are technically rational. As are all integral values. – Peter Nov 21 '19 at 11:10
-
6Technically, you won't be able to even input irrational number from keyboard because any number with finite amount of digits is rational by definition. – Denis Sheremet Nov 21 '19 at 11:15
-
Not "technically". This question is meaningless as it stands: it is not possible to "enter" an irrational number. – Sneftel Nov 21 '19 at 11:40
-
4It is possible by "symbolic" means: "sqrt(2)". Check also: https://www.wolframalpha.com/examples/mathematics/numbers/irrational-numbers/ – Trantor Nov 21 '19 at 11:43
-
2To be clear: Any number with a finite number of decimal (or n-ary) digits is trivially rational (`number = numberWithoutDecimalPoint / (10**decimalDigitCount)`). As is any number that eventually has a periodic digit sequence. The only way to express irrational numbers is by symbolic means. Please elaborate in which form you intend to take inputs, i.e. how you "enter an irrational number". – Max Langhof Nov 21 '19 at 12:01
-
1Check this out on [Stack Exchange MATHEMATICS](https://math.stackexchange.com/questions/898392/irrational-number-test). – Adrian Mole Nov 21 '19 at 12:17
4 Answers
All floating-point numbers can be represented in the form x = significand × 2exponent
, where significand
and exponent
are integers. All such numbers are rational by definition. That's why this problem has only an approximate solution.
One possible approach is to expand a number into a continued fraction. If you encounter very small or zero denominator, the input number is approximately rational. Or, better, if all denominators are not small, then the number is approximately irrational.
Rough idea:
bool is_rational(double x) {
x = std::abs(x);
for (int i = 0; i < 20; ++i) {
const auto a = std::floor(x);
if (x - a < 1e-8)
return true;
x = 1 / (x - a);
}
return false;
}
int main() {
std::cout << std::boolalpha;
std::cout << is_rational(2019. / 9102.) << std::endl; // Output: true
std::cout << is_rational(std::sqrt(2)) << std::endl; // Output: false
}
One should think about the optimal choice of magic numbers inside is_rational()
.

- 25,259
- 5
- 41
- 83
As @Jabberwocky pointed out, you won't be able to verify whether the number is truly irrational with computational means.
Looking at it like a typical student's assignment, my best bet it trying to approach the number by division without creating an endless loop. Consider using Hurwitz's Theorem or Dirichlet's approximation theorem. Either way you will have to set some boundary for your computation (your precision), after how many digits you consider the number irrational.

- 124
- 1
- 13
Irrational number are... Well.. irational. Meaning you can't represent them with a Numerical Value, that's why we write them via a symbole (eg: π). Most you can do is an approximation. For exemple: In Math.h there is approximation for Pi as long double. If you want something more accurate you can use a byte array to reimplements something like BigNum but it will still be an approximation.
We enter an irrational number
If your question was about an Input (cin) then just grab it as a string and do an aproximation.

- 129
- 8
-
1Your explanation is a bit off. Irrational numbers can be represented with a numerical value; they just can't be represented as a number _a/b_, where _a_ and _b_ are both integers. Also not all rational numbers are written with symbols, e.g. 2^(1/2). I realize you probably know this, I'm just pointing out that your answer isn't very precise. – ImaginaryHuman072889 Nov 21 '19 at 13:39
-
@ImaginaryHuman072889 In your code you can't assign a/b as a numerical value. That's what I wanted to show – MrHeliose Nov 21 '19 at 13:45
The amount of memory your computer has is finite. The number of rational numbers is infinite. More importantly, the amount of rational number with a decimal expression too long for your computer to store it is infinite as well (and this is the same to every computer in the world). This is due to the fact that you can build a rational number as "long" as you want just by adding random digits to it.
A computer can't really work with (all) real numbers. As far as I know the only thing computers do is work with a limited accuracy (although accurate enough to be very useful) . All of the numbers they work with are rational.
Given a number, the only thing you can ask your computer to do for you is to calculate its decimal expression to some extent after enough time. All numbers your computer is able to do this with are known as the set of computable numbers. But even this set is "small" when compared with the real numbers set in terms of cardinality.
Therefore, a computer has no way to decide wheter if a number is rational or not, as the concept of number they work with is too simple for doing that.

- 23
- 9
-
And yet *Mathematica* is able do it, while running on a computer: https://mathematica.stackexchange.com/questions/167184/check-if-number-is-irrational-transcendental – Jeremy Friesner Nov 21 '19 at 17:37
-
If it could it in a general way, it would really overwhelm me. If you have a number whose ten first million decimals are the same as those of pi, can you be sure is itpi what we are talking about? You can bet, but you just have a rational number that happens to be really close to pi. I think mathematica is just betting on numbers here. Anyway, I'm completely unfamiliar with Mathematica and how it works, so if anyone can give me light on the way this PossibleAlgebraic[] function works I'd be so happy. – Pascual Esteban Nov 21 '19 at 19:32
-