Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix out-of-bounds when converting from FixedDecimal to PluralOperands #290

Closed
sffc opened this issue Oct 2, 2020 · 11 comments · Fixed by #293
Closed

Fix out-of-bounds when converting from FixedDecimal to PluralOperands #290

sffc opened this issue Oct 2, 2020 · 11 comments · Fixed by #293
Labels
C-pluralrules Component: Plural rules T-bug Type: Bad behavior, security, privacy

Comments

@sffc
Copy link
Member

sffc commented Oct 2, 2020

FixedDecimal support numbers that are very large or very small. However, PluralOperands only supports numbers with integer and fraction parts that fit inside a u64. Discuss how to resolve cases when the FixedDecimal is larger than what the PluralOperands can support.

@sffc sffc added T-bug Type: Bad behavior, security, privacy discuss Discuss at a future ICU4X-SC meeting C-pluralrules Component: Plural rules labels Oct 2, 2020
@filmil
Copy link
Contributor

filmil commented Oct 2, 2020 via email

@sffc
Copy link
Member Author

sffc commented Oct 2, 2020

I lean toward saying that we just throw away digits that are out of bounds. We could cap the number of digits at 18 integer and 18 fraction or something like that.

@zbraniecki
Copy link
Member

In stdlib those are usually having lossy in the name. I'd suggest tryfrom and lossy and or panicking

@sffc
Copy link
Member Author

sffc commented Oct 3, 2020

It's technically lossy, but for all real situations, keeping 18 digits on both side of the decimal separator is far more than enough to run plural rules. Panicking is definitely not the right behavior. I'm not a fan of TryFrom because this is a hot code path. I suppose we could add TryFrom in addition to From (if that's allowed, given that TryFrom has a default implementation based on From).

@zbraniecki
Copy link
Member

It's technically lossy, but for all real situations, keeping 18 digits on both side of the decimal separator is far more than enough to run plural rules.

Sure, so then calling it from_fixed_decimal_lossy gives you:
a) correct description
b) communicates that it is lossy
c) appropriate overhead/perf

It's the same as From but with additional bit of information about lossiness.

@sffc
Copy link
Member Author

sffc commented Oct 3, 2020

Do you suggest we do not implement From<FixedDecimal> for PluralOperands and instead implement a from_fixed_decimal_lossy function?

@zbraniecki
Copy link
Member

Hmm, I'm torn. I see that clamping to 18 digits may be seen not as lossy here, but sort of loss of precision much like u64 as u32 is ot isize as usize is, rather than what String::*_lossy_* would do by potentially altering the string to cut out malformed chars.

And I understand the convenience value of using From here rather than just method name.

My only hiccup is that I'd like us to be able to have a path forward if later we want to add a fallible version because in some scenario we need to handle the overflow differently.

If having both, TryFrom and From is not possible, then
maybe then we could add a fallible constructor like try_from_fixed_decimal?

@filmil
Copy link
Contributor

filmil commented Oct 3, 2020

Hmm, I'm torn. I see that clamping to 18 digits may be seen not as lossy here, but sort of loss of precision much like u64 as u32 is ot isize as usize is, rather than what String::*_lossy_* would do by potentially altering the string to cut out malformed chars.

FWIW, I don't think this conversion is actually lossy: it does remove information of the original exact number, but that does not lose information about its plurality. It is a one-way transform: PluralOperands is as much lossy to FixedDecimal as PluralCategory is to PluralOperands. You can't recover 23 from PluralCategory::Few, but we don't call the transform select_lossy.

(By taking the least significant digits from both the integer and fractional part, we will probably remove any distinction between the "full" and "lossy" conversions.)

@zbraniecki
Copy link
Member

I'm convincend. From should work then and we shouldn't worry about lossiness.

@sffc sffc closed this as completed in #293 Oct 6, 2020
@sffc
Copy link
Member Author

sffc commented Oct 6, 2020

One other thought I had. We don't want overflowed PluralOperands to select =0 and =1 rules. For example, if you have 1e20+1, you don't want to get the =1 rule.

At what point in the process do you think clients are intended to support =0 and =1?

@zbraniecki
Copy link
Member

At what point in the process do you think clients are intended to support =0 and =1?

As in MessageFormat variant matching?

@zbraniecki zbraniecki removed the discuss Discuss at a future ICU4X-SC meeting label Oct 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-pluralrules Component: Plural rules T-bug Type: Bad behavior, security, privacy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants