From 3344ae17cf72492c2fc8d02d32e20b302927ffbd Mon Sep 17 00:00:00 2001 From: ChJR Date: Mon, 26 Oct 2020 00:27:14 +0900 Subject: [PATCH 1/4] Fix default format(float) result when the number is too big Before this change, format(1e16) returns '10000000000000000.0' in RustPython while CPython 3.7 returns '1e+16'. After this change, format(1e16) returns '1e+16' in RustPython too. --- vm/src/format.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/vm/src/format.rs b/vm/src/format.rs index 9b0a6a0a08..b0afe1a943 100644 --- a/vm/src/format.rs +++ b/vm/src/format.rs @@ -1,4 +1,5 @@ use crate::builtins::pystr; +use crate::common::float_ops; use crate::exceptions::{IntoPyException, PyBaseExceptionRef}; use crate::function::FuncArgs; use crate::pyobject::{ItemProtocol, PyObjectRef, PyResult, TypeProtocol}; @@ -405,7 +406,7 @@ impl FormatSpec { magnitude if magnitude.is_infinite() => Ok("inf".to_owned()), // Using the Debug format here to prevent the automatic conversion of floats // ending in .0 to their integer representation (e.g., 1.0 -> 1) - _ => Ok(format!("{:?}", magnitude)), + _ => Ok(float_ops::to_string(magnitude)), } } }; From 2219382928f3f60be71744134c396d0ab46ecd84 Mon Sep 17 00:00:00 2001 From: ChJR Date: Mon, 26 Oct 2020 01:01:50 +0900 Subject: [PATCH 2/4] Add tests for default format(float) working --- extra_tests/snippets/floats.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/extra_tests/snippets/floats.py b/extra_tests/snippets/floats.py index ce39be708b..18cbc2f426 100644 --- a/extra_tests/snippets/floats.py +++ b/extra_tests/snippets/floats.py @@ -487,6 +487,17 @@ def identical(x, y): assert float(1.2345678901234567890).__repr__() == "1.2345678901234567" assert float(1.2345678901234567890e308).__repr__() == "1.2345678901234567e+308" +assert format(1e15) == "1000000000000000.0" +assert format(1e16) == "1e+16" +assert format(1e308) == "1e+308" +assert format(1e309) == "inf" +assert format(1e-323) == "1e-323" +assert format(1e-324) == "0.0" +assert format(1e-5) == "1e-05" +assert format(1e-4) == "0.0001" +assert format(1.2345678901234567890) == "1.2345678901234567" +assert format(1.2345678901234567890e308) == "1.2345678901234567e+308" + assert float('0_0') == 0.0 assert float('.0') == 0.0 assert float('0.') == 0.0 From edc4de20e49917bed12715828be82c06ea5eae7a Mon Sep 17 00:00:00 2001 From: ChJR Date: Mon, 26 Oct 2020 23:48:28 +0900 Subject: [PATCH 3/4] Remove not matched comments --- vm/src/format.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/vm/src/format.rs b/vm/src/format.rs index b0afe1a943..69050b424e 100644 --- a/vm/src/format.rs +++ b/vm/src/format.rs @@ -404,8 +404,6 @@ impl FormatSpec { match magnitude { magnitude if magnitude.is_nan() => Ok("nan".to_owned()), magnitude if magnitude.is_infinite() => Ok("inf".to_owned()), - // Using the Debug format here to prevent the automatic conversion of floats - // ending in .0 to their integer representation (e.g., 1.0 -> 1) _ => Ok(float_ops::to_string(magnitude)), } } From 3838610e55000fb3ee99c46f982738e905469bc6 Mon Sep 17 00:00:00 2001 From: ChJR Date: Mon, 26 Oct 2020 23:58:51 +0900 Subject: [PATCH 4/4] Fix fmt check --- vm/src/format.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/vm/src/format.rs b/vm/src/format.rs index 69050b424e..963d7d0187 100644 --- a/vm/src/format.rs +++ b/vm/src/format.rs @@ -400,13 +400,11 @@ impl FormatSpec { magnitude if magnitude.is_infinite() => Ok("inf%".to_owned()), _ => Ok(format!("{:.*}%", precision, magnitude * 100.0)), }, - None => { - match magnitude { - magnitude if magnitude.is_nan() => Ok("nan".to_owned()), - magnitude if magnitude.is_infinite() => Ok("inf".to_owned()), - _ => Ok(float_ops::to_string(magnitude)), - } - } + None => match magnitude { + magnitude if magnitude.is_nan() => Ok("nan".to_owned()), + magnitude if magnitude.is_infinite() => Ok("inf".to_owned()), + _ => Ok(float_ops::to_string(magnitude)), + }, }; if raw_magnitude_string_result.is_err() {