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))
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))