mirror of
https://github.com/PaddiM8/kalker.git
synced 2025-06-27 21:21:42 +02:00
Fixed mod.rs errors for when rug is not used, and added some unit tests there
This commit is contained in:
parent
dea2ab9ee2
commit
09c99c9169
@ -49,13 +49,13 @@ impl KalkNum {
|
|||||||
let integer = self.value.clone().trunc();
|
let integer = self.value.clone().trunc();
|
||||||
|
|
||||||
// If it's an integer, there's nothing that would be done to it.
|
// If it's an integer, there's nothing that would be done to it.
|
||||||
if fract == 0 {
|
if fract == 0f64 {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Eg. 0.5 to 1/2
|
// Eg. 0.5 to 1/2
|
||||||
let as_abs_string = self.to_string().trim_start_matches("-").to_string();
|
let as_abs_string = self.to_string().trim_start_matches("-").to_string();
|
||||||
let sign = if self.value < 0 { "-" } else { "" };
|
let sign = if self.value < 0f64 { "-" } else { "" };
|
||||||
let fract_as_string = fract.to_string();
|
let fract_as_string = fract.to_string();
|
||||||
if as_abs_string.starts_with("0.5") {
|
if as_abs_string.starts_with("0.5") {
|
||||||
if as_abs_string.len() == 3 || (as_abs_string.len() > 6 && &as_abs_string[3..5] == "00")
|
if as_abs_string.len() == 3 || (as_abs_string.len() > 6 && &as_abs_string[3..5] == "00")
|
||||||
@ -65,7 +65,7 @@ impl KalkNum {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Eg. 1.33333333 to 1 + 1/3
|
// Eg. 1.33333333 to 1 + 1/3
|
||||||
if fract_as_string.len() >= 5 {
|
if fract_as_string.len() >= 7 {
|
||||||
let first_five_decimals = &fract_as_string[2..7];
|
let first_five_decimals = &fract_as_string[2..7];
|
||||||
if first_five_decimals == "33333" || first_five_decimals == "66666" {
|
if first_five_decimals == "33333" || first_five_decimals == "66666" {
|
||||||
let fraction = match first_five_decimals.as_ref() {
|
let fraction = match first_five_decimals.as_ref() {
|
||||||
@ -74,7 +74,7 @@ impl KalkNum {
|
|||||||
_ => "?",
|
_ => "?",
|
||||||
};
|
};
|
||||||
|
|
||||||
if integer == 0 {
|
if integer == 0f64 {
|
||||||
return Some(format!("{}{}", sign, fraction));
|
return Some(format!("{}{}", sign, fraction));
|
||||||
} else {
|
} else {
|
||||||
let explicit_sign = if sign == "" { "+" } else { "-" };
|
let explicit_sign = if sign == "" { "+" } else { "-" };
|
||||||
@ -102,7 +102,7 @@ impl KalkNum {
|
|||||||
|
|
||||||
/// Basic up/down rounding from 0.00xxx or 0.999xxx or xx.000xxx, etc.
|
/// Basic up/down rounding from 0.00xxx or 0.999xxx or xx.000xxx, etc.
|
||||||
pub fn round(&self) -> Option<KalkNum> {
|
pub fn round(&self) -> Option<KalkNum> {
|
||||||
let sign = if self.value < 0 { -1 } else { 1 };
|
let sign = if self.value < 0f64 { -1f64 } else { 1f64 };
|
||||||
let fract = self.value.clone().abs().fract();
|
let fract = self.value.clone().abs().fract();
|
||||||
let integer = self.value.clone().abs().trunc();
|
let integer = self.value.clone().abs().trunc();
|
||||||
|
|
||||||
@ -196,6 +196,27 @@ mod tests {
|
|||||||
(-0.665f64, None),
|
(-0.665f64, None),
|
||||||
(100f64, None),
|
(100f64, None),
|
||||||
(-100f64, None),
|
(-100f64, None),
|
||||||
|
(1f64, None),
|
||||||
|
(0.12f64, None),
|
||||||
|
(0.1f64, None),
|
||||||
|
(1.2f64, None),
|
||||||
|
(1.23f64, None),
|
||||||
|
(1.234f64, None),
|
||||||
|
(1.2345f64, None),
|
||||||
|
(1.23456f64, None),
|
||||||
|
(1.234567f64, None),
|
||||||
|
(1.2345678f64, None),
|
||||||
|
(1.23456789f64, None),
|
||||||
|
(-0.12f64, None),
|
||||||
|
(-0.1f64, None),
|
||||||
|
(-1.2f64, None),
|
||||||
|
(-1.23f64, None),
|
||||||
|
(-1.234f64, None),
|
||||||
|
(-1.2345f64, None),
|
||||||
|
(-1.23456f64, None),
|
||||||
|
(-1.234567f64, None),
|
||||||
|
(-1.2345678f64, None),
|
||||||
|
(-1.23456789f64, None),
|
||||||
];
|
];
|
||||||
|
|
||||||
for (input, output) in in_out {
|
for (input, output) in in_out {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user