{"record":{"id":"82e0b1cde7b3ea87","repo":"TheAlgorithms/C-Sharp","slug":"array-is-empty-tanh","errorCode":null,"errorMessage":"Array is empty.","messagePattern":"Array is empty\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Numeric/Tanh.cs","lineNumber":38,"sourceCode":"        // For a single double, we can directly use the optimized Math.Tanh method.\n        return Math.Tanh(input);\n    }\n\n    /// <summary>\n    ///     Compute the Hyperbolic Tangent (Tanh) function element-wise for a vector.\n    /// </summary>\n    /// <param name=\"input\">The input vector of real numbers.</param>\n    /// <returns>The output vector of real numbers, where each element is in the range [-1, 1].</returns>\n    public static double[] Compute(double[] input)\n    {\n        if (input is null)\n        {\n            throw new ArgumentNullException(nameof(input));\n        }\n\n        if (input.Length == 0)\n        {\n            throw new ArgumentException(\"Array is empty.\");\n        }\n\n        var outputVector = new double[input.Length];\n\n        for (var index = 0; index < input.Length; index++)\n        {\n            // Apply Tanh to each element using the optimized Math.Tanh method.\n            outputVector[index] = Math.Tanh(input[index]);\n        }\n\n        return outputVector;\n    }\n}\n","sourceCodeStart":20,"sourceCodeEnd":52,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Numeric/Tanh.cs#L20-L52","documentation":"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.","triggerScenarios":"Calling Tanh.Compute(Array.Empty<double>()) or on an array produced by an empty slice, empty deserialization, or a filter that matched nothing.","commonSituations":"Empty mini-batches in ML inference loops; empty JSON arrays from an API; chunking logic that produced a final zero-size chunk.","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"],"exampleFix":"// before\nvar result = Tanh.Compute(batch);\n// after\nvar result = batch.Length > 0 ? Tanh.Compute(batch) : Array.Empty<double>();","handlingStrategy":"validation","validationCode":"if (input is null || input.Length == 0) return Array.Empty<double>();","typeGuard":"static bool IsNonEmpty(double[]? a) => a is { Length: > 0 };","tryCatchPattern":"try { return Tanh.Compute(input); }\ncatch (ArgumentException ex) when (ex.Message == \"Array is empty.\") { return Array.Empty<double>(); }","preventionTips":["Skip empty mini-batches before invoking element-wise math","Early-return empty results for empty inputs","Check chunking logic cannot emit zero-size chunks"],"tags":["empty-array","argument-validation","numeric"],"backgroundTag":"empty-required-field","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}