Skip to content

Commit 213a335

Browse files
committed
Unify fixed-point format of floating numbers
1 parent 07a857b commit 213a335

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

common/src/float_ops.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ fn format_inf(case: Case) -> String {
107107
inf.to_string()
108108
}
109109

110+
pub fn format_fixed(precision: usize, magnitude: f64, case: Case) -> String {
111+
match magnitude {
112+
magnitude if magnitude.is_finite() => format!("{:.*}", precision, magnitude),
113+
magnitude if magnitude.is_nan() => format_nan(case),
114+
magnitude if magnitude.is_infinite() => format_inf(case),
115+
_ => "".to_string(),
116+
}
117+
}
118+
110119
// Formats floats into Python style exponent notation, by first formatting in Rust style
111120
// exponent notation (`1.0000e0`), then convert to Python style (`1.0000e+00`).
112121
pub fn format_exponent(precision: usize, magnitude: f64, case: Case) -> String {

vm/src/cformat.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,13 @@ impl CFormatSpec {
305305
};
306306

307307
let magnitude_string = match &self.format_type {
308-
CFormatType::Float(CFloatType::PointDecimal(_)) => {
308+
CFormatType::Float(CFloatType::PointDecimal(case)) => {
309+
let case = match case {
310+
CFormatCase::Lowercase => float_ops::Case::Lower,
311+
CFormatCase::Uppercase => float_ops::Case::Upper,
312+
};
309313
let magnitude = num.abs();
310-
format!("{:.*}", precision, magnitude)
314+
float_ops::format_fixed(precision, magnitude, case)
311315
}
312316
CFormatType::Float(CFloatType::Exponent(case)) => {
313317
let case = match case {

vm/src/format.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -347,16 +347,16 @@ impl FormatSpec {
347347
let precision = self.precision.unwrap_or(6);
348348
let magnitude = num.abs();
349349
let raw_magnitude_string_result: Result<String, &'static str> = match self.format_type {
350-
Some(FormatType::FixedPointUpper) => match magnitude {
351-
magnitude if magnitude.is_nan() => Ok("NAN".to_owned()),
352-
magnitude if magnitude.is_infinite() => Ok("INF".to_owned()),
353-
_ => Ok(format!("{:.*}", precision, magnitude)),
354-
},
355-
Some(FormatType::FixedPointLower) => match magnitude {
356-
magnitude if magnitude.is_nan() => Ok("nan".to_owned()),
357-
magnitude if magnitude.is_infinite() => Ok("inf".to_owned()),
358-
_ => Ok(format!("{:.*}", precision, magnitude)),
359-
},
350+
Some(FormatType::FixedPointUpper) => Ok(float_ops::format_fixed(
351+
precision,
352+
magnitude,
353+
float_ops::Case::Upper,
354+
)),
355+
Some(FormatType::FixedPointLower) => Ok(float_ops::format_fixed(
356+
precision,
357+
magnitude,
358+
float_ops::Case::Lower,
359+
)),
360360
Some(FormatType::Decimal) => Err("Unknown format code 'd' for object of type 'float'"),
361361
Some(FormatType::Binary) => Err("Unknown format code 'b' for object of type 'float'"),
362362
Some(FormatType::Octal) => Err("Unknown format code 'o' for object of type 'float'"),

0 commit comments

Comments
 (0)