tests in the steel decimal crate with serious issue fixed
This commit is contained in:
@@ -75,23 +75,32 @@ fn parse_scientific_notation(s: &str) -> Result<Decimal, String> {
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
// Basic arithmetic operations (now precision-aware)
|
||||
// Basic arithmetic operations
|
||||
pub fn decimal_add(a: String, b: String) -> Result<String, String> {
|
||||
let a_dec = parse_decimal(&a)?;
|
||||
let b_dec = parse_decimal(&b)?;
|
||||
Ok(format_result(a_dec + b_dec))
|
||||
|
||||
a_dec.checked_add(b_dec)
|
||||
.map(|result| format_result(result))
|
||||
.ok_or_else(|| "Addition overflowed".to_string())
|
||||
}
|
||||
|
||||
pub fn decimal_sub(a: String, b: String) -> Result<String, String> {
|
||||
let a_dec = parse_decimal(&a)?;
|
||||
let b_dec = parse_decimal(&b)?;
|
||||
Ok(format_result(a_dec - b_dec))
|
||||
|
||||
a_dec.checked_sub(b_dec)
|
||||
.map(|result| format_result(result))
|
||||
.ok_or_else(|| "Subtraction overflowed".to_string())
|
||||
}
|
||||
|
||||
pub fn decimal_mul(a: String, b: String) -> Result<String, String> {
|
||||
let a_dec = parse_decimal(&a)?;
|
||||
let b_dec = parse_decimal(&b)?;
|
||||
Ok(format_result(a_dec * b_dec))
|
||||
|
||||
a_dec.checked_mul(b_dec)
|
||||
.map(|result| format_result(result))
|
||||
.ok_or_else(|| "Multiplication overflowed".to_string())
|
||||
}
|
||||
|
||||
pub fn decimal_div(a: String, b: String) -> Result<String, String> {
|
||||
@@ -102,7 +111,9 @@ pub fn decimal_div(a: String, b: String) -> Result<String, String> {
|
||||
return Err("Division by zero".to_string());
|
||||
}
|
||||
|
||||
Ok(format_result(a_dec / b_dec))
|
||||
a_dec.checked_div(b_dec)
|
||||
.map(|result| format_result(result))
|
||||
.ok_or_else(|| "Division overflowed".to_string())
|
||||
}
|
||||
|
||||
// Precision control functions
|
||||
|
||||
Reference in New Issue
Block a user