TheAlgorithms/C-Sharp · error · ArgumentException
Array is empty.
Error message
Array is empty.
What it means
Tanh.Compute rejects zero-length arrays: the hyperbolic tangent of an empty vector is undefined in this library, so it throws ArgumentException ('Array is empty.') after the null check when input.Length == 0.
Solutions
- Guard with input.Length > 0 before calling and return an empty array early
- Fix the batch/chunk producer so empty batches are skipped
- Catch ArgumentException and treat it as the empty-input case
Example fix
// before var result = Tanh.Compute(batch); // after var result = batch.Length > 0 ? Tanh.Compute(batch) : Array.Empty<double>();
Defensive patterns
Strategy: validation
Validate before calling
if (input is null || input.Length == 0) return Array.Empty<double>();
Type guard
static bool IsNonEmpty(double[]? a) => a is { Length: > 0 }; Try / catch
try { return Tanh.Compute(input); }
catch (ArgumentException ex) when (ex.Message == "Array is empty.") { return Array.Empty<double>(); } Prevention
- Skip empty mini-batches before invoking element-wise math
- Early-return empty results for empty inputs
- Check chunking logic cannot emit zero-size chunks
When it happens
Trigger: Calling Tanh.Compute(Array.Empty<double>()) or on an array produced by an empty slice, empty deserialization, or a filter that matched nothing.
Common situations: Empty mini-batches in ML inference loops; empty JSON arrays from an API; chunking logic that produced a final zero-size chunk.
Understand the failure class
Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.
Related errors
- Array is empty.
- Error value is not on interval (0.0; 1.0).
- Input must be a non-negative integer.
- input
- Invalid parameter settings for Ascon Hash
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/82e0b1cde7b3ea87.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Numeric/Tanh.cs:38
// For a single double, we can directly use the optimized Math.Tanh method.
return Math.Tanh(input);
}
/// <summary>
/// Compute the Hyperbolic Tangent (Tanh) function element-wise for a vector.
/// </summary>
/// <param name="input">The input vector of real numbers.</param>
/// <returns>The output vector of real numbers, where each element is in the range [-1, 1].</returns>
public static double[] Compute(double[] input)
{
if (input is null)
{
throw new ArgumentNullException(nameof(input));
}
if (input.Length == 0)
{
throw new ArgumentException("Array is empty.");
}
var outputVector = new double[input.Length];
for (var index = 0; index < input.Length; index++)
{
// Apply Tanh to each element using the optimized Math.Tanh method.
outputVector[index] = Math.Tanh(input[index]);
}
return outputVector;
}
}
View on GitHub (pinned to 96e2905cab)