Complex Number Literals?

Topics: VB Language Design
Apr 4, 2014 at 11:34 AM

Complex Number Literals?

What about expanding the literals to include complex numbers?
Dim x = -12.34+-56.78i
' X : Numerics.Complex
At the moment is it being parsed as.
Dim x = (((-12.34) + (-56.78)) (i))
' x : Double
If i is already defined. Parsing would result in a error. End of statement expected

So I don't think extending parsing to include complex numbers would introduce any issues, related to backwards compatibility.
Argument
But wouldn't this potentially produce code that compiles, if i was a typing error? Previously it wouldn't haven't compiled.
It now would compile (or be inferred) as different type so would introduce compile time errors where it is being used.

BNF
    Digit_Zero  ::= '0'
    Digit_One   ::= '1'
    Digit_Two   ::= '2'
    Digit_Three ::= '3'
    Digit_Four  ::= '4'
    Digit_Five  ::= '5'
    Digit_Six   ::= '6'
    Digit_Seven ::= '7'
    Digit_Eight ::= '8'
    Digit_Nine  ::= '9'
     ZeroToNine ::= Digit_Zero  | Digit_One   | Digit_Two   | Digit_Three | Digit_Four  |  Digit_Five  | Digit_Six   | Digit_Seven | Digit_Eight | Digit_Nine
 Complex_Symbol ::= 'i'
          Digit ::= ZeroToNine
         Digits ::= Digit+
Negative_Symbol ::= '-'
Positive_Symbol ::= '+'
           Sign ::= Negative_Symbol | Positive_Symbol
 Integer_Number ::= (Sign)? Digits
  Decimal_Point ::= '.'
     Float_Part ::= Decimal_Point Digits
    Real_Number ::= Integer_Number Float_Part
   Complex_Part ::= Real_Number Complex_Symbol
      Real_Part ::= Real_Number
   Complex_Glue ::= '+'
 Complex_Number ::= Real_Part Complex_Glue Complex_Part 
Parse Tree
 -  1  2  .  3  4  +  -  5  6  .  7  8  i
 |  |  |  |  |  |  |  |  |  |  |  |  |  |
S- D1 D2 Dp D3 D4 S+ S- D5 D6 DP D7 D8 Si
   |--+|    |--+|       |--+|    |--+|
      |        |           |        |
S-    D+ Dp    D+ S+ S-    D+ DP    D+ Si
|-----+| |-----+|    |-----+| |-----+|
      |        |           |        |  
      NI       NF S+       NI       NF Si
      |--------+|          |--------+|
               |                    |
               NR S+                NR SI
                                    |--+|
                                       |
               NR S+                  Ni
               |----------------------+|
                                      |
                                      Nc
Apr 5, 2014 at 11:39 AM
I think a feature that saves a single character is not worth it. You can do this today:
var x = -12.34+-56.78*i;
I think the same code will work in VB too. Here, i is static member of some helper type, which can be referenced directly thanks to static using (new feature in C# 6.0, apparently VB already supports it).
Apr 6, 2014 at 3:11 AM
@svick I think you've miss understood this. i is not a variable but a literal identifier like &H is a literal identifier for hexadecimal.
Apr 6, 2014 at 10:23 AM
I understand that's what you're proposing. But I don't understand the reason for that, when you can do pretty much the same thing without a literal identifier.
Apr 6, 2014 at 12:51 PM
I don't think you get it. It's not 2 numbers (-12.34 and -56.78) but one complex number -12.34+56.78 i
var x =-12.34 + -56.78i;
       |    |   |     |
       +----+   +-----+
       |Real|   |Imgny|
       +----+---+-----+
       |Complex Number|
       +--------------+
Using your method would involve at least at a minimum 3 operators (or 5 if the minus signs counts). i would then have to a special type just to get it work with the type-checker. Plus it it'll involve an addition operator overload adding to Complex.
var = (Double) + ((Double)*(ImaginarySymbol)
      (Double) + (Complex)
      (Complex)
My way it is defined in one initialisation of an instance of complex.

Also i is often used as an identifier, typically for iteration number.
for( i=0; i<10; i++)
Developer
Apr 9, 2014 at 11:43 PM
Why is this better than defining your own struct type with overloaded operators? I guess the worst part is writing literals

new Complex(2.0, 3.0)

Is that really so bad?
Apr 10, 2014 at 3:31 PM
Why create my own? When it already exists System.Numerics.Complex also if the integer literal is too big for a int or long why not extend it to BigInteger?

Assuming the target framework is at least .net 4.0
Apr 13, 2014 at 10:39 AM
@AdamSpeight2008: svick understands your proposal completely; he knows that you are suggesting to introduce complex-number literals into the langugage.

He is telling you that your proposal doesn’t add much compared to what you can already do. You can already use a static field to achieve pretty much the same thing. Only instead of i you’d have to write Complex.ImaginaryOne, which is admittedly a lot longer, but complex numbers aren’t that common.