Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jun 19, 2025

The ComplexFormatter examples in the documentation were producing incorrect output when formatting complex numbers with negative imaginary parts. Instead of displaying 12 - 15i, they would show 12 + -15i.

Problem

The custom formatter always used a hardcoded " + " operator between the real and imaginary parts:

// C# - Before
return c1.Real.ToString(fmtString) + " + " + c1.Imaginary.ToString(fmtString) + "i";

// VB.NET - Before  
Return c1.Real.ToString(fmtString) + " + " + c1.Imaginary.ToString(fmtString) + "i"

When c1.Imaginary was negative, ToString() would include the negative sign, resulting in output like "12 + -15i" instead of the expected "12 - 15i".

Solution

Updated both C# and VB.NET versions to:

  1. Calculate the appropriate sign based on the imaginary part's value
  2. Use Math.Abs() to get the absolute value of the imaginary part
  3. Display the calculated sign separately
// C# - After
char sign = c1.Imaginary < 0 ? '-' : '+';
return c1.Real.ToString(fmtString) + " " + sign + " " + Math.Abs(c1.Imaginary).ToString(fmtString) + "i";
' VB.NET - After
Dim sign As Char = If(c1.Imaginary < 0.0, "-"c, "+"c)
Return c1.Real.ToString(fmtString) + " " + sign + " " + Math.Abs(c1.Imaginary).ToString(fmtString) + "i"

Test Results

  • Positive imaginary: 12 + 15i ✅ (unchanged)
  • Negative imaginary: 12 - 15i ✅ (fixed from 12 + -15i)
  • Zero imaginary: 12 + 0i ✅ (unchanged)

The fix applies to both 'I' and 'J' formatting cases and maintains backward compatibility for positive and zero imaginary parts.

Fixes #46422.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Example fails with a negative Imaginary part of a Complex. Fix Complex number custom formatting for negative imaginary parts Jun 19, 2025
Copilot AI requested a review from gewarren June 19, 2025 16:41
Copilot AI requested a review from gewarren June 19, 2025 17:55
Copilot AI requested a review from gewarren June 19, 2025 18:00
@gewarren gewarren marked this pull request as ready for review June 23, 2025 12:24
@gewarren gewarren requested a review from a team as a code owner June 23, 2025 12:24
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM @gewarren

Let's :shipit:

@gewarren gewarren merged commit 0c43c35 into main Jun 23, 2025
10 checks passed
@gewarren gewarren deleted the copilot/fix-46422 branch June 23, 2025 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Example fails with a negative Imaginary part of a Complex.

3 participants