dashu_float/convert.rs
1use core::{
2 convert::{TryFrom, TryInto},
3 num::FpCategory,
4};
5
6use dashu_base::{
7 Approximation::*, BitTest, ConversionError, DivRemEuclid, EstimatedLog2, FloatEncoding, Sign,
8 Signed,
9};
10use dashu_int::{IBig, UBig, Word};
11
12use crate::{
13 error::{assert_finite, panic_unlimited_precision},
14 fbig::FBig,
15 repr::{Context, Repr},
16 round::{
17 mode::{HalfAway, HalfEven, Zero},
18 Round, Rounded, Rounding,
19 },
20 utils::{ilog_exact, shl_digits, shl_digits_in_place, shr_digits},
21};
22
23impl<R: Round> Context<R> {
24 /// Convert an [IBig] instance to a [FBig] instance with precision
25 /// and rounding given by the context.
26 ///
27 /// # Examples
28 ///
29 /// ```
30 /// # use core::str::FromStr;
31 /// # use dashu_base::ParseError;
32 /// # use dashu_float::DBig;
33 /// use dashu_base::Approximation::*;
34 /// use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
35 ///
36 /// let context = Context::<HalfAway>::new(2);
37 /// assert_eq!(context.convert_int::<10>((-12).into()), Exact(DBig::from_str("-12")?));
38 /// assert_eq!(
39 /// context.convert_int::<10>(5678.into()),
40 /// Inexact(DBig::from_str("5.7e3")?, AddOne)
41 /// );
42 /// # Ok::<(), ParseError>(())
43 /// ```
44 pub fn convert_int<const B: Word>(&self, n: IBig) -> Rounded<FBig<R, B>> {
45 let repr = Repr::<B>::new(n, 0);
46 self.repr_round(repr).map(|v| FBig::new(v, *self))
47 }
48}
49
50macro_rules! impl_from_float_for_fbig {
51 ($t:ty) => {
52 impl TryFrom<$t> for Repr<2> {
53 type Error = ConversionError;
54
55 fn try_from(f: $t) -> Result<Self, Self::Error> {
56 match f.decode() {
57 Ok((man, exp)) => Ok(Repr::new(man.into(), exp as _)),
58 Err(FpCategory::Infinite) => match f.sign() {
59 Sign::Positive => Ok(Self::infinity()),
60 Sign::Negative => Ok(Self::neg_infinity()),
61 },
62 _ => Err(ConversionError::OutOfBounds), // NaN
63 }
64 }
65 }
66
67 impl<R: Round> TryFrom<$t> for FBig<R, 2> {
68 type Error = ConversionError;
69
70 fn try_from(f: $t) -> Result<Self, Self::Error> {
71 match f.decode() {
72 Ok((man, exp)) => {
73 let repr = Repr::new(man.into(), exp as _);
74
75 // The precision is inferenced from the mantissa, because the mantissa of
76 // normal float is always normalized. This will produce correct precision
77 // for subnormal floats
78 let bits = man.unsigned_abs().bit_len();
79 let context = Context::new(bits);
80 Ok(Self::new(repr, context))
81 }
82 Err(FpCategory::Infinite) => match f.sign() {
83 Sign::Positive => Ok(Self::INFINITY),
84 Sign::Negative => Ok(Self::NEG_INFINITY),
85 },
86 _ => Err(ConversionError::OutOfBounds), // NaN
87 }
88 }
89 }
90 };
91}
92
93impl_from_float_for_fbig!(f32);
94impl_from_float_for_fbig!(f64);
95
96impl<R: Round, const B: Word> FBig<R, B> {
97 /// Convert the float number to base 10 (with decimal exponents) rounding to even
98 /// and tying away from zero.
99 ///
100 /// It's equivalent to `self.with_rounding::<HalfAway>().with_base::<10>()`.
101 /// The output is directly of type [DBig][crate::DBig].
102 ///
103 /// See [with_base()][Self::with_base] for the precision behavior.
104 ///
105 /// # Examples
106 ///
107 /// ```
108 /// # use core::str::FromStr;
109 /// # use dashu_base::ParseError;
110 /// # use dashu_float::{FBig, DBig};
111 /// use dashu_base::Approximation::*;
112 /// use dashu_float::round::Rounding::*;
113 ///
114 /// type Real = FBig;
115 ///
116 /// assert_eq!(
117 /// Real::from_str("0x1234")?.to_decimal(),
118 /// Exact(DBig::from_str("4660")?)
119 /// );
120 /// assert_eq!(
121 /// Real::from_str("0x12.34")?.to_decimal(),
122 /// Inexact(DBig::from_str("18.20")?, NoOp)
123 /// );
124 /// assert_eq!(
125 /// Real::from_str("0x1.234p-4")?.to_decimal(),
126 /// Inexact(DBig::from_str("0.07111")?, AddOne)
127 /// );
128 /// # Ok::<(), ParseError>(())
129 /// ```
130 ///
131 /// # Panics
132 ///
133 /// Panics if the associated context has unlimited precision and the conversion
134 /// cannot be performed losslessly.
135 #[inline]
136 pub fn to_decimal(&self) -> Rounded<FBig<HalfAway, 10>> {
137 self.clone().with_rounding().with_base::<10>()
138 }
139
140 /// Convert the float number to base 2 (with binary exponents) rounding towards zero.
141 ///
142 /// It's equivalent to `self.with_rounding::<Zero>().with_base::<2>()`.
143 ///
144 /// See [with_base()][Self::with_base] for the precision and rounding behavior.
145 ///
146 /// # Examples
147 ///
148 /// ```
149 /// # use core::str::FromStr;
150 /// # use dashu_base::ParseError;
151 /// # use dashu_float::{FBig, DBig};
152 /// use dashu_base::Approximation::*;
153 /// use dashu_float::round::{mode::HalfAway, Rounding::*};
154 ///
155 /// type Real = FBig;
156 ///
157 /// assert_eq!(
158 /// DBig::from_str("1234")?.to_binary(),
159 /// Exact(Real::from_str("0x4d2")?)
160 /// );
161 /// assert_eq!(
162 /// DBig::from_str("12.34")?.to_binary(),
163 /// Inexact(Real::from_str("0xc.57")?, NoOp)
164 /// );
165 /// assert_eq!(
166 /// DBig::from_str("1.234e-1")?.to_binary(),
167 /// Inexact(Real::from_str("0x1.f97p-4")?, NoOp)
168 /// );
169 /// # Ok::<(), ParseError>(())
170 /// ```
171 ///
172 /// # Panics
173 ///
174 /// Panics if the associated context has unlimited precision and the conversion
175 /// cannot be performed losslessly.
176 #[inline]
177 pub fn to_binary(&self) -> Rounded<FBig<Zero, 2>> {
178 self.clone().with_rounding().with_base::<2>()
179 }
180
181 /// Explicitly change the precision of the float number.
182 ///
183 /// If the given precision is less than the current value in the context,
184 /// it will be rounded with the rounding mode specified by the generic parameter.
185 ///
186 /// # Examples
187 ///
188 /// ```rust
189 /// # use core::str::FromStr;
190 /// # use dashu_base::ParseError;
191 /// # use dashu_float::{FBig, DBig};
192 /// use dashu_base::Approximation::*;
193 /// use dashu_float::round::{mode::HalfAway, Rounding::*};
194 ///
195 /// let a = DBig::from_str("2.345")?;
196 /// assert_eq!(a.precision(), 4);
197 /// assert_eq!(
198 /// a.clone().with_precision(3),
199 /// Inexact(DBig::from_str("2.35")?, AddOne)
200 /// );
201 /// assert_eq!(
202 /// a.clone().with_precision(5),
203 /// Exact(DBig::from_str("2.345")?)
204 /// );
205 /// # Ok::<(), ParseError>(())
206 /// ```
207 #[inline]
208 pub fn with_precision(self, precision: usize) -> Rounded<Self> {
209 let new_context = Context::new(precision);
210
211 // shrink if necessary
212 let repr = if self.context.precision > precision {
213 // it also handles unlimited precision
214 new_context.repr_round(self.repr)
215 } else {
216 Exact(self.repr)
217 };
218
219 repr.map(|v| Self::new(v, new_context))
220 }
221
222 /// Explicitly change the rounding mode of the number.
223 ///
224 /// This operation doesn't modify the underlying representation, it only changes
225 /// the rounding mode in the context.
226 ///
227 /// # Examples
228 ///
229 /// ```rust
230 /// # use core::str::FromStr;
231 /// # use dashu_base::ParseError;
232 /// # use dashu_float::{FBig, DBig};
233 /// use dashu_base::Approximation::*;
234 /// use dashu_float::round::{mode::{HalfAway, Zero}, Rounding::*};
235 ///
236 /// type DBigHalfAway = DBig;
237 /// type DBigZero = FBig::<Zero, 10>;
238 ///
239 /// let a = DBigHalfAway::from_str("2.345")?;
240 /// let b = DBigZero::from_str("2.345")?;
241 /// assert_eq!(a.with_rounding::<Zero>(), b);
242 /// # Ok::<(), ParseError>(())
243 /// ```
244 #[inline]
245 pub fn with_rounding<NewR: Round>(self) -> FBig<NewR, B> {
246 FBig {
247 repr: self.repr,
248 context: Context::new(self.context.precision),
249 }
250 }
251
252 /// Explicitly change the base of the float number.
253 ///
254 /// This function internally calls [with_base_and_precision][Self::with_base_and_precision].
255 /// The precision of the result number will be calculated in such a way that the new
256 /// limit of the significand is less than or equal to before. That is, the new precision
257 /// will be the max integer such that
258 ///
259 /// `NewB ^ new_precision <= B ^ old_precision`
260 ///
261 /// If any rounding happens during the conversion, it follows the rounding mode specified
262 /// by the generic parameter.
263 ///
264 /// # Examples
265 ///
266 /// ```rust
267 /// # use core::str::FromStr;
268 /// # use dashu_base::ParseError;
269 /// # use dashu_float::{FBig, DBig};
270 /// use dashu_base::Approximation::*;
271 /// use dashu_float::round::{mode::Zero, Rounding::*};
272 ///
273 /// type FBin = FBig;
274 /// type FDec = FBig<Zero, 10>;
275 /// type FHex = FBig<Zero, 16>;
276 ///
277 /// let a = FBin::from_str("0x1.234")?; // 0x1234 * 2^-12
278 /// assert_eq!(
279 /// a.clone().with_base::<10>(),
280 /// // 1.1376953125 rounded towards zero
281 /// Inexact(FDec::from_str("1.137")?, NoOp)
282 /// );
283 /// assert_eq!(
284 /// a.clone().with_base::<16>(),
285 /// // conversion is exact when the new base is a power of the old base
286 /// Exact(FHex::from_str("1.234")?)
287 /// );
288 /// # Ok::<(), ParseError>(())
289 /// ```
290 ///
291 /// # Panics
292 ///
293 /// Panics if the associated context has unlimited precision and the conversion
294 /// cannot be performed losslessly.
295 #[inline]
296 #[allow(non_upper_case_globals)]
297 pub fn with_base<const NewB: Word>(self) -> Rounded<FBig<R, NewB>> {
298 // if self.context.precision is zero, then precision is also zero
299 let precision =
300 Repr::<B>::BASE.pow(self.context.precision).log2_bounds().0 / NewB.log2_bounds().1;
301 self.with_base_and_precision(precision as usize)
302 }
303
304 /// Explicitly change the base of the float number with given precision (under the new base).
305 ///
306 /// Infinities are mapped to infinities inexactly, the error will be [NoOp][Rounding::NoOp].
307 ///
308 /// Conversion for float numbers with unlimited precision is only allowed in following cases:
309 /// - The number is infinite
310 /// - The new base NewB is a power of B
311 /// - B is a power of the new base NewB
312 ///
313 /// # Examples
314 ///
315 /// ```rust
316 /// # use core::str::FromStr;
317 /// # use dashu_base::ParseError;
318 /// # use dashu_float::{FBig, DBig};
319 /// use dashu_base::Approximation::*;
320 /// use dashu_float::round::{mode::Zero, Rounding::*};
321 ///
322 /// type FBin = FBig;
323 /// type FDec = FBig<Zero, 10>;
324 /// type FHex = FBig<Zero, 16>;
325 ///
326 /// let a = FBin::from_str("0x1.234")?; // 0x1234 * 2^-12
327 /// assert_eq!(
328 /// a.clone().with_base_and_precision::<10>(8),
329 /// // 1.1376953125 rounded towards zero
330 /// Inexact(FDec::from_str("1.1376953")?, NoOp)
331 /// );
332 /// assert_eq!(
333 /// a.clone().with_base_and_precision::<16>(8),
334 /// // conversion can be exact when the new base is a power of the old base
335 /// Exact(FHex::from_str("1.234")?)
336 /// );
337 /// assert_eq!(
338 /// a.clone().with_base_and_precision::<16>(2),
339 /// // but the conversion is still inexact if the target precision is smaller
340 /// Inexact(FHex::from_str("1.2")?, NoOp)
341 /// );
342 /// # Ok::<(), ParseError>(())
343 /// ```
344 ///
345 /// # Panics
346 ///
347 /// Panics if the associated context has unlimited precision and the conversion
348 /// cannot be performed losslessly.
349 #[allow(non_upper_case_globals)]
350 #[inline]
351 pub fn with_base_and_precision<const NewB: Word>(
352 self,
353 precision: usize,
354 ) -> Rounded<FBig<R, NewB>> {
355 let context = Context::<R>::new(precision);
356 context
357 .convert_base(self.repr)
358 .map(|repr| FBig::new(repr, context))
359 }
360
361 /// Convert the float number to integer with the given rounding mode.
362 ///
363 /// # Warning
364 ///
365 /// If the float number has a very large exponent, it will be evaluated and result
366 /// in allocating an huge integer and it might eat up all your memory.
367 ///
368 /// To get a rough idea of how big the number is, it's recommended to use [EstimatedLog2].
369 ///
370 /// # Examples
371 ///
372 /// ```
373 /// # use core::str::FromStr;
374 /// # use dashu_base::ParseError;
375 /// # use dashu_float::{FBig, DBig};
376 /// use dashu_base::Approximation::*;
377 /// use dashu_float::round::Rounding::*;
378 ///
379 /// assert_eq!(
380 /// DBig::from_str("1234")?.to_int(),
381 /// Exact(1234.into())
382 /// );
383 /// assert_eq!(
384 /// DBig::from_str("1.234e6")?.to_int(),
385 /// Exact(1234000.into())
386 /// );
387 /// assert_eq!(
388 /// DBig::from_str("1.234")?.to_int(),
389 /// Inexact(1.into(), NoOp)
390 /// );
391 /// # Ok::<(), ParseError>(())
392 /// ```
393 ///
394 /// # Panics
395 ///
396 /// Panics if the number is infinte
397 pub fn to_int(&self) -> Rounded<IBig> {
398 assert_finite(&self.repr);
399
400 // shortcut when the number is already an integer
401 if self.repr.exponent >= 0 {
402 return Exact(shl_digits::<B>(&self.repr.significand, self.repr.exponent as usize));
403 }
404
405 let (hi, lo, precision) = self.split_at_point_internal();
406 let adjust = R::round_fract::<B>(&hi, lo, precision);
407 Inexact(hi + adjust, adjust)
408 }
409
410 /// Convert the float number to [f32] with the rounding mode associated with the type.
411 ///
412 /// Note that the conversion is inexact even if the number is infinite.
413 ///
414 /// # Examples
415 ///
416 /// ```
417 /// # use core::str::FromStr;
418 /// # use dashu_base::ParseError;
419 /// # use dashu_float::DBig;
420 /// assert_eq!(DBig::from_str("1.234")?.to_f32().value(), 1.234);
421 /// assert_eq!(DBig::INFINITY.to_f32().value(), f32::INFINITY);
422 /// # Ok::<(), ParseError>(())
423 /// ```
424 #[inline]
425 pub fn to_f32(&self) -> Rounded<f32> {
426 if self.repr.is_infinite() {
427 return Inexact(self.sign() * f32::INFINITY, Rounding::NoOp);
428 }
429
430 let context = Context::<R>::new(24);
431 if B != 2 {
432 let rounded: Rounded<Repr<2>> = context.convert_base(self.repr.clone());
433 rounded.and_then(|v| v.into_f32_internal())
434 } else {
435 context
436 .repr_round_ref(&self.repr)
437 .and_then(|v| v.into_f32_internal())
438 }
439 }
440
441 /// Convert the float number to [f64] with [HalfEven] rounding mode regardless of the mode associated with this number.
442 ///
443 /// Note that the conversion is inexact even if the number is infinite.
444 ///
445 /// # Examples
446 ///
447 /// ```
448 /// # use core::str::FromStr;
449 /// # use dashu_base::ParseError;
450 /// # use dashu_float::DBig;
451 /// assert_eq!(DBig::from_str("1.234")?.to_f64().value(), 1.234);
452 /// assert_eq!(DBig::INFINITY.to_f64().value(), f64::INFINITY);
453 /// # Ok::<(), ParseError>(())
454 /// ```
455 #[inline]
456 pub fn to_f64(&self) -> Rounded<f64> {
457 if self.repr.is_infinite() {
458 return Inexact(self.sign() * f64::INFINITY, Rounding::NoOp);
459 }
460
461 let context = Context::<HalfEven>::new(53);
462 if B != 2 {
463 let rounded: Rounded<Repr<2>> = context.convert_base(self.repr.clone());
464 rounded.and_then(|v| v.into_f64_internal())
465 } else {
466 context
467 .repr_round_ref(&self.repr)
468 .and_then(|v| v.into_f64_internal())
469 }
470 }
471}
472
473impl<R: Round> Context<R> {
474 // Convert the [Repr] from base B to base NewB, with the precision under the target base from this context.
475 #[allow(non_upper_case_globals)]
476 fn convert_base<const B: Word, const NewB: Word>(&self, repr: Repr<B>) -> Rounded<Repr<NewB>> {
477 // shortcut if NewB is the same as B
478 if NewB == B {
479 return Exact(Repr {
480 significand: repr.significand,
481 exponent: repr.exponent,
482 });
483 }
484
485 // shortcut for infinities, no rounding happens but the result is inexact
486 if repr.is_infinite() {
487 return Inexact(
488 Repr {
489 significand: repr.significand,
490 exponent: repr.exponent,
491 },
492 Rounding::NoOp,
493 );
494 }
495
496 if NewB > B {
497 // shortcut if NewB is a power of B
498 let n = ilog_exact(NewB, B);
499 if n > 1 {
500 let (exp, rem) = repr.exponent.div_rem_euclid(n as isize);
501 let signif = repr.significand * B.pow(rem as u32);
502 let repr = Repr::new(signif, exp);
503 return self.repr_round(repr);
504 }
505 } else {
506 // shortcut if B is a power of NewB
507 let n = ilog_exact(B, NewB);
508 if n > 1 {
509 let exp = repr.exponent * n as isize;
510 return Exact(Repr::new(repr.significand, exp));
511 }
512 }
513
514 // if the base cannot be converted losslessly, the precision must be set
515 if self.precision == 0 {
516 panic_unlimited_precision();
517 }
518
519 // XXX: there's a potential optimization: if B is a multiple of NewB, then the factor B
520 // should be trivially removed first, but this requires full support of const generics.
521
522 // choose a exponent threshold such that number with exponent smaller than this value
523 // will be converted by directly evaluating the power. The threshold here is chosen such
524 // that the power under base 10 will fit in a double word.
525 const THRESHOLD_SMALL_EXP: isize = (Word::BITS as f32 * 0.60206) as isize; // word bits * 2 / log2(10)
526 if repr.exponent.abs() <= THRESHOLD_SMALL_EXP {
527 // if the exponent is small enough, directly evaluate the exponent
528 if repr.exponent >= 0 {
529 let signif = repr.significand * Repr::<B>::BASE.pow(repr.exponent as usize);
530 Exact(Repr::new(signif, 0))
531 } else {
532 let num = Repr::new(repr.significand, 0);
533 let den = Repr::new(Repr::<B>::BASE.pow(-repr.exponent as usize).into(), 0);
534 self.repr_div(num, den)
535 }
536 } else {
537 // if the exponent is large, then we first estimate the result exponent as floor(exponent * log(B) / log(NewB)),
538 // then the fractional part is multiplied with the original significand
539 let work_context = Context::<R>::new(2 * self.precision); // double the precision to get the precise logarithm
540 let new_exp = repr.exponent
541 * work_context
542 .ln(&Repr::new(Repr::<B>::BASE.into(), 0))
543 .value();
544 let (exponent, rem) = new_exp.div_rem_euclid(work_context.ln_base::<NewB>());
545 let exponent: isize = exponent.try_into().unwrap();
546 let exp_rem = rem.exp();
547 let significand = repr.significand * exp_rem.repr.significand;
548 let repr = Repr::new(significand, exponent + exp_rem.repr.exponent);
549 self.repr_round(repr)
550 }
551 }
552}
553
554impl<const B: Word> Repr<B> {
555 // this method requires that the representation is already rounded to 24 binary bits
556 fn into_f32_internal(self) -> Rounded<f32> {
557 assert!(B == 2);
558 debug_assert!(self.is_finite());
559 debug_assert!(self.significand.bit_len() <= 24);
560
561 let sign = self.sign();
562 let man24: i32 = self.significand.try_into().unwrap();
563 if self.exponent >= 128 {
564 // max f32 = 2^128 * (1 - 2^-24)
565 match sign {
566 Sign::Positive => Inexact(f32::INFINITY, Rounding::AddOne),
567 Sign::Negative => Inexact(f32::NEG_INFINITY, Rounding::SubOne),
568 }
569 } else if self.exponent < -149 - 24 {
570 // min f32 = 2^-149
571 Inexact(sign * 0f32, Rounding::NoOp)
572 } else {
573 match f32::encode(man24, self.exponent as i16) {
574 Exact(v) => Exact(v),
575 // this branch only happens when the result underflows
576 Inexact(v, _) => Inexact(v, Rounding::NoOp),
577 }
578 }
579 }
580
581 /// Convert the float number representation to a [f32] with the default IEEE 754 rounding mode.
582 ///
583 /// The default IEEE 754 rounding mode is [HalfEven] (rounding to nearest, ties to even). To convert
584 /// the float number with a specific rounding mode, please use [FBig::to_f32].
585 ///
586 /// # Examples
587 ///
588 /// ```
589 /// # use dashu_base::Approximation::*;
590 /// # use dashu_float::{Repr, round::Rounding::*};
591 /// assert_eq!(Repr::<2>::one().to_f32(), Exact(1.0));
592 /// assert_eq!(Repr::<10>::infinity().to_f32(), Inexact(f32::INFINITY, NoOp));
593 /// ```
594 #[inline]
595 pub fn to_f32(&self) -> Rounded<f32> {
596 // Note: the implementation here should be kept consistent with FBig::to_f32
597
598 if self.is_infinite() {
599 return Inexact(self.sign() * f32::INFINITY, Rounding::NoOp);
600 }
601
602 let context = Context::<HalfEven>::new(24);
603 if B != 2 {
604 let rounded: Rounded<Repr<2>> = context.convert_base(self.clone());
605 rounded.and_then(|v| v.into_f32_internal())
606 } else {
607 context
608 .repr_round_ref(self)
609 .and_then(|v| v.into_f32_internal())
610 }
611 }
612
613 // this method requires that the representation is already rounded to 53 binary bits
614 fn into_f64_internal(self) -> Rounded<f64> {
615 assert!(B == 2);
616 debug_assert!(self.is_finite());
617 debug_assert!(self.significand.bit_len() <= 53);
618
619 let sign = self.sign();
620 let man53: i64 = self.significand.try_into().unwrap();
621 if self.exponent >= 1024 {
622 // max f64 = 2^1024 × (1 − 2^−53)
623 match sign {
624 Sign::Positive => Inexact(f64::INFINITY, Rounding::AddOne),
625 Sign::Negative => Inexact(f64::NEG_INFINITY, Rounding::SubOne),
626 }
627 } else if self.exponent < -1074 - 53 {
628 // min f64 = 2^-1074
629 Inexact(sign * 0f64, Rounding::NoOp)
630 } else {
631 match f64::encode(man53, self.exponent as i16) {
632 Exact(v) => Exact(v),
633 // this branch only happens when the result underflows
634 Inexact(v, _) => Inexact(v, Rounding::NoOp),
635 }
636 }
637 }
638
639 /// Convert the float number representation to a [f64] with the default IEEE 754 rounding mode.
640 ///
641 /// The default IEEE 754 rounding mode is [HalfEven] (rounding to nearest, ties to even). To convert
642 /// the float number with a specific rounding mode, please use [FBig::to_f64].
643 ///
644 /// # Examples
645 ///
646 /// ```
647 /// # use dashu_base::Approximation::*;
648 /// # use dashu_float::{Repr, round::Rounding::*};
649 /// assert_eq!(Repr::<2>::one().to_f64(), Exact(1.0));
650 /// assert_eq!(Repr::<10>::infinity().to_f64(), Inexact(f64::INFINITY, NoOp));
651 /// ```
652 #[inline]
653 pub fn to_f64(&self) -> Rounded<f64> {
654 // Note: the implementation here should be kept consistent with FBig::to_f64
655
656 if self.is_infinite() {
657 return Inexact(self.sign() * f64::INFINITY, Rounding::NoOp);
658 }
659
660 let context = Context::<HalfEven>::new(53);
661 if B != 2 {
662 let rounded: Rounded<Repr<2>> = context.convert_base(self.clone());
663 rounded.and_then(|v| v.into_f64_internal())
664 } else {
665 context
666 .repr_round_ref(self)
667 .and_then(|v| v.into_f64_internal())
668 }
669 }
670
671 /// Convert the float number representation to a [IBig].
672 ///
673 /// The fractional part is always rounded to zero. To convert with other rounding modes,
674 /// please use [FBig::to_int()].
675 ///
676 /// # Warning
677 ///
678 /// If the float number has a very large exponent, it will be evaluated and result
679 /// in allocating an huge integer and it might eat up all your memory.
680 ///
681 /// To get a rough idea of how big the number is, it's recommended to use [EstimatedLog2].
682 ///
683 /// # Examples
684 ///
685 /// ```
686 /// # use dashu_base::Approximation::*;
687 /// # use dashu_int::IBig;
688 /// # use dashu_float::{Repr, round::Rounding::*};
689 /// assert_eq!(Repr::<2>::neg_one().to_int(), Exact(IBig::NEG_ONE));
690 /// ```
691 ///
692 /// # Panics
693 ///
694 /// Panics if the number is infinte.
695 pub fn to_int(&self) -> Rounded<IBig> {
696 assert_finite(self);
697
698 if self.exponent >= 0 {
699 // the number is already an integer
700 Exact(shl_digits::<B>(&self.significand, self.exponent as usize))
701 } else if self.smaller_than_one() {
702 // the number is definitely smaller than
703 Inexact(IBig::ZERO, Rounding::NoOp)
704 } else {
705 let int = shr_digits::<B>(&self.significand, (-self.exponent) as usize);
706 Inexact(int, Rounding::NoOp)
707 }
708 }
709}
710
711impl<const B: Word> From<UBig> for Repr<B> {
712 #[inline]
713 fn from(n: UBig) -> Self {
714 Self::new(n.into(), 0)
715 }
716}
717impl<R: Round, const B: Word> From<UBig> for FBig<R, B> {
718 #[inline]
719 fn from(n: UBig) -> Self {
720 Self::from_parts(n.into(), 0)
721 }
722}
723
724impl<const B: Word> From<IBig> for Repr<B> {
725 #[inline]
726 fn from(n: IBig) -> Self {
727 Self::new(n, 0)
728 }
729}
730impl<R: Round, const B: Word> From<IBig> for FBig<R, B> {
731 #[inline]
732 fn from(n: IBig) -> Self {
733 Self::from_parts(n, 0)
734 }
735}
736
737impl<R: Round, const B: Word> TryFrom<FBig<R, B>> for IBig {
738 type Error = ConversionError;
739
740 #[inline]
741 fn try_from(value: FBig<R, B>) -> Result<Self, Self::Error> {
742 if value.repr.is_infinite() {
743 Err(ConversionError::OutOfBounds)
744 } else if value.repr.exponent < 0 {
745 Err(ConversionError::LossOfPrecision)
746 } else {
747 let mut int = value.repr.significand;
748 shl_digits_in_place::<B>(&mut int, value.repr.exponent as usize);
749 Ok(int)
750 }
751 }
752}
753
754impl<R: Round, const B: Word> TryFrom<FBig<R, B>> for UBig {
755 type Error = ConversionError;
756
757 #[inline]
758 fn try_from(value: FBig<R, B>) -> Result<Self, Self::Error> {
759 let int: IBig = value.try_into()?;
760 int.try_into()
761 }
762}
763
764macro_rules! fbig_unsigned_conversions {
765 ($($t:ty)*) => {$(
766 impl<const B: Word> From<$t> for Repr<B> {
767 #[inline]
768 fn from(value: $t) -> Repr<B> {
769 UBig::from(value).into()
770 }
771 }
772 impl<R: Round, const B: Word> From<$t> for FBig<R, B> {
773 #[inline]
774 fn from(value: $t) -> FBig<R, B> {
775 UBig::from(value).into()
776 }
777 }
778
779 impl<const B: Word> TryFrom<Repr<B>> for $t {
780 type Error = ConversionError;
781
782 fn try_from(value: Repr<B>) -> Result<Self, Self::Error> {
783 if value.sign() == Sign::Negative || value.is_infinite() {
784 Err(ConversionError::OutOfBounds)
785 } else {
786 let (log2_lb, _) = value.log2_bounds();
787 if log2_lb >= <$t>::BITS as f32 {
788 Err(ConversionError::OutOfBounds)
789 } else if value.exponent < 0 {
790 Err(ConversionError::LossOfPrecision)
791 } else {
792 shl_digits::<B>(&value.significand, value.exponent as usize).try_into()
793 }
794 }
795 }
796 }
797 impl<R: Round, const B: Word> TryFrom<FBig<R, B>> for $t {
798 type Error = ConversionError;
799
800 #[inline]
801 fn try_from(value: FBig<R, B>) -> Result<Self, Self::Error> {
802 value.repr.try_into()
803 }
804 }
805 )*};
806}
807fbig_unsigned_conversions!(u8 u16 u32 u64 u128 usize);
808
809macro_rules! fbig_signed_conversions {
810 ($($t:ty)*) => {$(
811 impl<R: Round, const B: Word> From<$t> for FBig<R, B> {
812 #[inline]
813 fn from(value: $t) -> FBig<R, B> {
814 IBig::from(value).into()
815 }
816 }
817
818 impl<R: Round, const B: Word> TryFrom<FBig<R, B>> for $t {
819 type Error = ConversionError;
820
821 fn try_from(value: FBig<R, B>) -> Result<Self, Self::Error> {
822 if value.repr.is_infinite() {
823 Err(ConversionError::OutOfBounds)
824 } else {
825 let (log2_lb, _) = value.repr.log2_bounds();
826 if log2_lb >= <$t>::BITS as f32 {
827 Err(ConversionError::OutOfBounds)
828 } else if value.repr.exponent < 0 {
829 Err(ConversionError::LossOfPrecision)
830 } else {
831 shl_digits::<B>(&value.repr.significand, value.repr.exponent as usize).try_into()
832 }
833 }
834 }
835 }
836 )*};
837}
838fbig_signed_conversions!(i8 i16 i32 i64 i128 isize);
839
840macro_rules! impl_from_fbig_for_float {
841 ($t:ty, $method:ident) => {
842 impl TryFrom<Repr<2>> for $t {
843 type Error = ConversionError;
844
845 #[inline]
846 fn try_from(value: Repr<2>) -> Result<Self, Self::Error> {
847 if value.is_infinite() {
848 Err(ConversionError::LossOfPrecision)
849 } else {
850 match value.$method() {
851 Exact(v) => Ok(v),
852 Inexact(v, _) => {
853 if v.is_infinite() {
854 Err(ConversionError::OutOfBounds)
855 } else {
856 Err(ConversionError::LossOfPrecision)
857 }
858 }
859 }
860 }
861 }
862 }
863
864 impl<R: Round> TryFrom<FBig<R, 2>> for $t {
865 type Error = ConversionError;
866
867 #[inline]
868 fn try_from(value: FBig<R, 2>) -> Result<Self, Self::Error> {
869 // this method is the same as the one for Repr, but it has to be re-implemented
870 // because the rounding behavior of to_32/to_64 is different.
871 if value.repr.is_infinite() {
872 Err(ConversionError::LossOfPrecision)
873 } else {
874 match value.$method() {
875 Exact(v) => Ok(v),
876 Inexact(v, _) => {
877 if v.is_infinite() {
878 Err(ConversionError::OutOfBounds)
879 } else {
880 Err(ConversionError::LossOfPrecision)
881 }
882 }
883 }
884 }
885 }
886 }
887 };
888}
889impl_from_fbig_for_float!(f32, to_f32);
890impl_from_fbig_for_float!(f64, to_f64);