Announcement

Collapse
No announcement yet.

Is the pipe operator strictly necessary for members which have been assigned inline?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    Is the pipe operator strictly necessary for members which have been assigned inline?

    The following deck shows the case:
    https://deck.net/86f705da5d243a5c1679e1d7e878a0cf

    Line 19:
    Demo.Program.i = (Demo.Program.i + 1) | 0;
    Is the | 0; portion at the end strictly needed? It actually seems like this isn't technically correct from the C# standpoint. If the member i ever becomes undefined, I would not expect the increment to set it to zero. I might expect it to return NaN. Whats the history behind using | [default value type]? Am I missing something?
    Last edited by geoffrey.mcgill; 2018-01-17 @ 10:52 AM.

    #2
    Hi selinc,

    The | 0; part is required to control range overflow correctly because in JavaScript numbers are stored as 64 bits floating numbers.

    At the first glance | 0; could be confusing, i.e. the fact that bitwise OR does what we need. That's possible because JavaScript performs bitwise operations on 32 bits numbers. So | 0; operation actually equals to the following (probably more clear definition):
     
     Demo.Program.i = (Demo.Program.i + 1) & 0xFFFFFFFF;

    The Deck sample below demonstrates overflow issue and how it's handled for different number types:
    https://deck.net/6480e2659a5322c8ae8e506cb89b4f95
    You can compare the results to .NET Fiddle output:
    https://dotnetfiddle.net/YvRl91

    As for undefined issue - I agree, I'd also expect NaN in the scenario you described. We're investigating how it's possible to address it without affecting performance.

    Comment


      #3
      I think I was confused. I'm not sure what the | does in the undefined case. I'm not too familiar with javascript.

      Comment

      Working...
      X