Skip to content

Example fails with a negative Imaginary part of a Complex. #46422

@OldSchoolNewWorld

Description

@OldSchoolNewWorld

Type of issue

Code doesn't work

Description

I was tinkering with names and layout, so I am providing what I think are the only the applicable parts, but not my actual revised code.
Basically: Calculate vs. hard code the middle sign. Use the calculated sign in the middle. Suppress the sign on the Imaginary part. Cover both the 'i' and 'j' case.

Sample code:

    If fmt.Substring(0, 1).Equals("I", StringComparison.OrdinalIgnoreCase) Then
        Return c1.Real.ToString(fmtString) + " + " + c1.Imaginary.ToString(fmtString) + "i"
    ElseIf fmt.Substring(0, 1).Equals("J", StringComparison.OrdinalIgnoreCase) Then
        Return c1.Real.ToString(fmtString) + " + " + c1.Imaginary.ToString(fmtString) + "j"
    Else
        Return c1.ToString(fmt, provider)
    End If

Suggestion:

    ' Determine the sign to display.
    dim sign=if(c1.Imaginary<0.0,"-"c,"+"c)
    ' Display the *determined* sign and the *absolute value* of the imaginary part.
    If fmt.Substring(0, 1).Equals("I", StringComparison.OrdinalIgnoreCase) Then
        Return c1.Real.ToString(fmtString) + " " + sign + " " + math,abs(c1.Imaginary).ToString(fmtString) + "i"
    ElseIf fmt.Substring(0, 1).Equals("J", StringComparison.OrdinalIgnoreCase) Then
        Return c1.Real.ToString(fmtString) + " + " + math.abs(c1.Imaginary).ToString(fmtString) + "j"
    Else
        Return c1.ToString(fmt, provider)
    End If

Maybe this should be its own case, but, while here, only some of the sample outputs have been updated to the ToString change.
"(12.3000001907349, 0)" vs. "<12.10; 15.40>".

Page URL

https://learn.microsoft.com/en-us/dotnet/fundamentals/runtime-libraries/system-numerics-complex?source=docs#format-a-complex-number

Content source URL

https://github.com/dotnet/docs/blob/main/docs/fundamentals/runtime-libraries/system-numerics-complex.md

Document Version Independent Id

a7805572-1e26-d823-aa06-0ad20172ab6a

Platform Id

aba02a42-6ce7-1e75-8ca1-d4667deec955

Article author

@gewarren

Metadata

  • ID: f1c6acac-5557-c431-8318-e93f55b9dd4e
  • PlatformId: aba02a42-6ce7-1e75-8ca1-d4667deec955
  • Service: dotnet-fundamentals

Related Issues

Activity

self-assigned this
on Jun 2, 2025
moved this from 🔖 Ready to 🏗 In progress in dotnet/docs June 2025 sprint projecton Jun 19, 2025
added
🗺️ mapQUESTOnly used as a way to mark an issue as updated for quest. RepoMan should instantly remove it.
on Jun 19, 2025
removed
🗺️ mapQUESTOnly used as a way to mark an issue as updated for quest. RepoMan should instantly remove it.
on Jun 19, 2025
moved this from 🏗 In progress to 👀 In review in dotnet/docs June 2025 sprint projecton Jun 20, 2025
added
🗺️ mapQUESTOnly used as a way to mark an issue as updated for quest. RepoMan should instantly remove it.
on Jun 20, 2025
removed
🗺️ mapQUESTOnly used as a way to mark an issue as updated for quest. RepoMan should instantly remove it.
on Jun 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @gewarren@OldSchoolNewWorld

    Issue actions

      Example fails with a negative Imaginary part of a Complex. · Issue #46422 · dotnet/docs