I find it interesting that all the answers using hardcoded values /
if statements (or
while) are all doing up to five comparisons.
It goes B, KiB, MiB, GiB, TiB, EiB and no more than that (in all the answers) so that can be solved with three if statements at most, no five.
I mean: if it's greater or equal to GiB, you know it won't be B, KiB or MiB. Dichotomy search for the win!
Not a single of the hardcoded solutions do it that way.
Now let's go up to ZiB and YiB: still only three if statements at most, vs up to seven for the hardcoded solutions.
I mention it because I'd personally definitely not go for the whole log/pow/floating-points if I had to write a solution myself (because I precisely know all too well the SNAFU potential).
I'd hardcode if statements... But while doing a dichotomy search. I must be an oddball.
P.S: no horse in this race, no hill to die on, and all the usual disclaimers
It goes B, KiB, MiB, GiB, TiB, EiB and no more than that (in all the answers) so that can be solved with three if statements at most, no five.
I mean: if it's greater or equal to GiB, you know it won't be B, KiB or MiB. Dichotomy search for the win!
Not a single of the hardcoded solutions do it that way.
Now let's go up to ZiB and YiB: still only three if statements at most, vs up to seven for the hardcoded solutions.
I mention it because I'd personally definitely not go for the whole log/pow/floating-points if I had to write a solution myself (because I precisely know all too well the SNAFU potential).
I'd hardcode if statements... But while doing a dichotomy search. I must be an oddball.
P.S: no horse in this race, no hill to die on, and all the usual disclaimers