|
1 | | -use fastnum::{dec128 as d, D128, D256}; |
| 1 | +use fastnum::{dec128 as d, D128}; |
2 | 2 | use crate::Number; |
3 | 3 |
|
4 | 4 | #[derive(Clone, Copy, PartialEq, Debug)] |
@@ -754,36 +754,28 @@ pub fn modulo(left: Number, right: Number) -> Result<Number, String> { |
754 | 754 | } |
755 | 755 | } |
756 | 756 |
|
757 | | -fn do_pow(left: D128, right: D128) -> D128 { |
758 | | - // Do pow with d256 for higher accuracy |
759 | | - let left = D256::try_from(left.transmute()).unwrap(); |
760 | | - let right = D256::try_from(right.transmute()).unwrap(); |
761 | | - let result = left.pow(right); |
762 | | - let result_d128: D128 = result.transmute(); |
763 | | - result_d128 |
764 | | -} |
765 | | - |
766 | 757 | /// Returns a [`Number`] to the power of another [`Number`] |
767 | 758 | /// |
768 | 759 | /// - If you take [`Length`] to the power of [`NoType`], the result has a unit of [`Area`]. |
769 | 760 | /// - If you take [`Length`] to the power of [`Length`], the result has a unit of [`Area`] |
770 | 761 | /// - If you take [`Length`] to the power of [`Area`], the result has a unit of [`Volume`] |
771 | 762 | /// - etc. |
772 | 763 | pub fn pow(left: Number, right: Number) -> Result<Number, String> { |
| 764 | + // I tried converting `right` to use powi, but somehow that was slower |
773 | 765 | let lcat = left.unit.category(); |
774 | 766 | let rcat = left.unit.category(); |
775 | 767 | if left.unit == NoUnit && right.unit == NoUnit { |
776 | 768 | // 3 ^ 2 |
777 | | - Ok(Number::new(do_pow(left.value, right.value), left.unit)) |
| 769 | + Ok(Number::new(left.value.pow(right.value), left.unit)) |
778 | 770 | } else if right.value == d!(1) && right.unit == NoUnit { |
779 | 771 | Ok(left) |
780 | 772 | } else if lcat == Length && right.unit == NoUnit && right.value == d!(2) { |
781 | 773 | // x km ^ 2 |
782 | | - let result = do_pow(left.value * left.unit.weight(), right.value); |
| 774 | + let result = (left.value * left.unit.weight()).pow(right.value); |
783 | 775 | Ok(to_ideal_unit(Number::new(result, SquareMillimeter))) |
784 | 776 | } else if lcat == Length && right.unit == NoUnit && right.value == d!(3) { |
785 | 777 | // x km ^ 3 |
786 | | - let result = do_pow(left.value * left.unit.weight(), right.value); |
| 778 | + let result = (left.value * left.unit.weight()).pow(right.value); |
787 | 779 | Ok(to_ideal_unit(Number::new(result, CubicMillimeter))) |
788 | 780 | } else if lcat == Length && rcat == Length && right.value == d!(1) { |
789 | 781 | // x km ^ 1 km |
|
0 commit comments