Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Irrational number check function

Tags:

python

math

I am trying to graph the following function:

f(x) = 0 if x is rational else 1 # so 1 if x is irrational 

My plan is to use python and matplotlib. How do you generate random irrational numbers in Python?

like image 318
epocolis Avatar asked Nov 28 '25 06:11

epocolis


2 Answers

This is called Dirichlet function, and it's example of function that nowhere continuous. It's a simple mathematical fact, between any pair of numbers, there is infinite number of rational and infinite irrational number.

Plotting this function in practice is equivalent to plotting f(x) = 0 and f(x) = 1, as you're plotting using discrete pixels.

There are two gotchas:

  • even though Python uses arbitrary-precision arithmetic, it's only capable of representing rational numbers;
  • in the set of real numbers there is continuum of irrational numbers and only aleph-zero rational numbers. Thus probability that any random number is irrational is 1;

Either way, this kind of "problem" is not meant to be approached as strictly programming problem.

like image 88
vartec Avatar answered Nov 30 '25 19:11

vartec


The answer is you can't.

What you can do is figure out some epsilon after which this number is considered irrational.

It will look the same.

consider this: square root of 2 is an irrational number.

wolframlpah gives you an approximation : 1.4142135623730950488016887242096980785696718753769480...

python only sees 1.4142135623730950488016887242096980785696718753769480 which means: 1+ 4142135623730950488016887242096980785696718753769480/ 10000000000000000000000000000000000000000000000000000

like image 33
raam86 Avatar answered Nov 30 '25 20:11

raam86