Skip to content

human-readable: calculate numbers in a loop#872

Open
eworm-de wants to merge 1 commit into
RsyncProject:masterfrom
eworm-de:human-1
Open

human-readable: calculate numbers in a loop#872
eworm-de wants to merge 1 commit into
RsyncProject:masterfrom
eworm-de:human-1

Conversation

@eworm-de

Copy link
Copy Markdown
Contributor

This drops a lot of else if blocks and extends units by "E", "Z" & "Y".

@eworm-de eworm-de force-pushed the human-1 branch 2 times, most recently from 989ba05 to dd918b8 Compare April 22, 2026 09:33
@iliruci23-hue

Copy link
Copy Markdown

Ismail

@eworm-de eworm-de force-pushed the human-1 branch 4 times, most recently from 3e7e591 to 37afb8b Compare May 3, 2026 17:18
@eworm-de eworm-de force-pushed the human-1 branch 6 times, most recently from 4da2ea0 to a5defbf Compare May 26, 2026 22:02
@eworm-de eworm-de force-pushed the human-1 branch 4 times, most recently from b7ae0ad to be26f4f Compare June 2, 2026 11:15

@steadytao steadytao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This needs one portability fix before merge. The new loop calls labs(num / mult) but num is int64; on platforms where int64 is not long that can truncate or otherwise compare the wrong value for large inputs. The comparison can avoid that by staying in int64 arithmetic, for example by comparing both positive and negative ranges directly rather than calling labs().

The docs also list Z and Y but with the current int64 input those units are not reachable in practice. If the implementation only reaches E, I would keep the documentation to the reachable units.

@eworm-de

eworm-de commented Jun 6, 2026

Copy link
Copy Markdown
Contributor Author

Updated - here, and in #459 with some additional changes.

@steadytao steadytao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is much closer but I think there is still one signed-range issue before merge.

pos_num = num * (num < 0 ? -1 : 1) overflows for INT64_MIN since the minimum signed value cannot be represented as a positive int64. The old code avoided that because it converted to double before negating.

Could the scale-selection loop avoid forming an absolute signed value? For example, compare num / mult against both positive and negative powi thresholds directly, or otherwise handle the minimum signed value without negating it.

@eworm-de

eworm-de commented Jun 6, 2026

Copy link
Copy Markdown
Contributor Author

Ah, yes... Unlikely to hit this 😁️ - but you are right.

But it should be fine when using an unsinged data type? Switched to uint64_t.

I would like to keep the variable, as a follow-up commit (currently in 61802ac) re-uses it.

@steadytao steadytao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Agreed that this is unlikely to be hit in normal use but this helper is specifically in the numeric formatting path and the fix is small enough that I think we should keep it defined for the full int64 range.

There is still one signed-range issue here: assigning to uint64_t does not avoid the overflow because num * -1 is evaluated in signed int64 before conversion. For INT64_MIN, that is still undefined behaviour.

This needs to convert before negating or avoid forming a signed absolute value. For example: uint64_t abs_num = num < 0 ? 0 - (uint64_t)num : (uint64_t)num;

That keeps the INT64_MIN case defined in unsigned arithmetic.

@eworm-de

eworm-de commented Jun 7, 2026

Copy link
Copy Markdown
Contributor Author

I did not want to say that the case is irrelevant, we should definitely get this right. I just wanted to explain why my mind did not came up with this. 😉️

Same for the overflow in calculation - I was not aware of this.

Updated again, I hope it is ok now. Thanks a lot for the detailed review!

@steadytao steadytao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you very much, this looks good to me now. Nice work on cleaning this up and sticking with my tendency for correctness 🤣

@steadytao

Copy link
Copy Markdown
Member

@tridge Could you review and merge when available, cheers mate!

@steadytao

Copy link
Copy Markdown
Member

@eworm-de I will also have a more thorough look at 459 within the next 24-48 hours, thank you for your work thus far.

@eworm-de

eworm-de commented Jun 7, 2026

Copy link
Copy Markdown
Contributor Author

Thanks a lot, and you are welcome!

Let me know if you want #459 to be split into smaller pull requests for review.

@steadytao

Copy link
Copy Markdown
Member

Let me know if you want #459 to be split into smaller pull requests for review.

It's small enough, that'll be fine.

@eworm-de eworm-de force-pushed the human-1 branch 4 times, most recently from e3ec4c2 to c79f181 Compare June 9, 2026 05:09
This drops a lot of `else if` blocks and extends units by "E" (Exa),
which is 2^60 and thus the last to fit into int64.
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