2121using QuadrupleLib . Accelerators ;
2222using Float128 = QuadrupleLib . Float128 < QuadrupleLib . Accelerators . DefaultAccelerator > ;
2323using System . Globalization ;
24+ using System . Text ;
2425
2526namespace GoogolSharp
2627{
@@ -65,23 +66,18 @@ private static string ToBinaryString(uint number, int bitWidth = 0)
6566 /// Returns a human-readable string representation of this <see cref="Arithmonym"/>.
6667 /// </summary>
6768 public override string ToString ( ) => ToCommonString ( ) ;
68-
69- /// <summary>
70- /// Returns a human-readable string representation of this <see cref="Arithmonym"/>.
71- /// </summary>
7269 public string ToLetterString ( )
7370 {
7471 if ( IsNaN ( this ) ) return "NaN" ;
7572 if ( this == PositiveInfinity ) return "∞" ;
7673 if ( this == NegativeInfinity ) return "-∞" ;
7774 if ( this == Zero ) return "0" ;
7875
79- // Reconstruct operand in [2, 10)
8076 Float128 value = Operand ;
8177
82- string output = "" ;
78+ var sb = new StringBuilder ( ) ;
8379 if ( _IsNegative )
84- output += "-" ;
80+ sb . Append ( '-' ) ;
8581
8682 string [ ] letters = [ "" , "A" , "B" , "C" , "D" , "E" , "F" , "J" , "K" , "L" , "M" , "N" , "P" ] ;
8783 while ( letters . Length < 63 )
@@ -90,42 +86,61 @@ public string ToLetterString()
9086 switch ( Letter )
9187 {
9288 case 0x01 :
93- output += _IsReciprocal
94- ? 1 / ( 1 + ( ( value - 2 ) / 8 ) )
95- : 1 + ( ( value - 2 ) / 8 ) ;
96- break ;
89+ {
90+ // 1 + (value - 2)/8 → FMA(value, 1/8, 1 - 2/8)
91+ Float128 t = Float128 . FusedMultiplyAdd ( value , Float128 . One / 8 , 1 - ( Float128 ) 2 / 8 ) ;
92+ sb . Append ( _IsReciprocal ? ( 1 / t ) . ToString ( "R" , null ) : t . ToString ( "R" , null ) ) ;
93+ break ;
94+ }
95+
9796 case 0x02 :
98- output += _IsReciprocal
99- ? 1 / ( 2 + ( ( value - 2 ) / 4 ) )
100- : 2 + ( ( value - 2 ) / 4 ) ;
101- break ;
97+ {
98+ // 2 + (value - 2)/4 → FMA(value, 1/4, 2 - 2/4)
99+ Float128 t = Float128 . FusedMultiplyAdd ( value , Float128 . One / 4 , 2 - ( Float128 ) 2 / 4 ) ;
100+ sb . Append ( _IsReciprocal ? ( 1 / t ) . ToString ( "R" , null ) : t . ToString ( "R" , null ) ) ;
101+ break ;
102+ }
103+
102104 case 0x03 :
103- output += _IsReciprocal
104- ? 1 / ( value * 2 )
105- : value * 2 ;
106- break ;
105+ {
106+ Float128 t = value * 2 ;
107+ sb . Append ( _IsReciprocal ? ( 1 / t ) . ToString ( "R" , null ) : t . ToString ( "R" , null ) ) ;
108+ break ;
109+ }
110+
107111 case 0x04 :
108- output += _IsReciprocal
109- ? 1 / ( value * 10 )
110- : value * 10 ;
111- break ;
112+ {
113+ Float128 t = value * 10 ;
114+ sb . Append ( _IsReciprocal ? ( 1 / t ) . ToString ( "R" , null ) : t . ToString ( "R" , null ) ) ;
115+ break ;
116+ }
117+
112118 case 0x05 :
113- output += _IsReciprocal
114- ? Float128PreciseTranscendentals . SafeExp10 ( - value ) : Float128PreciseTranscendentals . SafeExp10 ( value ) ;
115- break ;
119+ {
120+ Float128 t = _IsReciprocal
121+ ? Float128PreciseTranscendentals . SafeExp10 ( - value )
122+ : Float128PreciseTranscendentals . SafeExp10 ( value ) ;
123+
124+ sb . Append ( t . ToString ( "R" , null ) ) ;
125+ break ;
126+ }
127+
116128 case 0x06 :
117- output += ArithmonymFormattingUtils . FormatArithmonymFromLetterF ( Operand , _IsReciprocal ) ;
129+ sb . Append ( ArithmonymFormattingUtils . FormatArithmonymFromLetterF ( Operand , _IsReciprocal ) ) ;
118130 break ;
131+
119132 default :
120133 if ( _IsReciprocal )
121- output += "1 / " ;
122- output += letters [ Letter ] ;
123- output += value ;
134+ sb . Append ( "1 / " ) ;
135+ sb . Append ( letters [ Letter ] ) ;
136+ sb . Append ( value . ToString ( "R" , null ) ) ;
124137 break ;
125138 }
126- return output ;
139+
140+ return sb . ToString ( ) ;
127141 }
128142
143+
129144 /// <summary>
130145 /// Returns a human-readable string representation of this <see cref="Arithmonym"/>.
131146 /// </summary>
@@ -136,63 +151,79 @@ public string ToCommonString()
136151 if ( this == NegativeInfinity ) return "-∞" ;
137152 if ( this == Zero ) return "0" ;
138153
139- // Reconstruct operand in [2, 10)
140154 Float128 value = Operand ;
141155
142- if ( Letter == 0x0C ) value += 2 ;
156+ if ( Letter == 0x0C )
157+ value += 2 ;
143158
144- string output = "" ;
159+ var sb = new StringBuilder ( ) ;
145160 if ( _IsNegative )
146- output += "-" ;
161+ sb . Append ( '-' ) ;
147162
148163 string [ ] prefixes = [ "" , "A" , "B" , "C" , "D" , "10^" , "10^^" , "{10,10," , "{10," , "{10," , "{10,10," , "{10,10,10," , "{10," , "X^^" , "X^^^" , "{X," ] ;
149164 while ( prefixes . Length < 63 )
150165 prefixes = [ .. prefixes , $ "[{ prefixes . Length } ]"] ;
166+
151167 string [ ] suffixes = [ "" , "" , "" , "" , "" , "" , "" , "}" , ",1,2}" , ",2,2}" , ",2}" , "}" , "(1)2}" , " & 10" , " & 10" ] ;
152168 while ( suffixes . Length < 63 )
153169 suffixes = [ .. suffixes , $ "[{ suffixes . Length } ]"] ;
154170
155171 switch ( Letter )
156172 {
157173 case 0x01 :
158- output += _IsReciprocal
159- ? 1 / ( 1 + ( ( value - 2 ) / 8 ) )
160- : 1 + ( ( value - 2 ) / 8 ) ;
161- break ;
174+ {
175+ Float128 t = Float128 . FusedMultiplyAdd ( value , Float128 . One / 8 , 1 - ( Float128 ) 2 / 8 ) ;
176+ sb . Append ( _IsReciprocal ? ( 1 / t ) . ToString ( "R" , null ) : t . ToString ( "R" , null ) ) ;
177+ break ;
178+ }
179+
162180 case 0x02 :
163- output += _IsReciprocal
164- ? 1 / ( 2 + ( ( value - 2 ) / 4 ) )
165- : 2 + ( ( value - 2 ) / 4 ) ;
166- break ;
181+ {
182+ Float128 t = Float128 . FusedMultiplyAdd ( value , Float128 . One / 4 , 2 - ( Float128 ) 2 / 4 ) ;
183+ sb . Append ( _IsReciprocal ? ( 1 / t ) . ToString ( "R" , null ) : t . ToString ( "R" , null ) ) ;
184+ break ;
185+ }
186+
167187 case 0x03 :
168188 {
169- Float128 result = _IsReciprocal
170- ? 1 / ( value * 2 )
171- : value * 2 ;
172- output += ArithmonymFormattingUtils . FormatNearInteger ( result ) ;
189+ Float128 t = value * 2 ;
190+ Float128 result = _IsReciprocal ? 1 / t : t ;
191+ sb . Append ( ArithmonymFormattingUtils . FormatNearInteger ( result ) ) ;
173192 break ;
174193 }
194+
175195 case 0x04 :
176- output += _IsReciprocal
177- ? 1 / ( value * 10 )
178- : value * 10 ;
179- break ;
196+ {
197+ Float128 t = value * 10 ;
198+ sb . Append ( _IsReciprocal ? ( 1 / t ) . ToString ( "R" , null ) : t . ToString ( "R" , null ) ) ;
199+ break ;
200+ }
201+
180202 case 0x05 :
181- output += _IsReciprocal
182- ? Float128PreciseTranscendentals . SafeExp10 ( - value ) : Float128PreciseTranscendentals . SafeExp10 ( value ) ;
183- break ;
203+ {
204+ Float128 t = _IsReciprocal
205+ ? Float128PreciseTranscendentals . SafeExp10 ( - value )
206+ : Float128PreciseTranscendentals . SafeExp10 ( value ) ;
207+
208+ sb . Append ( t . ToString ( "R" , null ) ) ;
209+ break ;
210+ }
211+
184212 case 0x06 :
185- output += ArithmonymFormattingUtils . FormatArithmonymFromLetterF ( Operand , _IsReciprocal , "*10^" , false ) ;
213+ sb . Append ( ArithmonymFormattingUtils . FormatArithmonymFromLetterF ( Operand , _IsReciprocal , "*10^" , false ) ) ;
186214 break ;
215+
187216 default :
188217 if ( _IsReciprocal )
189- output += "1 / " ;
190- output += prefixes [ Letter ] ;
191- output += value ;
192- output += suffixes [ Letter ] ;
218+ sb . Append ( "1 / " ) ;
219+ sb . Append ( prefixes [ Letter ] ) ;
220+ sb . Append ( value . ToString ( "R" , null ) ) ;
221+ sb . Append ( suffixes [ Letter ] ) ;
193222 break ;
194223 }
195- return output ;
224+
225+ return sb . ToString ( ) ;
196226 }
227+
197228 }
198229}
0 commit comments