Skip to content

Commit

Permalink
Rename FloatPrecision::Floating to FloatPrecision::RoundTrip (#5616)
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth authored Oct 1, 2024
1 parent 6572ff9 commit 95b3b5c
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- `calendrical_calculations`:
- `databake`
- `fixed_decimal`
- `FloatPrecision::Floating` renamed to `FloatPrecision::RoundTrip` (https://github.com/unicode-org/icu4x/pull/5616)
- `FixedDecimal::concatenated_end()` now returns both `self` and `other` in the error case. (https://github.com/unicode-org/icu4x/pull/5623)
- `icu_pattern`
- `litemap`
Expand Down
4 changes: 2 additions & 2 deletions components/experimental/src/compactdecimal/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,10 @@ impl CompactDecimalFormatter {
&self,
value: f64,
) -> Result<FormattedCompactDecimal<'_>, fixed_decimal::LimitError> {
use fixed_decimal::FloatPrecision::Floating;
use fixed_decimal::FloatPrecision::RoundTrip;
// NOTE: This first gets the shortest representation of the f64, which
// manifests as double rounding.
let partly_rounded = FixedDecimal::try_from_f64(value, Floating)?;
let partly_rounded = FixedDecimal::try_from_f64(value, RoundTrip)?;
Ok(self.format_fixed_decimal(partly_rounded))
}

Expand Down
4 changes: 2 additions & 2 deletions ffi/capi/bindings/c/FixedDecimal.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ffi/capi/bindings/cpp/icu4x/FixedDecimal.d.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions ffi/capi/bindings/cpp/icu4x/FixedDecimal.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions ffi/capi/bindings/dart/FixedDecimal.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ffi/capi/bindings/js/FixedDecimal.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ffi/capi/bindings/js/FixedDecimal.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions ffi/capi/src/fixed_decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ pub mod ffi {
#[diplomat::rust_link(fixed_decimal::FixedDecimal::try_from_f64, FnInStruct)]
#[diplomat::rust_link(fixed_decimal::FloatPrecision, Enum)]
#[diplomat::rust_link(fixed_decimal::DoublePrecision, Enum, hidden)]
#[diplomat::attr(js, rename = "from_number_with_floating_precision")]
#[diplomat::attr(js, rename = "from_number_with_round_trip_precision")]
#[diplomat::attr(supports = fallible_constructors, named_constructor)]
pub fn from_double_with_floating_precision(
pub fn from_double_with_round_trip_precision(
f: f64,
) -> Result<Box<FixedDecimal>, FixedDecimalLimitError> {
let precision = fixed_decimal::DoublePrecision::Floating;
let precision = fixed_decimal::DoublePrecision::RoundTrip;
Ok(Box::new(FixedDecimal(
fixed_decimal::FixedDecimal::try_from_f64(f, precision)?,
)))
Expand Down
8 changes: 4 additions & 4 deletions tutorials/cpp/fixeddecimal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ int main() {
return 1;
}

decimal = FixedDecimal::from_double_with_floating_precision(100.01).ok().value();
decimal = FixedDecimal::from_double_with_round_trip_precision(100.01).ok().value();
out = fdf->format(*decimal.get());
std::cout << "Formatted float value is " << out << std::endl;
if (out != "১০০.০১") {
Expand Down Expand Up @@ -87,14 +87,14 @@ int main() {

fdf = FixedDecimalFormatter::create_with_manual_data("+", "", "-", "", "/", "_", 4, 2, 4, digits, FixedDecimalGroupingStrategy::Auto).ok().value();

decimal = FixedDecimal::from_double_with_floating_precision(123456.8901).ok().value();
decimal = FixedDecimal::from_double_with_round_trip_precision(123456.8901).ok().value();
out = fdf->format(*decimal.get());
std::cout << "Formatted float value for custom numeric system is " << out << std::endl;
if (out != "bcdefg/ijab") {
std::cout << "Output does not match expected output" << std::endl;
return 1;
}
decimal = FixedDecimal::from_double_with_floating_precision(123451234567.8901).ok().value();
decimal = FixedDecimal::from_double_with_round_trip_precision(123451234567.8901).ok().value();
out = fdf->format(*decimal.get());
std::cout << "Formatted float value for custom numeric system is " << out << std::endl;
if (out != "bc_de_fb_cd_efgh/ijab") {
Expand All @@ -107,7 +107,7 @@ int main() {
fdf = FixedDecimalFormatter::create_with_grouping_strategy(
*dp.get(), *locale.get(), FixedDecimalGroupingStrategy::Auto).ok().value();

decimal = FixedDecimal::from_double_with_floating_precision(123456.8901).ok().value();
decimal = FixedDecimal::from_double_with_round_trip_precision(123456.8901).ok().value();
out = fdf->format(*decimal.get());
std::cout << "Formatted value is " << out << std::endl;
if (out != "๑๒๓,๔๕๖.๘๙๐๑") {
Expand Down
2 changes: 1 addition & 1 deletion utils/fixed_decimal/benches/fixed_decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ fn rounding_benches(c: &mut Criterion) {
];

let nums: Vec<_> = triangular_floats(1e7)
.map(|f| FixedDecimal::try_from_f64(f, FloatPrecision::Floating).unwrap())
.map(|f| FixedDecimal::try_from_f64(f, FloatPrecision::RoundTrip).unwrap())
.collect();
let mut group = c.benchmark_group("rounding");

Expand Down
10 changes: 5 additions & 5 deletions utils/fixed_decimal/src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2622,7 +2622,7 @@ pub enum FloatPrecision {
///
/// This results in a FixedDecimal having enough digits to recover the original floating point
/// value, with no trailing zeros.
Floating,
RoundTrip,
}

#[cfg(feature = "ryu")]
Expand All @@ -2649,7 +2649,7 @@ impl FixedDecimal {
/// assert_writeable_eq!(decimal, "-5.10");
///
/// let decimal =
/// FixedDecimal::try_from_f64(0.012345678, FloatPrecision::Floating)
/// FixedDecimal::try_from_f64(0.012345678, FloatPrecision::RoundTrip)
/// .expect("Finite quantity");
/// assert_writeable_eq!(decimal, "0.012345678");
///
Expand Down Expand Up @@ -2683,7 +2683,7 @@ impl FixedDecimal {
decimal.lower_magnitude = 0;
}
match precision {
FloatPrecision::Floating => (),
FloatPrecision::RoundTrip => (),
FloatPrecision::Integer => {
if lowest_magnitude < 0 {
return Err(LimitError);
Expand Down Expand Up @@ -2739,12 +2739,12 @@ fn test_float() {
let cases = [
TestCase {
input: 1.234567,
precision: FloatPrecision::Floating,
precision: FloatPrecision::RoundTrip,
expected: "1.234567",
},
TestCase {
input: 888999.,
precision: FloatPrecision::Floating,
precision: FloatPrecision::RoundTrip,
expected: "888999",
},
// HalfExpand tests
Expand Down

0 comments on commit 95b3b5c

Please sign in to comment.