The trait bound `Integer: LowerExp` is not satisfied
As of rug=1.16.0 is not currently possible to format rug::Integer using {:+e} scientific notation, unless casting to rug::Float first.
The compiler complains: The trait bound Integer: LowerExp is not satisfied
Unsure the most efficient way to implement this trait, but hopefully this should be possible as an extra piece of syntactic sugar.
Example Code
use std::time::Instant;
use rug::{Float, Integer, ops::Pow};
fn main() {
for n in 0..20 {
let time_start = Instant::now();
let value = fermat_number(n);
// let value = Float::with_val(128, value); // BUGFIX: Cast Integer to Float
let time_taken = time_start.elapsed().as_nanos();
println!("{time_taken:>6}ns | fermat_number({n}) = {value:+.2e}");
}
}
fn fermat_number(n: u32) -> Integer {
Integer::from(2).pow( u32::pow(2,n) ) + 1
}
Actual Output with Integer
error[E0277]: the trait bound `Integer: LowerExp` is not satisfied
--> src/bin/fermat_numbers.rs:10:54
|
10 | println!("{time_taken:>6}ns | fermat({n}) = {value:+.2e}");
| ^^^^^ the trait `LowerExp` is not implemented for `Integer`
|
= help: the following other types implement trait `LowerExp`:
&T
&mut T
Complex
Float
Simd<T, LANES>
az::Round<T>
f32
f64
and 12 others
note: required by a bound in `ArgumentV1::<'a>::new_lower_exp`
--> /Users/jamie/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:346:5
|
346 | arg_new!(new_lower_exp, LowerExp);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `ArgumentV1::<'a>::new_lower_exp`
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
Expected Output with Float
141875ns | fermat(0) = +3.0e0
1083ns | fermat(1) = +5.0e0
1000ns | fermat(2) = +1.7e1
833ns | fermat(3) = +2.6e2
833ns | fermat(4) = +6.6e4
833ns | fermat(5) = +4.3e9
875ns | fermat(6) = +1.8e19
875ns | fermat(7) = +3.4e38
875ns | fermat(8) = +1.2e77
3625ns | fermat(9) = +1.3e154
Cargo.toml
[dependencies]
rug = "1.16.0"