Home > other >  A rational number is known, two integers, the divisor of two integers for the rational number (some
A rational number is known, two integers, the divisor of two integers for the rational number (some

Time:11-18

When writing a program to use rational number is known, said the rational number of two division by integer. Below is the procedure to have questions and Suggestions can communication, deficiencies please point out.
 
The import math as mt
The import re

Def Ftoints (x: float) - & gt; The tuple:
If abs (x)==0:
Return 0, 1
Kk=1 if x> 0 the else - 1
Ss=STR (abs (x))
Dotloc=ss. Find (". ")
If dotloc==1:
Return int (x), 1
SST=re. The.findall (r '. \ [0-9] *? ([0-9] {1}) 1 + \ [0-9]? $', ss)
If len (SST) & gt; 0:
Ssloc=ss. Find (SST [0])
A=int (SST [0])
B=int ((10 * * len (SST [0]) - 1) * 10 * * (ssloc - dotloc - 1))
C=mt. The GCD (a, b)
A=a//c;
=b/b/cNeo-philosophy=ss [: ssloc]
Dd, ee=Ftoints (float (neo-philosophy))
A=int (* ee + b * dd)
B=int (b * ee)
The else:
B=int (10 * * (len (ss) - dotloc - 1))
A=int (abs (x) * b)
C=mt. The GCD (a, b)
A=a//c;
=b/b/cReturn kk * a, b


If __name__=="__main__" :
Print (Ftoints (3.66666667))

# Out: (1) - 11, 3)

CodePudding user response:

Print (Ftoints (0.0001))
Print (Ftoints (0.000000001))

CodePudding user response:

Improved, the format of default precision is 6 f format can only handle below this accuracy, increase the accuracy of other normal data cannot handle (because the precision of broadening the above after 0)

 
The import math as mt
The import re

Def Ftoints (x: float) - & gt; The tuple:
If mt. Isclose (abs (x), 0, rel_tol=1-9, e abs_tol=1-9) e:
Return 0, 1
Kk=1 if x> 0 the else - 1
Ss="{: f}". The format (abs (x))
Dotloc=ss. Find (". ")
If dotloc==1:
Return int (x), 1
SST=re. The.findall (r '. \ [0-9] *? ([0-9] {1}) 1 + \ [0-9]? $', ss)
If len (SST) & gt; 0 and int (SST [0]).=0:
Ssloc=ss. Find (SST [0])
A=int (SST [0])
B=int ((10 * * len (SST [0]) - 1) * 10 * * (ssloc - dotloc - 1))
C=mt. The GCD (a, b)
If c!=0:
A=a//c;
=b/b/cNeo-philosophy=ss [: ssloc]
Dd, ee=Ftoints (float (neo-philosophy))
A=int (* ee + b * dd)
B=int (b * ee)
The else:
B=int (10 * * (len (ss) - dotloc - 1))
A=int (abs (x) * b)
C=mt. The GCD (a, b)
If c!=0:
A=a//c;
=b/b/cReturn kk * a, b

If __name__=="__main__" :
Print (Ftoints (3.66666667))

# Out: (1) - 11, 3)

CodePudding user response:

Print (Ftoints (2334334334) -.)

CodePudding user response:

reference chuifengde reply: 3/f
print (Ftoints (2334334334) -.)


And modify the, welcome fault-finding, set precision to 15, too high when the input data and final storage is different also. The program is as follows:

 
The import math as mt
The import re

Def Ftoints (x: float) - & gt; The tuple:
If mt. Isclose (abs (x), 0, rel_tol=1-15, e abs_tol=1 e - 15) :
Return 0, 1
Kk=1 if x> 0 the else - 1
Ss="{: & lt; 0.15 f} ". The format (abs (x))
Dotloc=ss. Find (". ")
If dotloc==1:
Return int (x), 1
SST=re. The.findall (r '. \ [0-9] *? ([0-9] {1}) 1 + \ [1-9]? [0] + $', ss)
If len (SST) & gt; 0 and int (SST [0]).=0:
Ssloc=ss. Find (SST [0])
A=int (SST [0])
B=int ((10 * * len (SST [0]) - 1) * 10 * * (ssloc - dotloc - 1))
C=mt. The GCD (a, b)
If c!=0:
A=a//c;
=b/b/cNeo-philosophy=ss [: ssloc]
Dd, ee=Ftoints (float (neo-philosophy))
A=round (a + b * * ee dd)
B=round (b * ee)
The else:
Sk=re. The.findall (r '[0] + $, ss)
Tt=len (ss) if len (sk) & lt;=0 else ss. Rfind (sk [0])
B=round (10 * * (tt - dotloc - 1))
A=round (abs (x) * b)
C=mt. The GCD (a, b)
If c!=0:
A=a//c;
=b/b/cReturn kk * a, b

If __name__=="__main__" :
Print (Ftoints (0.01))
Print (Ftoints (0.000000001))
Print (Ftoints (2334334334) -.)
Print (Ftoints (1.001))
Print (Ftoints (0.003333333))
Print (Ftoints (0.12666666667))

# Out:
(1, 100)
(1, 1000000000)
(1166, 4995)
(1001, 1000)
(1, 300)
(150) - 19,

CodePudding user response:

A small mistake, the program above this line? [0] + $instead? [0] * $
SST=re. The.findall (r '. \ [0-9] *? ([0-9] {1}) 1 + \ [1-9]? [0] + $', ss)
Instead of
SST=re. The.findall (r '. \ [0-9] *? ([0-9] {1}) 1 + \ [1-9]? [0] * $', ss)

CodePudding user response:

Is ultimately such
 
Def Ftoints (x: float) - & gt; The tuple:
If mt. Isclose (abs (x), 0, rel_tol=1-15, e abs_tol=1 e - 15) :
Return 0, 1
Kk=1 if x> 0 the else - 1
Ss="{: & lt; 0.15 f} ". The format (abs (x))
Dotloc=ss. Find (". ")
If dotloc==1:
Return round (x), 1
SST=re. The.findall (r '. \ [0-9] *? ([0-9] {1}) 1 + \ [1-9]? [0] * $', ss)
If len (SST) & gt; 0 and int (SST [0]).=0:
Ssloc=ss. Find (SST [0])
A=int (SST [0])
B=round ((10 * * len (SST [0]) - 1) * 10 * * (ssloc - dotloc - 1))
C=mt. The GCD (a, b)
If c!=0:
A=a//c;
=b/b/cNeo-philosophy=ss [: ssloc]
Dd, ee=Ftoints (float (neo-philosophy))
A=round (a + b * * ee dd)
B=round (b * ee)
The else:
Sk=re. The.findall (r '[0] + $, ss)
Tt=len (ss) if len (sk) & lt;=0 else ss. Rfind (sk [0])
B=round (10 * * (tt - dotloc - 1))
A=round (abs (x) * b)
C=mt. The GCD (a, b)
If c!=0:
A=a//c;
=b/b/cReturn kk * a, b

CodePudding user response:

Ftoints (0.22334334334)

CodePudding user response:

The
reference 7 floor chuifengde response:
Ftoints (0.22334334334)


Second iteration and repeat number, the following procedure to remove the second iteration, welcome to continue fault-finding, change after the procedure is as follows:
 
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related