@@ -209,55 +209,56 @@ impl fmt::Display for Number {
209
209
}
210
210
}
211
211
212
- fn exponent_to_power_f64 ( e : i16 ) -> f64 {
213
- static POS_POWERS : [ f64 ; 23 ] = [
212
+ fn exponentiate_f64 ( n : f64 , e : i16 ) -> f64 {
213
+ static CACHE_POWERS : [ f64 ; 23 ] = [
214
214
1.0 , 1e1 , 1e2 , 1e3 , 1e4 , 1e5 , 1e6 , 1e7 ,
215
215
1e8 , 1e9 , 1e10 , 1e11 , 1e12 , 1e13 , 1e14 , 1e15 ,
216
216
1e16 , 1e17 , 1e18 , 1e19 , 1e20 , 1e21 , 1e22
217
217
] ;
218
218
219
- static NEG_POWERS : [ f64 ; 23 ] = [
220
- 1.0 , 1e-1 , 1e-2 , 1e-3 , 1e-4 , 1e-5 , 1e-6 , 1e-7 ,
221
- 1e-8 , 1e-9 , 1e-10 , 1e-11 , 1e-12 , 1e-13 , 1e-14 , 1e-15 ,
222
- 1e-16 , 1e-17 , 1e-18 , 1e-19 , 1e-20 , 1e-21 , 1e-22
223
- ] ;
224
-
225
- let index = e. abs ( ) as usize ;
219
+ if e >= 0 {
220
+ let index = e as usize ;
226
221
227
- if index < 23 {
228
- if e < 0 {
229
- NEG_POWERS [ index]
222
+ n * if index < 23 {
223
+ CACHE_POWERS [ index]
230
224
} else {
231
- POS_POWERS [ index]
225
+ 10f64 . powf ( index as f64 )
232
226
}
233
227
} else {
234
- // powf is more accurate
235
- 10f64 . powf ( e as f64 )
228
+ let index = -e as usize ;
229
+
230
+ n / if index < 23 {
231
+ CACHE_POWERS [ index]
232
+ } else {
233
+ 10f64 . powf ( index as f64 )
234
+ }
236
235
}
237
236
}
238
237
239
- fn exponent_to_power_f32 ( e : i16 ) -> f32 {
240
- static POS_POWERS : [ f32 ; 16 ] = [
241
- 1.0 , 1e1 , 1e2 , 1e3 , 1e4 , 1e5 , 1e6 , 1e7 ,
242
- 1e8 , 1e9 , 1e10 , 1e11 , 1e12 , 1e13 , 1e14 , 1e15
243
- ] ;
244
238
245
- static NEG_POWERS : [ f32 ; 16 ] = [
246
- 1.0 , 1e-1 , 1e-2 , 1e-3 , 1e-4 , 1e-5 , 1e-6 , 1e-7 ,
247
- 1e-8 , 1e-9 , 1e-10 , 1e-11 , 1e-12 , 1e-13 , 1e-14 , 1e-15
239
+ fn exponentiate_f32 ( n : f32 , e : i16 ) -> f32 {
240
+ static CACHE_POWERS : [ f32 ; 23 ] = [
241
+ 1.0 , 1e1 , 1e2 , 1e3 , 1e4 , 1e5 , 1e6 , 1e7 ,
242
+ 1e8 , 1e9 , 1e10 , 1e11 , 1e12 , 1e13 , 1e14 , 1e15 ,
243
+ 1e16 , 1e17 , 1e18 , 1e19 , 1e20 , 1e21 , 1e22
248
244
] ;
249
245
250
- let index = e. abs ( ) as usize ;
246
+ if e >= 0 {
247
+ let index = e as usize ;
251
248
252
- if index < 16 {
253
- if e < 0 {
254
- NEG_POWERS [ index]
249
+ n * if index < 23 {
250
+ CACHE_POWERS [ index]
255
251
} else {
256
- POS_POWERS [ index]
252
+ 10f32 . powf ( index as f32 )
257
253
}
258
254
} else {
259
- // powf is more accurate
260
- 10f32 . powf ( e as f32 )
255
+ let index = -e as usize ;
256
+
257
+ n / if index < 23 {
258
+ CACHE_POWERS [ index]
259
+ } else {
260
+ 10f32 . powf ( index as f32 )
261
+ }
261
262
}
262
263
}
263
264
@@ -269,11 +270,11 @@ impl From<Number> for f64 {
269
270
let mut e = num. exponent ;
270
271
271
272
if e < -308 {
272
- n *= exponent_to_power_f64 ( e + 308 ) ;
273
+ n = exponentiate_f64 ( n , e + 308 ) ;
273
274
e = -308 ;
274
275
}
275
276
276
- let f = n * exponent_to_power_f64 ( e) ;
277
+ let f = exponentiate_f64 ( n , e) ;
277
278
if num. is_sign_positive ( ) { f } else { -f }
278
279
}
279
280
}
@@ -286,11 +287,11 @@ impl From<Number> for f32 {
286
287
let mut e = num. exponent ;
287
288
288
289
if e < -127 {
289
- n *= exponent_to_power_f32 ( e + 127 ) ;
290
+ n = exponentiate_f32 ( n , e + 127 ) ;
290
291
e = -127 ;
291
292
}
292
293
293
- let f = n * exponent_to_power_f32 ( e) ;
294
+ let f = exponentiate_f32 ( n , e) ;
294
295
if num. is_sign_positive ( ) { f } else { -f }
295
296
}
296
297
}
0 commit comments