@@ -148,68 +148,6 @@ private ulong Low64
148
148
1e80
149
149
} ;
150
150
151
- // Fast access for solving for x in: 2^x = 10^(-n) where n is 0-28
152
- private static readonly int [ ] s_intDecimalScaleToIeeeBase2 = new int [ ] {
153
- 0 ,
154
- - 1 ,
155
- - 2 ,
156
- - 4 ,
157
- - 5 ,
158
- - 7 ,
159
- - 8 ,
160
- - 10 ,
161
- - 11 ,
162
- - 12 ,
163
- - 14 ,
164
- - 15 ,
165
- - 17 ,
166
- - 18 ,
167
- - 20 ,
168
- - 21 ,
169
- - 23 ,
170
- - 24 ,
171
- - 25 ,
172
- - 27 ,
173
- - 28 ,
174
- - 30 ,
175
- - 31 ,
176
- - 33 ,
177
- - 34 ,
178
- - 36 ,
179
- - 37 ,
180
- - 38 ,
181
- - 40
182
- /* 315,
183
- 312,
184
- 308,
185
- 305,
186
- 302,
187
- 298,
188
- 295,
189
- 292,
190
- 289,
191
- 285,
192
- 282,
193
- 279,
194
- 275,
195
- 272,
196
- 269,
197
- 265,
198
- 262,
199
- 259,
200
- 255,
201
- 252,
202
- 249,
203
- 245,
204
- 242,
205
- 239,
206
- 235,
207
- 232,
208
- 229,
209
- 225,
210
- 222*/
211
- } ;
212
-
213
151
#region Decimal Math Helpers
214
152
215
153
private static unsafe uint GetExponent ( float f )
@@ -1653,11 +1591,17 @@ internal static float VarR4FromDec(in decimal value)
1653
1591
/// </summary>
1654
1592
internal static double VarR8FromDec ( in decimal value )
1655
1593
{
1656
- // Value taken via reverse engineering the double that corresponds to 2^64. (oleaut32 has ds2to64 = DEFDS(0, 0, DBLBIAS + 65, 0))
1657
- const double ds2to64 = 1.8446744073709552e+019 ;
1594
+ // Step 1: Isolate integral component as a double
1595
+ decimal integral = Truncate( value ) ;
1596
+ UInt128 integralMantissa = new UInt128 ( integral . High , integral . Low64 ) ;
1597
+ double integralDouble = ( double ) integralMantissa ;
1598
+
1599
+ // Step 2: Isolate fractional component as a double
1600
+ decimal fractional = value - integral;
1601
+ UInt128 fractionalMantissa = new UInt128 ( fractional . High , fractional . Low64 ) ;
1602
+ double fractionalDouble = ( double ) fractionalMantissa / s_doublePowers10 [ fractional . Scale ] ;
1658
1603
1659
- double dbl = ( ( double ) value . Low64 +
1660
- ( double ) value . High * ds2to64 ) / s_doublePowers10 [ value . Scale ] ;
1604
+ double dbl = integralDouble + fractionalDouble ;
1661
1605
1662
1606
if ( decimal . IsNegative ( value ) )
1663
1607
dbl = - dbl ;
0 commit comments