American Citizen on Thu, 05 Jun 2025 07:08:36 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

question on recovering a rational number from its decimal value


The situation of recovering a rational from a decimal value is one where I found the best performance was given by using contfracpnqn(contfrac(decimal_value)) to recover the rational number. But I will bring up bestappr and algdep also and provide a simple example.

For example:

 \p38
   realprecision = 38 significant digits
? a = 7557322358563246340/14991082624209354397
? b=1.0*a
%182 = 0.50412118644178492341805370212027471167
? bestappr(b)
%183 = 79189105149167506491695797085942154095/157083469766673122582441828941171667778
? bestappr(b,10^20)
%184 = 7557322358563246340/14991082624209354397

I am trying to recover a, not some rational with many more digits to both the numerator and denominator for a which is what bestappr(b) did without a denominator size restriction.

Let's look at c= contfracpnqn(contfrac(b))

%185 =
[ 7557322358563246340 1893554184859258427]

[14991082624209354397 3756148790778748428]
and c[1,1]/c[2,1] recovers a quite nicely.

I found this method quite reliable for nailing the "correct" rational given its decimal.

Would using algdep(b,1) work well too?

? algdep(b,1)
%190 = 579338739897539*x - 292056932908836

this is in error, by increasing precision to 57 digits I can successfully recover a

? \p57
   realprecision = 57 significant digits
? b=1.0*a
%192 = 0.504121186441784923418053702120274711673751913427062758309
? algdep(b,1)
%193 = 14991082624209354397*x - 7557322358563246340

so apparently we have to increase the precision by 1.5x before doing the algdep command as it seems to me.

What is the timing trade off when doings hundreds of thousands of rational number recoveries from their decimal value? Is algdep(b,1) more costly in time than bestappr(b) and is contfracpnqn(contfrac(b)) faster than bestappr?

Randall