Remove useless custon floatingpoint and use libm instead

This commit is contained in:
Guillaume Gielly
2024-12-26 19:10:57 +01:00
parent f20c0552dd
commit 4bcf779119
4 changed files with 32 additions and 62 deletions
+6 -48
View File
@@ -6,6 +6,8 @@
use alloc::string::{String, ToString};
use core::{ffi::CStr, ptr, slice, str};
use libm::{fabs, floor, pow, round, trunc};
extern crate alloc;
mod strfmon;
@@ -60,50 +62,6 @@ struct FormatFlags {
international: bool,
}
/// Use our own floating point implementation
/// TODO : maybe we can use num-traits crate
#[inline]
fn my_fabs(x: f64) -> f64 {
if x < 0.0 {
-x
} else {
x
}
}
#[inline]
fn my_floor(x: f64) -> f64 {
let i = x as i64;
if x < 0.0 && x != i as f64 {
(i - 1) as f64
} else {
i as f64
}
}
#[inline]
fn my_trunc(x: f64) -> f64 {
if x < 0.0 {
-my_floor(-x)
} else {
my_floor(x)
}
}
#[inline]
fn my_round(x: f64) -> f64 {
my_floor(x + 0.5)
}
#[inline]
fn my_pow10(n: usize) -> f64 {
let mut result = 1.0;
for _ in 0..n {
result *= 10.0;
}
result
}
/// Formats a monetary value according to the current locale.
fn apply_grouping(int_str: &str, monetary: &LocaleMonetaryInfo) -> String {
let mut grouped = String::with_capacity(int_str.len() * 2);
@@ -131,15 +89,15 @@ fn apply_grouping(int_str: &str, monetary: &LocaleMonetaryInfo) -> String {
/// Safe handling of large monetary values. Returns None if the value is too large to format
fn format_value_parts(value: f64, frac_digits: usize) -> Option<(String, i64)> {
let abs_value = my_fabs(value);
let abs_value = fabs(value);
if abs_value > (i64::MAX as f64) {
// Check if the value is too large to format
return None;
}
let int_part = my_trunc(abs_value) as i64;
let scale = my_pow10(frac_digits);
let frac_part = my_round((abs_value - int_part as f64) * scale) as i64;
let int_part = trunc(abs_value) as i64;
let scale = pow(10.0, frac_digits as f64);
let frac_part = round((abs_value - int_part as f64) * scale) as i64;
Some((int_part.to_string(), frac_part))
}
+8 -8
View File
@@ -1,8 +1,7 @@
use super::{
my_fabs, my_pow10, my_round, my_trunc, FormatFlags, LocaleMonetaryInfo, DEFAULT_MONETARY,
};
use super::{FormatFlags, LocaleMonetaryInfo, DEFAULT_MONETARY};
use alloc::string::{String, ToString};
use core::{ffi::CStr, ptr, slice, str};
use libm::{fabs, floor, pow, round, trunc};
/*
The strfmon() function formats a monetary value according to the format string format and writes the result to the character
@@ -156,8 +155,8 @@ fn format_monetary(
) -> Option<usize> {
let mut pos = 0; // Initialize the position to 0
let is_negative = value < 0.0; // Check if the value is negative
let abs_value = my_fabs(value); // Get the absolute value of the number
// Check if the international flag is set
let abs_value = fabs(value); // Get the absolute value of the number
let frac_digits = if flags.international {
flags
.right_precision
@@ -168,9 +167,9 @@ fn format_monetary(
.unwrap_or(monetary.frac_digits as usize)
};
let scale = my_pow10(frac_digits);
let mut int_part = my_trunc(abs_value) as i64;
let mut frac_part = my_round((abs_value - int_part as f64) * scale) as i64;
let scale = pow(10.0, frac_digits as f64);
let mut int_part = trunc(abs_value) as i64;
let mut frac_part = round((abs_value - int_part as f64) * scale) as i64;
// Ensure that the fractional part (frac_part) doesnt overflow due to rounding
// when abs_value is very close to the next integer value.
@@ -243,6 +242,7 @@ fn format_monetary(
// Add a space between the currency symbol and the value
let sep_by_space = if is_negative {
//let scale = my_pow10(frac_digits);
monetary.n_sep_by_space
} else {
monetary.p_sep_by_space