Home > front end >  Convert floating number to "prime" fraction
Convert floating number to "prime" fraction

Time:09-22

Sorry for a possibly misleading title.

Given a floating point input "A", we need factor it as

Q = A   B/C

where A is integer part of Q, so basically fix(Q), and B & C must be coprime to one other.

Example:

Q = 14.7419 => Q = 14   23/31

So that

A = 14, B = 23, C = 31

Is there a way to get B and C with intrinsic matlab functions? If not, I would appreciate a guidance in the right direction :)

Thanks in advance.

CodePudding user response:

The solution here would not be the rats function as suggested by beaker under the question, but the rat function:

Q = 14.7419;
A = fix( Q )
[B,C] = rat( Q-A, 0.001)

The second argument to rat is the tolerance. For further details please refer to https://www.mathworks.com/help/matlab/ref/rat.html.

  • Related