TheAlgorithms/C-Sharp · error

should be greater than zero

Error message

{nameof(maxStep)} should be greater than zero

What it means

Mandelbrot.GetBitmap requires maxStep, the iteration cap controlling escape-time detail, to be strictly positive. After validating width and height it throws ArgumentOutOfRangeException when maxStep <= 0, since the per-pixel loop would otherwise never terminate meaningfully.

Solutions

  1. Pass maxStep >= 1 (the default is 50)
  2. Validate the iteration count before calling and clamp to at least 1
  3. Fix the config/parse path that yielded 0 or a negative value

Example fix

// before
var bmp = Mandelbrot.GetBitmap(800, 600, 3.2, steps);
// after
var bmp = Mandelbrot.GetBitmap(800, 600, 3.2, Math.Max(1, steps));
Defensive patterns

Strategy: validation

Validate before calling

if (maxStep <= 0) throw new ArgumentException("maxStep must be positive", nameof(maxStep));

Type guard

static bool IsValidIterationCount(int n) => n > 0;

Try / catch

try { var bmp = Mandelbrot.GetBitmap(w, h, figureWidth, steps); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "maxStep")
{
    // retry with default maxStep = 50
}

Prevention

When it happens

Trigger: Calling Algorithms.Other.Mandelbrot.GetBitmap with maxStep <= 0, e.g. GetBitmap(800, 600, 3.2, 0) or a negative step count passed from tuning code.

Common situations: Experimenting with iteration depth and passing 0; parsing a 'max iterations' config value where an empty field became 0; sign errors in generated parameters.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/0d19c50e7dbeb77e. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Other/Mandelbrot.cs:68

        bool useDistanceColorCoding = true)
    {
        if (bitmapWidth <= 0)
        {
            throw new ArgumentOutOfRangeException(
                nameof(bitmapWidth),
                $"{nameof(bitmapWidth)} should be greater than zero");
        }

        if (bitmapHeight <= 0)
        {
            throw new ArgumentOutOfRangeException(
                nameof(bitmapHeight),
                $"{nameof(bitmapHeight)} should be greater than zero");
        }

        if (maxStep <= 0)
        {
            throw new ArgumentOutOfRangeException(
                nameof(maxStep),
                $"{nameof(maxStep)} should be greater than zero");
        }

        var bitmap = new SKBitmap(bitmapWidth, bitmapHeight);
        var figureHeight = figureWidth / bitmapWidth * bitmapHeight;

        // loop through the bitmap-coordinates
        for (var bitmapX = 0; bitmapX < bitmapWidth; bitmapX++)
        {
            for (var bitmapY = 0; bitmapY < bitmapHeight; bitmapY++)
            {
                // determine the figure-coordinates based on the bitmap-coordinates
                var figureX = figureCenterX + ((double)bitmapX / bitmapWidth - 0.5) * figureWidth;
                var figureY = figureCenterY + ((double)bitmapY / bitmapHeight - 0.5) * figureHeight;

                var distance = GetDistance(figureX, figureY, maxStep);

View on GitHub (pinned to 96e2905cab)