Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Complex numbers in C#

Complex numbers in C#

Scheduled Pinned Locked Moved The Lounge
csharpcomjsontutorialquestion
26 Posts 15 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Jorgen Andersson

    If spacecraft or nuclear reactors are ever allowed to be run on .Net I will hide in a cave for the foreseeable future.

    Wrong is evil and must be defeated. - Jeff Ello

    S Offline
    S Offline
    Slacker007
    wrote on last edited by
    #3

    this gave me a good hearty chuckle. The kind of chuckle that can give a person a hernia. :thumbsup:

    1 Reply Last reply
    0
    • C Chris Maunder

      I was reading about complex numbers in C# and saw [this](https://docs.microsoft.com/en-us/dotnet/api/system.numerics.complex?view=net-5.0)

      Complex minusOne = new Complex(-1, 0);
      Console.WriteLine(Complex.Sqrt(minusOne));
      // The example displays the following output:
      // (6.12303176911189E-17, 1) on 32-bit systems.
      // (6.12323399573677E-17,1) on IA64 systems.

      I'm curious: is there any reason one would not simply hardcode Complex.Sqrt(-1) to equal new Complex(0, 1);? The whole thing about complex numbers is they are based on the fundamental concept that i2 = -1. Why wouldn't you bake that in as an absolute and let the representational errors happen elsewhere? I get that actually detecting all cases of √-1 is tricky and messy at best, but it's not like you can actually compare, with arbitrary precision, two floating point values anyway. Future warning: if I ever get access to the .NET code in a way that lets me sneak in a change, then this will happen. It may cause manned spacecraft to veer off course and crash into the moon, or nuclear reactors to overheat and take out half a continent. But, dammit, √-1 will equal i.

      cheers Chris Maunder

      R Offline
      R Offline
      Rick York
      wrote on last edited by
      #4

      Do you really work on an Itanium system? That's what the IA-64 instruction was for - the Intel Itanium systems. The 64-bit instruction more commonly used today is called X64 and was originally developed by AMD.

      "They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"

      P C 2 Replies Last reply
      0
      • R Rick York

        Do you really work on an Itanium system? That's what the IA-64 instruction was for - the Intel Itanium systems. The 64-bit instruction more commonly used today is called X64 and was originally developed by AMD.

        "They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #5

        Hmmm... how can I get .net on my Itanium (OpenVMS) system?

        1 Reply Last reply
        0
        • C Chris Maunder

          I was reading about complex numbers in C# and saw [this](https://docs.microsoft.com/en-us/dotnet/api/system.numerics.complex?view=net-5.0)

          Complex minusOne = new Complex(-1, 0);
          Console.WriteLine(Complex.Sqrt(minusOne));
          // The example displays the following output:
          // (6.12303176911189E-17, 1) on 32-bit systems.
          // (6.12323399573677E-17,1) on IA64 systems.

          I'm curious: is there any reason one would not simply hardcode Complex.Sqrt(-1) to equal new Complex(0, 1);? The whole thing about complex numbers is they are based on the fundamental concept that i2 = -1. Why wouldn't you bake that in as an absolute and let the representational errors happen elsewhere? I get that actually detecting all cases of √-1 is tricky and messy at best, but it's not like you can actually compare, with arbitrary precision, two floating point values anyway. Future warning: if I ever get access to the .NET code in a way that lets me sneak in a change, then this will happen. It may cause manned spacecraft to veer off course and crash into the moon, or nuclear reactors to overheat and take out half a continent. But, dammit, √-1 will equal i.

          cheers Chris Maunder

          J Offline
          J Offline
          Jon McKee
          wrote on last edited by
          #6

          I have a guess. When dealing with stuff in mathematics that involves a degree of error, you want to treat all input consistently. Having some input that generates a value with error baked in and other input that generates a hard-coded exact value is a bad idea. With Complex.Sqrt, they use [polar coordinates](https://referencesource.microsoft.com/#System.Numerics/System/Numerics/Complex.cs,d83f2d44d890c43f,references) to calculate the complex number. This is going to have some error baked in. If a developer using Complex is adjusting for error, and the operations on Complex treat some input differently (like -1), then this adjustment would remove the error from some cases and introduce error in others. If you want an error-less representation of Complex.Sqrt(-1) you can use Complex.ImaginaryOne. That would be my reasoning at least.

          1 Reply Last reply
          0
          • C Chris Maunder

            I was reading about complex numbers in C# and saw [this](https://docs.microsoft.com/en-us/dotnet/api/system.numerics.complex?view=net-5.0)

            Complex minusOne = new Complex(-1, 0);
            Console.WriteLine(Complex.Sqrt(minusOne));
            // The example displays the following output:
            // (6.12303176911189E-17, 1) on 32-bit systems.
            // (6.12323399573677E-17,1) on IA64 systems.

            I'm curious: is there any reason one would not simply hardcode Complex.Sqrt(-1) to equal new Complex(0, 1);? The whole thing about complex numbers is they are based on the fundamental concept that i2 = -1. Why wouldn't you bake that in as an absolute and let the representational errors happen elsewhere? I get that actually detecting all cases of √-1 is tricky and messy at best, but it's not like you can actually compare, with arbitrary precision, two floating point values anyway. Future warning: if I ever get access to the .NET code in a way that lets me sneak in a change, then this will happen. It may cause manned spacecraft to veer off course and crash into the moon, or nuclear reactors to overheat and take out half a continent. But, dammit, √-1 will equal i.

            cheers Chris Maunder

            G Offline
            G Offline
            Gary R Wheeler
            wrote on last edited by
            #7

            I hope you know you just induced me to have a flashback to the graduate linear systems course I took in 1988, the last time I cared that i = √-1 was a thing. I'm now going to have to spend the evening drinking hard apple cider, binge-watching Eureka[^], and talking to my sleeping greyhound in order to purge the memory from my neural cache.

            Software Zen: delete this;

            1 Reply Last reply
            0
            • C Chris Maunder

              I was reading about complex numbers in C# and saw [this](https://docs.microsoft.com/en-us/dotnet/api/system.numerics.complex?view=net-5.0)

              Complex minusOne = new Complex(-1, 0);
              Console.WriteLine(Complex.Sqrt(minusOne));
              // The example displays the following output:
              // (6.12303176911189E-17, 1) on 32-bit systems.
              // (6.12323399573677E-17,1) on IA64 systems.

              I'm curious: is there any reason one would not simply hardcode Complex.Sqrt(-1) to equal new Complex(0, 1);? The whole thing about complex numbers is they are based on the fundamental concept that i2 = -1. Why wouldn't you bake that in as an absolute and let the representational errors happen elsewhere? I get that actually detecting all cases of √-1 is tricky and messy at best, but it's not like you can actually compare, with arbitrary precision, two floating point values anyway. Future warning: if I ever get access to the .NET code in a way that lets me sneak in a change, then this will happen. It may cause manned spacecraft to veer off course and crash into the moon, or nuclear reactors to overheat and take out half a continent. But, dammit, √-1 will equal i.

              cheers Chris Maunder

              O Offline
              O Offline
              obermd
              wrote on last edited by
              #8

              That looks like an implementation error. Does the 32-bit system have a 80x87 math coprocessor?

              1 Reply Last reply
              0
              • R Rick York

                Do you really work on an Itanium system? That's what the IA-64 instruction was for - the Intel Itanium systems. The 64-bit instruction more commonly used today is called X64 and was originally developed by AMD.

                "They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"

                C Offline
                C Offline
                Chris Maunder
                wrote on last edited by
                #9

                I work on whatever Tim Cook decides should be in my Macbook.

                cheers Chris Maunder

                1 Reply Last reply
                0
                • C Chris Maunder

                  I was reading about complex numbers in C# and saw [this](https://docs.microsoft.com/en-us/dotnet/api/system.numerics.complex?view=net-5.0)

                  Complex minusOne = new Complex(-1, 0);
                  Console.WriteLine(Complex.Sqrt(minusOne));
                  // The example displays the following output:
                  // (6.12303176911189E-17, 1) on 32-bit systems.
                  // (6.12323399573677E-17,1) on IA64 systems.

                  I'm curious: is there any reason one would not simply hardcode Complex.Sqrt(-1) to equal new Complex(0, 1);? The whole thing about complex numbers is they are based on the fundamental concept that i2 = -1. Why wouldn't you bake that in as an absolute and let the representational errors happen elsewhere? I get that actually detecting all cases of √-1 is tricky and messy at best, but it's not like you can actually compare, with arbitrary precision, two floating point values anyway. Future warning: if I ever get access to the .NET code in a way that lets me sneak in a change, then this will happen. It may cause manned spacecraft to veer off course and crash into the moon, or nuclear reactors to overheat and take out half a continent. But, dammit, √-1 will equal i.

                  cheers Chris Maunder

                  S Offline
                  S Offline
                  Stuart Dootson
                  wrote on last edited by
                  #10

                  Well, I had to [try it in C++](https://godbolt.org/z/9ajWdG5j5)... ```c++ #include #include #include int main() { const auto a = std::complex{-1, 0}; const auto b = std::sqrt(a); std::cout << std::setprecision(20) << "sqrt" << a << " -> " << b << "\n"; } ``` and that output ``` sqrt(-1,0) -> (0,1) ``` in gcc and clang. And, as you have been a physicist, I figured maybe [Fortran](https://godbolt.org/z/Tzvhxv7s1) might be an alternative... ```fortran program test implicit none COMPLEX*8 a,b a = (-1,0) b = sqrt(a) write (*,*) a,b endprogram test ``` Guess what - sqrt(-1) == i there too: ``` (-1.00000000,0.00000000) (0.00000000,1.00000000) ``` And all the [other](https://godbolt.org/z/Wqa3zc9MY) [non .NET](https://godbolt.org/z/9z553jvee) [languages](https://godbolt.org/z/b8rGrqYce) I tried also gave the same answer... So I guess .NET is the outlier here! With all that, though, they're still missing the negative root :doh:

                  Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                  J M C 3 Replies Last reply
                  0
                  • J Jorgen Andersson

                    If spacecraft or nuclear reactors are ever allowed to be run on .Net I will hide in a cave for the foreseeable future.

                    Wrong is evil and must be defeated. - Jeff Ello

                    E Offline
                    E Offline
                    Eric R Johansson
                    wrote on last edited by
                    #11

                    Ehm.. As a fellow swedish developer, I suggest you hide right now, because software I've written in C# is actually in a control room for a unnamed reactor. =)

                    J 1 Reply Last reply
                    0
                    • J Jorgen Andersson

                      If spacecraft or nuclear reactors are ever allowed to be run on .Net I will hide in a cave for the foreseeable future.

                      Wrong is evil and must be defeated. - Jeff Ello

                      L Offline
                      L Offline
                      Luca Cestola
                      wrote on last edited by
                      #12

                      It is not related to language-specific issues, but to floating point representation. Every language has these problems with basic floating-point data structures.

                      J 1 Reply Last reply
                      0
                      • E Eric R Johansson

                        Ehm.. As a fellow swedish developer, I suggest you hide right now, because software I've written in C# is actually in a control room for a unnamed reactor. =)

                        J Offline
                        J Offline
                        Jorgen Andersson
                        wrote on last edited by
                        #13

                        But is it actually controlling the reactor?

                        Wrong is evil and must be defeated. - Jeff Ello

                        E 1 Reply Last reply
                        0
                        • L Luca Cestola

                          It is not related to language-specific issues, but to floating point representation. Every language has these problems with basic floating-point data structures.

                          J Offline
                          J Offline
                          Jorgen Andersson
                          wrote on last edited by
                          #14

                          My concern isn't language specific nor floating point specific. I'd rather not trust anything running on a PC for high security purposes.

                          Wrong is evil and must be defeated. - Jeff Ello

                          1 Reply Last reply
                          0
                          • J Jorgen Andersson

                            But is it actually controlling the reactor?

                            Wrong is evil and must be defeated. - Jeff Ello

                            E Offline
                            E Offline
                            Eric R Johansson
                            wrote on last edited by
                            #15

                            Not directly but a crash will cause an emergency stop. // E

                            J 1 Reply Last reply
                            0
                            • E Eric R Johansson

                              Not directly but a crash will cause an emergency stop. // E

                              J Offline
                              J Offline
                              Jorgen Andersson
                              wrote on last edited by
                              #16

                              Ok, that's interesting. :~ I suppose it's not an off the shelf PC it's running on?

                              Wrong is evil and must be defeated. - Jeff Ello

                              E 1 Reply Last reply
                              0
                              • J Jorgen Andersson

                                Ok, that's interesting. :~ I suppose it's not an off the shelf PC it's running on?

                                Wrong is evil and must be defeated. - Jeff Ello

                                E Offline
                                E Offline
                                Eric R Johansson
                                wrote on last edited by
                                #17

                                Everything runs on off the shelf pcs. That are 5+ years old. But on the other hand, the backups have backups. There's some really, REALLY old stuff in there that's custom built, but that's even more scary. That's it, I better stop before I bust some NDA and get SÄPO after my ass. =) // E

                                J 1 Reply Last reply
                                0
                                • E Eric R Johansson

                                  Everything runs on off the shelf pcs. That are 5+ years old. But on the other hand, the backups have backups. There's some really, REALLY old stuff in there that's custom built, but that's even more scary. That's it, I better stop before I bust some NDA and get SÄPO after my ass. =) // E

                                  J Offline
                                  J Offline
                                  Jorgen Andersson
                                  wrote on last edited by
                                  #18

                                  Oh fuck. :wtf: It's not like as if it's an unknown problem: Malware Discovered in German Nuclear Power Plant - Security News[^] But I really thought critical systems were running on a Safety-Critical System[^].

                                  Wrong is evil and must be defeated. - Jeff Ello

                                  E 1 Reply Last reply
                                  0
                                  • J Jorgen Andersson

                                    Oh fuck. :wtf: It's not like as if it's an unknown problem: Malware Discovered in German Nuclear Power Plant - Security News[^] But I really thought critical systems were running on a Safety-Critical System[^].

                                    Wrong is evil and must be defeated. - Jeff Ello

                                    E Offline
                                    E Offline
                                    Eric R Johansson
                                    wrote on last edited by
                                    #19

                                    Two more words: Windows. XP. :'( // E

                                    R 1 Reply Last reply
                                    0
                                    • S Stuart Dootson

                                      Well, I had to [try it in C++](https://godbolt.org/z/9ajWdG5j5)... ```c++ #include #include #include int main() { const auto a = std::complex{-1, 0}; const auto b = std::sqrt(a); std::cout << std::setprecision(20) << "sqrt" << a << " -> " << b << "\n"; } ``` and that output ``` sqrt(-1,0) -> (0,1) ``` in gcc and clang. And, as you have been a physicist, I figured maybe [Fortran](https://godbolt.org/z/Tzvhxv7s1) might be an alternative... ```fortran program test implicit none COMPLEX*8 a,b a = (-1,0) b = sqrt(a) write (*,*) a,b endprogram test ``` Guess what - sqrt(-1) == i there too: ``` (-1.00000000,0.00000000) (0.00000000,1.00000000) ``` And all the [other](https://godbolt.org/z/Wqa3zc9MY) [non .NET](https://godbolt.org/z/9z553jvee) [languages](https://godbolt.org/z/b8rGrqYce) I tried also gave the same answer... So I guess .NET is the outlier here! With all that, though, they're still missing the negative root :doh:

                                      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                                      J Offline
                                      J Offline
                                      jsc42
                                      wrote on last edited by
                                      #20

                                      Are we sure that we are comparing like-with-like? The original answers were (6.12303176911189E-17, 1) or (6.12323399573677E-17,1) Your answers were (0.00000000,1.00000000) If you display 6.12303176911189E-17 in a non-exponent form, it will come out as 0.00000000 because it is a rounded version of 0.000000000000000006123... (I may have miscounted the zeros) So both answers may be representations of the same number. This is easy to test e.g. in FORTRAN which has an E format type for exponential and F for floating point.

                                      C S 2 Replies Last reply
                                      0
                                      • S Stuart Dootson

                                        Well, I had to [try it in C++](https://godbolt.org/z/9ajWdG5j5)... ```c++ #include #include #include int main() { const auto a = std::complex{-1, 0}; const auto b = std::sqrt(a); std::cout << std::setprecision(20) << "sqrt" << a << " -> " << b << "\n"; } ``` and that output ``` sqrt(-1,0) -> (0,1) ``` in gcc and clang. And, as you have been a physicist, I figured maybe [Fortran](https://godbolt.org/z/Tzvhxv7s1) might be an alternative... ```fortran program test implicit none COMPLEX*8 a,b a = (-1,0) b = sqrt(a) write (*,*) a,b endprogram test ``` Guess what - sqrt(-1) == i there too: ``` (-1.00000000,0.00000000) (0.00000000,1.00000000) ``` And all the [other](https://godbolt.org/z/Wqa3zc9MY) [non .NET](https://godbolt.org/z/9z553jvee) [languages](https://godbolt.org/z/b8rGrqYce) I tried also gave the same answer... So I guess .NET is the outlier here! With all that, though, they're still missing the negative root :doh:

                                        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                                        C Offline
                                        C Offline
                                        Chris Maunder
                                        wrote on last edited by
                                        #21

                                        That is awesome. Thank you.

                                        cheers Chris Maunder

                                        1 Reply Last reply
                                        0
                                        • S Stuart Dootson

                                          Well, I had to [try it in C++](https://godbolt.org/z/9ajWdG5j5)... ```c++ #include #include #include int main() { const auto a = std::complex{-1, 0}; const auto b = std::sqrt(a); std::cout << std::setprecision(20) << "sqrt" << a << " -> " << b << "\n"; } ``` and that output ``` sqrt(-1,0) -> (0,1) ``` in gcc and clang. And, as you have been a physicist, I figured maybe [Fortran](https://godbolt.org/z/Tzvhxv7s1) might be an alternative... ```fortran program test implicit none COMPLEX*8 a,b a = (-1,0) b = sqrt(a) write (*,*) a,b endprogram test ``` Guess what - sqrt(-1) == i there too: ``` (-1.00000000,0.00000000) (0.00000000,1.00000000) ``` And all the [other](https://godbolt.org/z/Wqa3zc9MY) [non .NET](https://godbolt.org/z/9z553jvee) [languages](https://godbolt.org/z/b8rGrqYce) I tried also gave the same answer... So I guess .NET is the outlier here! With all that, though, they're still missing the negative root :doh:

                                          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                                          M Offline
                                          M Offline
                                          Member_15089351
                                          wrote on last edited by
                                          #22

                                          I had a message-passing algorithm written in MATLAB, but our SW Dev department wanted it written in C#. After I wrote it in C#, I found a numerical instability in the C# version. Later I wanted to run the same algorithm on a Spark Cluster, so I rewrote it again in Python. The MATLAB version running on my Windows 10 machine yields the same answers as the Python version (whether I run on my local machine or a Spark Cluster in Azure). The only fix I could find for C# was to round the output of one method to 11 or 12 decimal digits so that the numbers that ought to remain identical would remain identical. I think something funky is happening to double precision (maybe all floating point) calculations in .NET. Thanks for introducing me to GodBolt.org. I think it's going to be very useful.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups