Approaching a real number by a rational number?

How can I build a code that for any real number it will find a rational one that approaches it very closely?

What have you tried so far?

Computers can only represent rational numbers, so it would help if you gave more details about your problem.

well basically this is all it says.
"Write a code that will approximate with a given ε>0 any real number by a rational one"

I'm unsure if you are a student of mathematics , or computer science or both ?
It would be interesting to know if you have been 'taught' anything relevant towards the task you have now been assigned ? it might point the way to approach what you have been asked to do.

I expect you are being asked to implement the continued fractions algorithm.
Continued fractions (
sidenote : the linked slide does have a rather glaring typo, when discussing sqrt(2) it should be 99/70 rather than 89/70