runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Complex.Tan returns infinities for a finite tangent near π/2

Open vera-rubin opened this issue 1 month ago • 4 comments

Description

Complex.Tan returns infinities for a finite tangent near π/2.

Reproduction Steps

Run dotnet new console -n TanRepro -f net10.0, replace TanRepro/Program.cs with this, then run dotnet run --project TanRepro -c Release.

// CC0-1.0
using System.Globalization;
using System.Numerics;

CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
var z = new Complex(1.5707963267948966, 1e-10);
var actual = Complex.Tan(z);
var quotient = Complex.Sin(z) / Complex.Cos(z);
Console.WriteLine($"Tan = ({actual.Real:R}, {actual.Imaginary:R})");
Console.WriteLine($"Sin/Cos = ({quotient.Real:R}, {quotient.Imaginary:R})");

Expected behavior

The documented quotient is finite for this input.

Actual behavior

By inspection, Complex.Tan yields infinities.

Configuration

Reported SDK 10.0.402 / runtime 10.0.13 and SDK 11.0.100-rc.2.26504.105 / runtime 11.0.0-rc.2.26504.105, Ubuntu 24.04.5 x64, source inspected only at 00cc05139240bca32b3e778ee2f6ffd7c6a5bd60.

Other information

The doubled-angle denominator, cos(2*x) + cosh(2*y), rounds to zero here.

AI-assisted.

— ChipLab

vera-rubin avatar Oct 16 '26 00:10 vera-rubin

Tagging subscribers to this area: @dotnet/area-system-globalization See info in area-owners.md if you want to be subscribed.

Tagging subscribers to this area: @dotnet/area-system-numerics See info in area-owners.md if you want to be subscribed.

The documented quotient is finite for this input.

Notably you're misunderstanding the documentation here. It is not stating the result returned is the same as that, it is stating that Tan is mathematically equivalent to Sin/Cos


This is then not a regression and is not something we guarantee 0.5 ULP results on or around, so it's not something we're going to backport. However, it is something I'll look into for .NET 12 as part of improving the perf and accuracy of math functions in general.

We could only fit so many fixes into .NET 11 and that was focused on exposing Complex<T>, ensuring general (but not strict) compliance with most edge case values required by Annex G of the C standard, and improving perf/accuracy of a few functions where reasonable.

tannergooding avatar Oct 16 '26 01:10 tannergooding

Thanks, that makes sense: I misread that line as a statement about the returned values rather than the math. Glad it is on the list for .NET 12.

AI-assisted.

vera-rubin avatar Oct 16 '26 03:10 vera-rubin