Skip to content

Fix format_number function #1021

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

Merged
merged 3 commits into from
Feb 10, 2021
Merged

Conversation

MaxVerevkin
Copy link
Collaborator

When I fist read the code I thought that format_number should format numbers to engineering notation, but in fact it produces outputs like "0.43MB" instead of "430KB", even if the minimal suffix is set to "K". For me, it doesn't make much sense. Please correct me if the current behavior is intentional.

Copy link
Collaborator

@ammgws ammgws left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the sake of comparison, what are each of these tests currently returning with the current code?

@MaxVerevkin
Copy link
Collaborator Author

MaxVerevkin commented Feb 5, 2021

@ammgws

Before

[src/util.rs:562] format_number(1.0, 3, "", "s") = "1.00s"
[src/util.rs:563] format_number(1.007, 3, "", "s") = "1.01s"
[src/util.rs:564] format_number(1.007, 4, "K", "s") = "0.001Ks"
[src/util.rs:565] format_number(1007., 3, "K", "s") = "1.01Ks"
[src/util.rs:566] format_number(107_000., 3, "", "s") = "0.11Ms"
[src/util.rs:567] format_number(107., 3, "", "s") = "0.11Ks"
[src/util.rs:568] format_number(0.000_123_123, 3, "", "N") = "0.12mN"

After

[src/util.rs:562] format_number(1.0, 3, "", "s") = "1.00s"
[src/util.rs:563] format_number(1.007, 3, "", "s") = "1.01s"
[src/util.rs:564] format_number(1.007, 4, "K", "s") = "0.001Ks"
[src/util.rs:565] format_number(1007., 3, "K", "s") = "1.01Ks"
[src/util.rs:566] format_number(107_000., 3, "", "s") = "107Ks"
[src/util.rs:567] format_number(107., 3, "", "s") = "107s"
[src/util.rs:568] format_number(0.000_123_123, 3, "", "N") = "123uN"

@ammgws
Copy link
Collaborator

ammgws commented Feb 5, 2021

@GladOSkar What do you think?

@GladOSkar
Copy link
Contributor

GladOSkar commented Feb 5, 2021

When i implemented this i based it off the proposed format from #240, which suggests 0.42M instead of 420K. I believe the idea was to always have a decimal point in there such that the entire string always has the same length (given a monospaced font) so the bar doesn't jump around when the value changes.

That said, i don't mind either way. But this could be considered a regression by some.

@MaxVerevkin
Copy link
Collaborator Author

MaxVerevkin commented Feb 5, 2021

One simple solution I see is to count characters, not digits. For example, if I have total_digits=3 (which should be renamed then), I would get "420K" and "5.3M".

Edit: no, that's bad idea. (but I'm not sure)

@MaxVerevkin
Copy link
Collaborator Author

MaxVerevkin commented Feb 5, 2021

And maybe make this configurable, because switching between these two behaviors (from this PR and current) is quite easy:

---    let exp_level = (raw_value.log10().div_euclid(3.) as i32)
+++    let exp_level = ((raw_value.log10() + 1.).div_euclid(3.) as i32)

@GladOSkar
Copy link
Contributor

GladOSkar commented Feb 5, 2021

Sounds reasonable, although it could be a bit awkward for non-monospaced fonts and also doesn't read as nicely. But that decision is not up to me @ammgws. I'm fine with either.

@MaxVerevkin
Copy link
Collaborator Author

MaxVerevkin commented Feb 6, 2021

Well, with non-monospace fonts the bar will change its size either way. @ammgws, @YodaEmbedding what do you think?

@MaxVerevkin
Copy link
Collaborator Author

I will leave it as it is, maybe just refactor it later.

@MaxVerevkin MaxVerevkin closed this Feb 8, 2021
@ammgws ammgws reopened this Feb 8, 2021
@ammgws
Copy link
Collaborator

ammgws commented Feb 8, 2021

it produces outputs like "0.43MB" instead of "430KB", even if the minimal suffix is set to "K"

This does seem like it should be fixed though

@MaxVerevkin
Copy link
Collaborator Author

If "430KB" is the expected behavior, then if user wants the bar not to change it's size, he may set speed_min_unit = "M".

@ammgws
Copy link
Collaborator

ammgws commented Feb 8, 2021

Yeah that's my thought too.

Comment on lines +54 to +55
.max(min_exp_level)
.min(4);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, clamp() is stabilized in rust 1.50.0 which releases soon.

@ammgws
Copy link
Collaborator

ammgws commented Feb 10, 2021

Cheers!

@ammgws ammgws merged commit 6414861 into greshake:master Feb 10, 2021
@MaxVerevkin MaxVerevkin deleted the new_format_number branch February 10, 2021 06:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants