@@ -5,44 +5,38 @@ String times10(String nstring) {
55 return nstring.split ("." )[0 ]+ nstring.split ("." )[1 ][0 ]+ "." + nstring.split ("." )[1 ].substring (1 );
66 return nstring+ "0" ;
77}
8- String ? advdiv (double n1d , double n2d, [int ri = 0 , final String rstr1 = "[" , final String rstr2 = "]" ]) {
8+ String ? advdiv (double n1 , double n2d, [int ri = 0 , final String rstr1 = "[" , final String rstr2 = "]" ]) {
99 bool over = false ;
10- int carry = 0 , newcarry = 0 , x, d;
11- int ? rcount = null ;
12- String n1string, n2string, r, res = "" ;
13- List <int > carries;
10+ int carry = 0 , newcarry = 0 , rcount = - 1 , x, y, d;
11+ String n1string, n2string, r, res = "" , result;
12+ List <int > carries = [];
1413 List <String > n1s;
15- late final int n1, n2, dotx;
14+ late final int n2, dotx;
1615 late final String sign;
1716 late final List <String > n1s1;
1817
1918 if (n2d== 0 )
2019 return null ;
21- sign = (n1d < 0 ? n2d >= 0 : n2d < 0 ) ? "-" : "" ;
22- n1d = n1d .abs ();
20+ sign = (n1 < 0 ? n2d >= 0 : n2d < 0 ) ? "-" : "" ;
21+ n1 = n1 .abs ();
2322 n2d = n2d.abs ();
2423 ri = ri.abs ();
2524 r = ri.toString ();
26- n1string = n1d .toString ();
25+ n1string = n1 .toString ();
2726 n2string = n2d.toString ();
2827 if (n1string.endsWith (".0" ))
29- n1string = n1string.replaceAll (".0" , "" );
28+ n1string = n1string.replaceFirst (".0" , "" );
3029 if (n2string.endsWith (".0" ))
31- n2string = n2string.replaceAll (".0" , "" );
30+ n2string = n2string.replaceFirst (".0" , "" );
3231
33- while (n1string. contains ( "." ) || n2string.contains ("." )) {
32+ while (n2string.contains ("." )) {
3433 if (! n1string.contains ("." )) {
3534 n1string+= r[0 ];
3635 if (r.length > 1 )
3736 r = r.substring (1 )+ r[0 ];
3837 }
3938 else {
4039 n1string = times10 (n1string);
41- if (n1string.contains ("." )) {
42- n1string+= r[0 ];
43- if (r.length > 1 )
44- r = r.substring (1 )+ r[0 ];
45- };
4640 };
4741 n2string = times10 (n2string);
4842 if (n1string.endsWith (".0" ))
@@ -51,7 +45,7 @@ String? advdiv(double n1d, double n2d, [int ri = 0, final String rstr1 = "[", fi
5145 n2string = n2string.replaceAll (".0" , "" );
5246 };
5347
54- n1 = int .parse (n1string);
48+ n1 = double .parse (n1string);
5549 n2 = int .parse (n2string);
5650 n1s = n1string.split ("" );
5751 n1s1 = n1string.split ("." )[0 ].split ("" );
@@ -69,35 +63,44 @@ String? advdiv(double n1d, double n2d, [int ri = 0, final String rstr1 = "[", fi
6963
7064 dotx = x;
7165 res+= "." ;
72- carries = [carry];
66+ if (! n1s.contains ("." ))
67+ n1s.add ("." );
7368
7469 while (n1s.length <= x)
7570 n1s.add ("" );
7671
7772 while (true ) {
7873 x++ ;
7974 if (x >= n1s.length) {
80- if (rcount== null )
81- rcount = 0 ;
82- else
83- rcount++ ;
75+ rcount++ ;
8476 over = true ;
8577 n1s.add (r[rcount % r.length]);
8678 };
8779
8880 newcarry = (int .parse (times10 (carry.toString ())) + int .parse (n1s[x])) - n2 * ((int .parse (times10 (carry.toString ())) + int .parse (n1s[x])) ~ / n2);
8981
90- if (( newcarry== 0 && r== "0" ) || List . generate (carries.length, ( int y) => y). any ((y) => carries[y] == newcarry && (y % r.length) == ((x - dotx) % r.length)))
82+ if (newcarry== 0 && r== "0" ) {
9183 res+= ((int .parse (times10 (carry.toString ())) + int .parse (n1s[x])) ~ / n2).toString ();
92- if (newcarry== 0 && r== "0" )
9384 return sign+ res.replaceAll (RegExp (r'^0+|0$' ), "" ).replaceFirst (RegExp (r'^\.' ), "0." ).replaceFirst (RegExp (r'\.$' ), "" );
94- if (List .generate (carries.length, (int y) => y).any ((y) => carries[y]== newcarry && (y % r.length)== ((x - dotx) % r.length)))
95- return sign+ (res.substring (0 , dotx + List .generate (carries.length, (int y) => y).firstWhere ((y) => carries[y]== newcarry && (y % r.length)== ((x - dotx) % r.length)) + 1 )+ rstr1+ res.substring (dotx + List .generate (carries.length, (int y) => y).firstWhere ((y) => carries[y]== newcarry && (y % r.length)== ((x - dotx) % r.length)) + 1 )+ rstr2).replaceFirst (RegExp (r'^0+' ), "" ).replaceFirst (RegExp (r'^\.' ), "0." );
85+ };
86+ if (over) {
87+ for (y = 0 ; y < carries.length; y++ ) {
88+ if (carries[y]== newcarry && (y % r.length)== ((rcount + 1 ) % r.length)) {
89+ res+= ((int .parse (times10 (carry.toString ())) + int .parse (n1s[x])) ~ / n2).toString ();
90+ result = sign+ (res.substring (0 , x - rcount + y)+ "[" + res.substring (x - rcount + y)+ "]" ).replaceFirst (RegExp (r'^0+' ), "" ).replaceFirst (RegExp (r'^\.' ), "0." );
91+ if (result[result.indexOf ("[" ) - 1 ]== result[result.indexOf ("]" ) - 1 ])
92+ result = result.substring (0 , result.indexOf ("[" ) - 1 )+ "[" + result[result.indexOf ("[" ) - 1 ]+ result.substring (result.indexOf ("[" ) + 1 , result.indexOf ("]" ) - 1 )+ "]" ;
93+ if (result.indexOf ("]" )== result.indexOf ("[" ) + 3 && result[result.indexOf ("[" ) + 1 ]== result[result.indexOf ("[" ) + 2 ])
94+ result = result.substring (0 , result.indexOf ("[" ) + 2 )+ "]" ;
95+ return result.replaceFirst ("[" , rstr1).replaceFirst ("]" , rstr2);
96+ };
97+ };
98+ };
9699
97100 res+= ((int .parse (times10 (carry.toString ())) + int .parse (n1s[x])) ~ / n2).toString ();
98- carry = newcarry;
99101 if (over)
100102 carries.add (carry);
103+ carry = newcarry;
101104 };
102105}
103106
0 commit comments