Skip to content

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

Open
@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

Metadata

Metadata

Type

No type

Projects

Status

👀 In review

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions