(Me || Compiler) == Stupid

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven Nagy

    #1

    (Me || Compiler) == Stupid

    private void Test(short i, short j)
    {
    short k = i + j;
    }

    What the hell is wrong with this code?
    "Can not implicitly convert type int to short" is the compile error.
    ASP.NET 1.1 C#

    Thanks

  • Marc Gravell

    #2
    Re: (Me || Compiler) == Stupid

    It isn't very intuitive, granted, but you just need to cast:

    short k = (short)(i + j);

    Discussed more here (but doesn't really state "why"):
    http://msdn2.microsoft.com/en-us/library/ybs77ex4.aspx

    Marc


    Comment

    • Alvaro Enríquez de Luna

      #3
      RE: (Me || Compiler) == Stupid

      'Short + short' can be bigger than a 'short', and you can not implicitly
      convert it to a short value due to the size. You have to do it explicitly:

      private void Test(short i, short j)
      {
      short k = (short) (i + j);
      }

      "Steven Nagy" wrote:
      [color=blue]
      > private void Test(short i, short j)
      > {
      > short k = i + j;
      > }
      >
      > What the hell is wrong with this code?
      > "Can not implicitly convert type int to short" is the compile error.
      > ASP.NET 1.1 C#
      >
      > Thanks
      >
      >[/color]

      Comment

      • Marc Gravell

        #4
        Re: (Me || Compiler) == Stupid

        To me this is an inconsistency in the framework;

        Int32 + Int32 can be longer than an Int32 (int), but it does this silently
        (wrapping), returning an Int32.
        Likewise with Int64 (long), returning an Int64; yet Int16 (short) returns an
        Int32 on summation.

        *Presumably* this was for some some meaningful reason... not quite sure what
        it is though; anybody?

        Marc


        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: (Me || Compiler) == Stupid

          Marc Gravell wrote:[color=blue]
          > To me this is an inconsistency in the framework;
          >
          > Int32 + Int32 can be longer than an Int32 (int), but it does this silently
          > (wrapping), returning an Int32.
          > Likewise with Int64 (long), returning an Int64; yet Int16 (short) returns an
          > Int32 on summation.
          >
          > *Presumably* this was for some some meaningful reason... not quite sure what
          > it is though; anybody?[/color]

          Performance. Processors are generally happier doing Int32+Int32 than
          Int16+Int16, so unless you specify that you want to bound it, the
          language assumes it's okay to do things the fast way.

          Jon

          Comment

          • Marc Gravell

            #6
            Re: (Me || Compiler) == Stupid

            There we are; and yes, that does seem like a pretty good reason to me ;-p
            (not that I work with Int16 very often...)

            Cheers,

            Marc


            Comment

            • Steven Nagy

              #7
              Re: (Me || Compiler) == Stupid

              Thanks for the response guys.
              If its a processor performance thing as Jon suggested, then I am
              completely ok with it.
              If its anything other, then fooey on C#.

              Perhaps it is important for me to pose this question as well then.
              The reason I went for a (short) is because I figured it is the ideal
              data type because my values won't go over 20.
              Basically, its a mini chess application and I have an array declared:
              short[,] Board;
              Given Jon's statement about CPU performance, would I be better to have
              declared as an int instead?
              There won't be many arithmetic operations, mostly just comparison. The
              arithmetic comes in when checking if moves are valid and so on.
              It all works fine currently as a short. Whats the recommondation?
              Preserve current structure as short, or do a Find/Replace with int ?
              (Note, if you think there is even a minor performance improvement
              either way, even if its not going to be noticeable, please indicate
              so!)

              Thanks

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: (Me || Compiler) == Stupid

                Steven Nagy <learndotnet@ho tmail.com> wrote:[color=blue]
                > Thanks for the response guys.
                > If its a processor performance thing as Jon suggested, then I am
                > completely ok with it.
                > If its anything other, then fooey on C#.
                >
                > Perhaps it is important for me to pose this question as well then.
                > The reason I went for a (short) is because I figured it is the ideal
                > data type because my values won't go over 20.[/color]

                Why not use byte then, out of interest?
                [color=blue]
                > Basically, its a mini chess application and I have an array declared:
                > short[,] Board;
                > Given Jon's statement about CPU performance, would I be better to have
                > declared as an int instead?[/color]

                I doubt that it'll make a significant difference for you - but if you
                have a *lot* of boards in memory at a time, then the memory difference
                could be significant.
                [color=blue]
                > There won't be many arithmetic operations, mostly just comparison. The
                > arithmetic comes in when checking if moves are valid and so on.
                > It all works fine currently as a short. Whats the recommondation?
                > Preserve current structure as short, or do a Find/Replace with int ?
                > (Note, if you think there is even a minor performance improvement
                > either way, even if its not going to be noticeable, please indicate
                > so!)[/color]

                I'd consider going to byte, but wouldn't move to int without
                benchmarking first. You *may* find a minor performance improvement, but
                I doubt it personally.

                --
                Jon Skeet - <skeet@pobox.co m>
                http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                If replying to the group, please do not mail me too

                Comment

                • Steven Nagy

                  #9
                  Re: (Me || Compiler) == Stupid

                  Ok thanks Jon.
                  yeah I don't know why I didn't go byte, perhaps because I am mental.

                  Yes, there could be lots of boards in memory at once in the future,
                  which is what I am catering for now.

                  In the trade off of faster cpu vs less ram, I guess I have the dilemna
                  of:

                  If I go for CPU, the request can be completed quicker, and then garbage
                  collection returns my ram.
                  If I go for RAM, then I can handle more simultaneous requests, but they
                  process slower.

                  Given a web site context (ASP.NET) then which should I choose?
                  Process the requests faster, or preserve as much ram as possible?

                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: (Me || Compiler) == Stupid

                    Steven Nagy <learndotnet@ho tmail.com> wrote:[color=blue]
                    > Ok thanks Jon.
                    > yeah I don't know why I didn't go byte, perhaps because I am mental.
                    >
                    > Yes, there could be lots of boards in memory at once in the future,
                    > which is what I am catering for now.
                    >
                    > In the trade off of faster cpu vs less ram, I guess I have the dilemna
                    > of:
                    >
                    > If I go for CPU, the request can be completed quicker, and then garbage
                    > collection returns my ram.
                    > If I go for RAM, then I can handle more simultaneous requests, but they
                    > process slower.
                    >
                    > Given a web site context (ASP.NET) then which should I choose?
                    > Process the requests faster, or preserve as much ram as possible?[/color]

                    I doubt whether the difference in either will be signficant, to be
                    honest - but you won't know without benchmarking (as close to real
                    usage as possible).

                    --
                    Jon Skeet - <skeet@pobox.co m>
                    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                    If replying to the group, please do not mail me too

                    Comment

                    • Steven Nagy

                      #11
                      Re: (Me || Compiler) == Stupid

                      Ok cheers mate

                      Comment

                      Working...