Humanizr/Humanizer · error · ArgumentOutOfRangeException

The value must be finite and fit the range supported by Byte

Error message

The value must be finite and fit the range supported by ByteSize.Bits.

What it means

Thrown by ByteSize.FromKibibytes(double) when the argument, multiplied by BytesInKibibyte (1024), produces a result that is not finite (NaN or Infinity) or falls outside the supported byte range of approximately ±1 exbibyte (specifically ±BytesInPebibyte * 1024). The IsSupportedUnitValue guard rejects overflow-prone or non-finite inputs before they corrupt the ByteSize struct.

Source

Thrown at src/Humanizer/Bytes/ByteSize.cs:327

        new(value / (double)BitsInByte);

    internal static ByteSize FromBytesAndBits(double bytes, long bits) =>
        new(bytes, bits);

    public static ByteSize FromBytes(double value) =>
        new(value);

    public static ByteSize FromKilobytes(double value) =>
        new(value * BytesInKilobyte);

    /// <summary>
    /// Creates a byte size from a number of kibibytes.
    /// </summary>
    public static ByteSize FromKibibytes(double value)
    {
        if (!IsSupportedUnitValue(value, BytesInKibibyte))
        {
            throw new ArgumentOutOfRangeException(nameof(value), "The value must be finite and fit the range supported by ByteSize.Bits.");
        }

        return new(value * BytesInKibibyte);
    }

    public static ByteSize FromMegabytes(double value) =>
        new(value * BytesInMegabyte);

    /// <summary>
    /// Creates a byte size from a number of mebibytes.
    /// </summary>
    public static ByteSize FromMebibytes(double value)
    {
        if (!IsSupportedUnitValue(value, BytesInMebibyte))
        {
            throw new ArgumentOutOfRangeException(nameof(value), "The value must be finite and fit the range supported by ByteSize.Bits.");
        }

View on GitHub (pinned to ffc2b77c0f)

Solutions

  1. Validate that the input is finite (double.IsFinite) and within a reasonable range before calling FromKibibytes.
  2. Use ByteSize.FromBytes or the unchecked kilobyte/megabyte factory methods if you need to bypass the range guard (note those methods do not validate).
  3. Sanitize upstream calculations that might produce NaN or Infinity.

Example fix

// before
var size = ByteSize.FromKibibytes(someExternalValue);

// after
if (!double.IsFinite(someExternalValue) ||
    Math.Abs(someExternalValue) > 1e15)
    throw new ArgumentException("Invalid kibibyte value", nameof(someExternalValue));
var size = ByteSize.FromKibibytes(someExternalValue);
Defensive patterns

Strategy: validation

Validate before calling

if (!double.IsFinite(value) || Math.Abs(value) > 1e15)
    throw new ArgumentOutOfRangeException(nameof(value),
        "Value must be finite and within the supported kibibyte range.");
var size = ByteSize.FromKibibytes(value);

Type guard

static bool IsValidByteSizeUnitValue(double value, double bytesPerUnit) =>
    double.IsFinite(value * bytesPerUnit) &&
    Math.Abs(value * bytesPerUnit) < 1024d * 1024 * 1024 * 1024 * 1024;

Prevention

When it happens

Trigger: Calling ByteSize.FromKibibytes(double.NaN), ByteSize.FromKibibytes(double.PositiveInfinity), or a value so large that value * 1024 exceeds the supported range. For example, FromKibibytes(1e18) would overflow the supported bounds.

Common situations: A developer receives a size value from an external source (API, file system, user input) that is NaN, Infinity, or astronomically large, and passes it to FromKibibytes without validation. Also common when a division-by-zero upstream produces NaN that flows into this method.

Related errors


AI-assisted analysis of Humanizr/Humanizer@ffc2b77c0f (2026-08-13). Data as JSON: /api/errors/2c3fe9c7b6d55025. Report an issue: GitHub.