Skip to content

type::Size gives incorrect results for exabyte+ values. #488

@paxcut

Description

@paxcut

To reproduce the error use pattern

import type.size;
type::Size128 size @ 0x0;

on two inputs. First on

00 00 00 00 00 00 00 10 00 00 00 00 00 00 00 00

this return 1 Eib which is correct.
then change the input to

00 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00

and the result is incorrectly set to 1 EiB again instead of 1024 EiB.

The error is dies to this line in the code for the formatter

  while (sizeFloat >= 1024 && i <= 6) {

which should really be

  while (sizeFloat >= 1024 && i < 6) {

I can submit a PR with this fix or I also have rewritten the formatter in a shorter version as:

u32 i = std::math::min(u32(std::math::log2(size))/10,6);
double sizeFloat = double(size)/double(1 << 10 * i);
str sizeStr[7] = {"Byte", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"};
str formatString = (i!=0) ? "{:.3f} {}" : "{} {}";
str result = std::format(formatString, sizeFloat, sizeStr[i]);
if (i==0 && sizeFloat != 1.0)
    result += 's';
return result;

that can also be used instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions