# How can I test if a number is irrational?

조회 수: 60(최근 30일)

표시 이전 댓글

Hello there,

In digital signal processing, one indicator that a signal may be quasi-periodic is that the ratio between the pair of frequencies (f1/f2) provides an irrational number as result. What I want to do is check if the result is irrational or not in the form of a function, just as ge(a,b) returns a boolean relative to 'a' being greater or equal to 'b' or not. However, I was not able to find any function appropriate for this (and no other posts regarding this too).

P.S.: I know that the user will know if (s)he is inputing an irrational number. As silly as it seems, I still want to display a message saying whether it is periodic or quasi-periodic. By the way, of course, there is still the possibility of inputing two irrational numbers, one for each frequency, and having a rational result. This is why such a check becomes helpful.

----------------------------------------------

EDIT 1: As an addition, I guess the aformentioned may have mislead most of you to believe that I wanted MATLAB to tell me if a number is rational or not only by its double or int value. However, I'm thinking about what is going in the Symbolic Toolbox level.

Take, for instance, these parameters:

f1 = 2;

f2 = sqrt(2);

a = f1/f2;

Provides (or must provide) in symbolical level...

a = sqrt(2);

Since I can work, for instance, with functions. I'm considering that this is different from

a = double(f1/f2);

We know that if we type this, say, in the command window, f1/f2 will yield the same as double(f1/f2), but the processing is not exactly the same for these cases. My knowledge on MATLAB may fail at this point, but I believe that in some point in the processing of data, f1/f2 still is a symbolic value before it is converted to double to be displayed. That is the level of processing that I'm trying to work with. It seems reasonable to me that, at this stage, it may be easy to infer the "class" of our symbolic number... or isn't it?

### 답변(5개)

Guillaume
2017년 1월 11일

편집: Guillaume
2017년 1월 11일

I don't think you've thought this through properly:

- Nobody knows a way to find whether an arbitrary number is irrational.
- By definition, all numbers stored on a computer (in IEE754 format) are rationals, since they're all fractions of powers of 2.

##### 댓글 수: 3

Walter Roberson
2020년 10월 11일

Walter Roberson
2017년 1월 11일

In MATLAB, all numbers that can be expressed in the data types uint8, uint16, uint32, uint64, int8, int16, int32, int64, and logical are definitely rational. For the data types single and double, the only values that can be expressed that are not rational are -inf, +inf, and NaN, but those values are also not irrational (they are not, strictly speaking, numbers.)

Therefore the only way numbers could be entered that might potentially be irrational is if they are entered as strings, either as named constants or as expressions like sqrt(2) and you would have to proceed from there.

##### 댓글 수: 8

Christopher Creutzig
2018년 3월 27일

> Given two symbolic constants, f1 and f2, that involve only numbers and roots of integers or rationals, then MATLAB will automatically factor the rationals inside of roots and remove as much outside the root as possible.

That is not exactly true. E.g., MATLAB will not cancel `sqrt(sym(6))/sqrt(sym(2))` by default. A call to `simplify` will.

John D'Errico
2017년 1월 12일

편집: John D'Errico
2017년 1월 13일

You say there should be some simple way to know if a symbolic toolbox result is rational or not. Note that as simple a question of whether pi+exp(1) is rational is unproven as far as I can see.

You can want a nice simple solution to exist. But nice simple solutions are not always available.

You talk about a ratio between a pair of frequencies as a rational number. To me, this is silly, that you are worried about something being EXACTLY representable as a rational number, beyond 16 significant digits. Instead, you might just look if the ratio is well approximated by a fraction with a reasonably small denominator.

[N,D] = rat(.2324343434232,0.001)

N =

10

D =

43

N/D

ans =

0.23256

[N,D] = rat(0.30000001,0.001)

N =

3

D =

10

Pick some reasonable tolerance, and don't worry about 40 digits.

David Goodmanson
2017년 1월 16일

편집: David Goodmanson
2017년 1월 16일

Hello Alexandre, although your interest is along conceptual lines, still it's fun to look at practical consequences. Supposing the two frequencies are high, up around the GHz cell phone range, and have all 16 digits of double precision:

f1 = 1234567890123456e-6

f1 = 1234567890123457e-6

If f1 and f2 are incommensurate (gcd = 1) as in this example, and supposing you had frequency stability of better than one part in 10^16 (which isn't going to happen outside of a metrology lab) then it will take 1e6 seconds before the carrier pattern repeats, or about 12 days. Not even Heloise and Abelard would be on the phone that long.

There is of course a big difference philosophically, but when you get enough digits the distinction between incommensurate and 'irrational ratio' becomes moot.

LALE ASIK
2018년 3월 23일

편집: LALE ASIK
2018년 3월 23일

May I ask you a question? Do you find a method how to test it?

##### 댓글 수: 6

Walter Roberson
2018년 3월 27일

For example, consider 43 and 179 with an fft bin width of 1. 43/179 is 0.240223463687151, which needs all of the decimals to express the ratio of frequencies as far as humans can easily perceive. But with the bin width of 1, the value could be between 42/180 and 44/178, and 42/180 is 0.233333333333333 which is 7/30 which people probably would consider rational.

Can we get a measure of rationality by using rat() and looking at the complexity of the continued fraction?

>> rat(43/179)

ans =

'0 + 1/(4 + 1/(6 + 1/(7)))'

>> rat(42/180)

ans =

'0 + 1/(4 + 1/(4 + 1/(-2)))'

>> rat(44/178)

ans =

'0 + 1/(4 + 1/(22))'

>> rat(pi)

ans =

'3 + 1/(7 + 1/(16))'

Ummm... No, apparently not. When rat(pi) shows up as less complex than the other values, we are in trouble. Unless, that is, we start adding in the tolerance to the rat() call:

>> rat(pi,1e-16)

ans =

'3 + 1/(7 + 1/(16 + 1/(-294 + 1/(3 + 1/(-4 + 1/(5 + 1/(-15)))))))'

But still, what we would tend to see as simple, 42/180, 0.23* comes out more complex than 44/178, 0.247191011235955, that we would struggle to see rationality in.

### 참고 항목

### 제품

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!