Skip to content

Commit d48bf9c

Browse files
committed
Fix tiny numbers not printing properly with rug floats, closes #147
1 parent 74d787f commit d48bf9c

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

kalk/src/kalk_value/mod.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
pub mod with_rug;
33

44
#[cfg(feature = "rug")]
5-
use rug::{Float, ops::Pow};
5+
use rug::{ops::Pow, Float};
66

77
#[cfg(not(feature = "rug"))]
88
pub mod regular;
@@ -148,7 +148,8 @@ impl ScientificNotation {
148148
value,
149149
exponent: exponent - modulo + 1,
150150
imaginary: self.imaginary,
151-
}.to_string()
151+
}
152+
.to_string()
152153
}
153154
}
154155

@@ -547,7 +548,7 @@ impl KalkValue {
547548
let exponent = value.abs().log10().floor() as i32 + 1;
548549

549550
ScientificNotation {
550-
value: value / (10f64.powf(exponent as f64 - 1f64) as f64),
551+
value: value / (10f64.pow(exponent - 1)),
551552
// I... am not sure what else to do...
552553
exponent,
553554
imaginary: complex_number_type == ComplexNumberType::Imaginary,
@@ -936,7 +937,7 @@ impl KalkValue {
936937
) => {
937938
if self.has_imaginary()
938939
|| imaginary_rhs != &0f64
939-
|| (real < 0f64 && real_rhs < &1f64)
940+
|| (real_rhs > &0f64 && real_rhs < &1f64)
940941
{
941942
let a = real;
942943
let b = imaginary;
@@ -1155,6 +1156,10 @@ pub fn format_number(input: f64) -> String {
11551156

11561157
#[cfg(feature = "rug")]
11571158
pub fn format_number_big(input: &Float) -> String {
1159+
if input.clone().log10() < 0f64 {
1160+
return input.to_f64().to_string();
1161+
}
1162+
11581163
let input_str = input.to_string();
11591164
let mut result = if input_str.contains('.') {
11601165
input_str
@@ -1562,6 +1567,7 @@ mod tests {
15621567
fn test_to_string_pretty() {
15631568
let in_out = vec![
15641569
(float!(0.99999), float!(0.0), "0.99999 ≈ 1"),
1570+
(float!(0.00000001), float!(0.0), "0.00000001 ≈ 10^-8"),
15651571
(float!(-0.99999), float!(0.0), "-0.99999 ≈ -1"),
15661572
(float!(0.0), float!(0.99999), "0.99999i ≈ i"),
15671573
(float!(0.000000001), float!(0.0), "10^-9 ≈ 0"),
@@ -1601,7 +1607,8 @@ mod tests {
16011607
(float!(3.00000000004), float!(0.0), "3"),
16021608
];
16031609
for (real, imaginary, output) in in_out {
1604-
let result = KalkValue::Number(real, imaginary, None).to_string_pretty(ScientificNotationFormat::Normal);
1610+
let result = KalkValue::Number(real, imaginary, None)
1611+
.to_string_pretty(ScientificNotationFormat::Normal);
16051612
assert_eq!(output, result);
16061613
}
16071614
}
@@ -1624,7 +1631,10 @@ mod tests {
16241631
imaginary: false,
16251632
};
16261633

1627-
assert_eq!(sci.to_string_format(ScientificNotationFormat::Engineering), output);
1634+
assert_eq!(
1635+
sci.to_string_format(ScientificNotationFormat::Engineering),
1636+
output
1637+
);
16281638
}
16291639
}
16301640
}

0 commit comments

Comments
 (0)