Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is that the literal "99" is being treated as an int. If you add "L" it will treat it as a long. To fix your compilation problem:</p> <pre><code>long test2 = 99L * 99L * 99L * 99L * 99L; </code></pre> <p>And to fix the "incorrect result" caused by integer overflow:</p> <pre><code>long r = 99; long test1 = r * r * r * r * r; </code></pre> <p>The key point is that the expression to the right of the "=" is evaluated <em>before</em> the assignment to <code>long r</code> is done.</p> <p>There are other literal suffixes you might be interested in:</p> <pre><code>Type Suffix Example uint U or u 100U long L or l 100L ulong UL or ul 100UL float F or f 123.45F decimal M or m 123.45M </code></pre> <p>@m.edmonson, regarding your question about why it comes out to 919965907. What's happening, is that the value is "wrapping" around int.MaxValue. You can see this with a little test program: </p> <pre><code>int i = 99; // 99 i *= 99; // 9801 i *= 99; // 970299 i *= 99; // 96059601 i *= 99; // 919965907 should be 9509900499 but comes out to 919965907 // which is (9509900499 % int.MaxValue) long k = 9509900499 % int.MaxValue; </code></pre> <p>What is meant by "wrapping around"? When you exceed <code>int.MaxValue</code> by 1, the value "goes back" to <code>int.MinValue</code>.</p> <pre><code>int j = int.MaxValue; j++; bool isNowMinValue = (j == int.MinValue); // true, the value has "wrapped around" </code></pre> <p>This is a bit simplistic; if you search for "integer overflow" you will get a better explanation. It's worth understanding how integers (and other numeric types) are represented with 32 bits:</p> <p><a href="http://en.wikipedia.org/wiki/Signed_number_representations" rel="noreferrer">http://en.wikipedia.org/wiki/Signed_number_representations</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload