No operators defined on ushort

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Axel Dahmen

    No operators defined on ushort

    Hi,

    I want to start a technical discussion on the fact that C# doesn't define
    any mathematical operators on other integral types than int.

    Simple things like the following just aren't possible in C#:

    ushort a,b,c;

    a = b = 0;
    c = a - b;

    Whatever integral data type you use, you'll always get an error "can't
    implicitly convert to int." What's the purpose of this?? If the language
    provides types like byte or ushort I want to use them. I want to see
    overflow errors whenever routines miscalculate something.

    What the compiler does here is technically and mathematically incomplete.
    And it should be fixed ASAP.

    Anyone?

    Axel Dahmen




  • Yves Tourchot

    #2
    Re: No operators defined on ushort


    Take a look at these:





    "Axel Dahmen" <NoSpam@NoOneKn ows.de> wrote in message
    news:exzuatimDH A.1084@tk2msftn gp13.phx.gbl...[color=blue]
    > Hi,
    >
    > I want to start a technical discussion on the fact that C# doesn't define
    > any mathematical operators on other integral types than int.
    >
    > Simple things like the following just aren't possible in C#:
    >
    > ushort a,b,c;
    >
    > a = b = 0;
    > c = a - b;
    >
    > Whatever integral data type you use, you'll always get an error "can't
    > implicitly convert to int." What's the purpose of this?? If the language
    > provides types like byte or ushort I want to use them. I want to see
    > overflow errors whenever routines miscalculate something.
    >
    > What the compiler does here is technically and mathematically incomplete.
    > And it should be fixed ASAP.
    >
    > Anyone?
    >
    > Axel Dahmen
    >
    >
    >
    >[/color]


    Comment

    • Axel Dahmen

      #3
      Re: No operators defined on ushort

      Well, Yves, I know these documents. Though they explain the effect itself
      and how to deal with C#'s incomplete data type implementation, they don't
      discuss the fact that the missing operators render C# incomplete.

      Here is my point of view: Mathematically and in Informatics science, a
      native scalar data type establishes a K vector space. - In C#, only the
      integer data type fulfills this requirement. All other integral C# data
      types are crippled so far.

      Regarding this aspect, C# is faulty. To get a truly comprehensive (a
      "valid") language, C# needs to add operators to all native scalar data
      types.

      I might be wrong, but in case I'm not, the arguments in this discussion
      should lead to implementing the missing operators into C#.



      -----------------------------------
      "Yves Tourchot" <devnetsol_rems pam_@sympatico. ca> schrieb im Newsbeitrag
      news:ezamb.1386 6$aw5.427596@ne ws20.bellglobal .com...[color=blue]
      >
      > Take a look at these:
      >
      >[/color]

      vclrfshort.asp[color=blue]
      >[/color]

      vclrfimplicitnu mericconversion .asp[color=blue]
      >
      >
      > "Axel Dahmen" <NoSpam@NoOneKn ows.de> wrote in message
      > news:exzuatimDH A.1084@tk2msftn gp13.phx.gbl...[color=green]
      > > Hi,
      > >
      > > I want to start a technical discussion on the fact that C# doesn't[/color][/color]
      define[color=blue][color=green]
      > > any mathematical operators on other integral types than int.
      > >
      > > Simple things like the following just aren't possible in C#:
      > >
      > > ushort a,b,c;
      > >
      > > a = b = 0;
      > > c = a - b;
      > >
      > > Whatever integral data type you use, you'll always get an error "can't
      > > implicitly convert to int." What's the purpose of this?? If the language
      > > provides types like byte or ushort I want to use them. I want to see
      > > overflow errors whenever routines miscalculate something.
      > >
      > > What the compiler does here is technically and mathematically[/color][/color]
      incomplete.[color=blue][color=green]
      > > And it should be fixed ASAP.
      > >
      > > Anyone?
      > >
      > > Axel Dahmen
      > >
      > >
      > >
      > >[/color]
      >
      >[/color]




      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: No operators defined on ushort

        Axel Dahmen <NoSpam@NoOneKn ows.de> wrote:[color=blue]
        > Well, Yves, I know these documents. Though they explain the effect itself
        > and how to deal with C#'s incomplete data type implementation, they don't
        > discuss the fact that the missing operators render C# incomplete.
        >
        > Here is my point of view: Mathematically and in Informatics science, a
        > native scalar data type establishes a K vector space. - In C#, only the
        > integer data type fulfills this requirement. All other integral C# data
        > types are crippled so far.
        >
        > Regarding this aspect, C# is faulty. To get a truly comprehensive (a
        > "valid") language, C# needs to add operators to all native scalar data
        > types.
        >
        > I might be wrong, but in case I'm not, the arguments in this discussion
        > should lead to implementing the missing operators into C#.[/color]

        I think the arguments in this discussion would be much better placed if
        they were concerned with usefulness rather than academic completeness.

        Whether or not C#'s non-int data types are "crippled" doesn't seem to
        have stopped people writing C# code which works, does it? The question
        shouldn't be whether or not C# is "incomplete ", it's whether it can be
        made potentially *more* useful than it already is.

        I would certainly be interested to hear from the language authors on
        why these choices were made, but if there are some appropriate
        practical reasons, I for one have no problem in sacrificing
        "completene ss" for "usefulness ".

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • mikeb

          #5
          Re: No operators defined on ushort

          Axel Dahmen wrote:
          [color=blue]
          > Well, Yves, I know these documents. Though they explain the effect itself
          > and how to deal with C#'s incomplete data type implementation, they don't
          > discuss the fact that the missing operators render C# incomplete.
          >
          > Here is my point of view: Mathematically and in Informatics science, a
          > native scalar data type establishes a K vector space. - In C#, only the
          > integer data type fulfills this requirement. All other integral C# data
          > types are crippled so far.
          >
          > Regarding this aspect, C# is faulty. To get a truly comprehensive (a
          > "valid") language, C# needs to add operators to all native scalar data
          > types.
          >
          > I might be wrong, but in case I'm not, the arguments in this discussion
          > should lead to implementing the missing operators into C#.
          >[/color]

          ANSI C and C++ also always perform integer arithmetic using int (or
          larger) types. It's just that in these languages you do not get an
          error when assigning an int into a smaller integral type (you might get
          a warning, though). The C# language designers decided that silently
          truncating integral values was the source of enough bugs that they do
          not allow implicit conversions if the conversion can potentially lose data.
          [color=blue]
          >
          >
          > -----------------------------------
          > "Yves Tourchot" <devnetsol_rems pam_@sympatico. ca> schrieb im Newsbeitrag
          > news:ezamb.1386 6$aw5.427596@ne ws20.bellglobal .com...
          >[color=green]
          >>Take a look at these:
          >>
          >>[/color]
          >
          > http://msdn.microsoft.com/library/de...us/csref/html/
          > vclrfshort.asp
          >
          > http://msdn.microsoft.com/library/de...us/csref/html/
          > vclrfimplicitnu mericconversion .asp
          >[color=green]
          >>
          >>"Axel Dahmen" <NoSpam@NoOneKn ows.de> wrote in message
          >>news:exzuatim DHA.1084@tk2msf tngp13.phx.gbl. ..
          >>[color=darkred]
          >>>Hi,
          >>>
          >>>I want to start a technical discussion on the fact that C# doesn't[/color][/color]
          >
          > define
          >[color=green][color=darkred]
          >>>any mathematical operators on other integral types than int.
          >>>
          >>>Simple things like the following just aren't possible in C#:
          >>>
          >>> ushort a,b,c;
          >>>
          >>> a = b = 0;
          >>> c = a - b;
          >>>
          >>>Whatever integral data type you use, you'll always get an error "can't
          >>>implicitly convert to int." What's the purpose of this?? If the language
          >>>provides types like byte or ushort I want to use them. I want to see
          >>>overflow errors whenever routines miscalculate something.
          >>>
          >>>What the compiler does here is technically and mathematically[/color][/color]
          >
          > incomplete.
          >[color=green][color=darkred]
          >>>And it should be fixed ASAP.
          >>>
          >>>Anyone?
          >>>
          >>>Axel Dahmen
          >>>
          >>>
          >>>
          >>>[/color]
          >>
          >>[/color]
          >
          >
          >
          >[/color]

          --
          mikeb

          Comment

          • Kenneth Baltrinic

            #6
            Re: No operators defined on ushort

            The following works for me. It would seem that the subtraction operator is
            defined but returns an int. Go figure.

            ushort a,b,c;


            a = b = 0;
            c = (ushort)(a - b);


            --Ken

            "Axel Dahmen" <NoSpam@NoOneKn ows.de> wrote in message
            news:exzuatimDH A.1084@tk2msftn gp13.phx.gbl...[color=blue]
            > Hi,
            >
            > I want to start a technical discussion on the fact that C# doesn't define
            > any mathematical operators on other integral types than int.
            >
            > Simple things like the following just aren't possible in C#:
            >
            > ushort a,b,c;
            >
            > a = b = 0;
            > c = a - b;
            >
            > Whatever integral data type you use, you'll always get an error "can't
            > implicitly convert to int." What's the purpose of this?? If the language
            > provides types like byte or ushort I want to use them. I want to see
            > overflow errors whenever routines miscalculate something.
            >
            > What the compiler does here is technically and mathematically incomplete.
            > And it should be fixed ASAP.
            >
            > Anyone?
            >
            > Axel Dahmen
            >
            >
            >
            >[/color]


            Comment

            • Axel Dahmen

              #7
              Re: No operators defined on ushort

              Jon,

              adding the missing operators has practical impact, of course. First of all
              it would make an end to the clumsy spelling. Furthermore, ask yourself: Why
              are the smaller integral types implemented into a language anyway?

              However, your view regarding practical issues is wrong because this
              discussion is not about the semantics of .NET, the syntax of C# or Visual
              Studio features but about C#'s completelyness. Just because people write
              programs in C# and these programs work doesn't mean everything in the
              language is correct. I might say for one, the job simply isn't done yet.



              ------------------------------------------------------------------
              "Jon Skeet [C# MVP]" <skeet@pobox.co m> schrieb im Newsbeitrag
              news:MPG.1a03c8 40f5f99a3c98993 c@msnews.micros oft.com...[color=blue]
              > Axel Dahmen <NoSpam@NoOneKn ows.de> wrote:[color=green]
              > > Well, Yves, I know these documents. Though they explain the effect[/color][/color]
              itself[color=blue][color=green]
              > > and how to deal with C#'s incomplete data type implementation, they[/color][/color]
              don't[color=blue][color=green]
              > > discuss the fact that the missing operators render C# incomplete.
              > >
              > > Here is my point of view: Mathematically and in Informatics science, a
              > > native scalar data type establishes a K vector space. - In C#, only the
              > > integer data type fulfills this requirement. All other integral C# data
              > > types are crippled so far.
              > >
              > > Regarding this aspect, C# is faulty. To get a truly comprehensive (a
              > > "valid") language, C# needs to add operators to all native scalar data
              > > types.
              > >
              > > I might be wrong, but in case I'm not, the arguments in this discussion
              > > should lead to implementing the missing operators into C#.[/color]
              >
              > I think the arguments in this discussion would be much better placed if
              > they were concerned with usefulness rather than academic completeness.
              >
              > Whether or not C#'s non-int data types are "crippled" doesn't seem to
              > have stopped people writing C# code which works, does it? The question
              > shouldn't be whether or not C# is "incomplete ", it's whether it can be
              > made potentially *more* useful than it already is.
              >
              > I would certainly be interested to hear from the language authors on
              > why these choices were made, but if there are some appropriate
              > practical reasons, I for one have no problem in sacrificing
              > "completene ss" for "usefulness ".[/color]







              Comment

              • Axel Dahmen

                #8
                Re: No operators defined on ushort

                No, they both don't perform integer arithmetic using int (if they do, it's a
                a compiler peculiarity, not a language feature). And you don't get a warning
                either.

                I guess if someone's choosing a particular data type, he's aware of its
                peculiarities. In fact, he might be using it *because* of its peculiarities.
                If he doesn't, he might simply use int anyway.

                The .NET runtime may perform its integral calculations internally by using
                int on PC platforms; however, I don't mind. I want to discuss about C# as a
                platform independant language and some lack therein.


                --------------------------------
                "mikeb" <mailbox.google @mailnull.com> schrieb im Newsbeitrag
                news:#WCOvnomDH A.1728@TK2MSFTN GP09.phx.gbl...[color=blue]
                > Axel Dahmen wrote:
                >[color=green]
                > > Well, Yves, I know these documents. Though they explain the effect[/color][/color]
                itself[color=blue][color=green]
                > > and how to deal with C#'s incomplete data type implementation, they[/color][/color]
                don't[color=blue][color=green]
                > > discuss the fact that the missing operators render C# incomplete.
                > >
                > > Here is my point of view: Mathematically and in Informatics science, a
                > > native scalar data type establishes a K vector space. - In C#, only the
                > > integer data type fulfills this requirement. All other integral C# data
                > > types are crippled so far.
                > >
                > > Regarding this aspect, C# is faulty. To get a truly comprehensive (a
                > > "valid") language, C# needs to add operators to all native scalar data
                > > types.
                > >
                > > I might be wrong, but in case I'm not, the arguments in this discussion
                > > should lead to implementing the missing operators into C#.
                > >[/color]
                >
                > ANSI C and C++ also always perform integer arithmetic using int (or
                > larger) types. It's just that in these languages you do not get an
                > error when assigning an int into a smaller integral type (you might get
                > a warning, though). The C# language designers decided that silently
                > truncating integral values was the source of enough bugs that they do
                > not allow implicit conversions if the conversion can potentially lose[/color]
                data.















                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: No operators defined on ushort

                  Axel Dahmen <NoSpam@NoOneKn ows.de> wrote:[color=blue]
                  > adding the missing operators has practical impact, of course. First of all
                  > it would make an end to the clumsy spelling.[/color]

                  Do you mean casting?
                  [color=blue]
                  > Furthermore, ask yourself: Why
                  > are the smaller integral types implemented into a language anyway?[/color]

                  I'm not trying to defend the lack of operators on those types - as I
                  said, I don't know the reasons myself, and would be interested to hear
                  them. I was suggesting that you were coming at the issue from an
                  unhelpful angle.
                  [color=blue]
                  > However, your view regarding practical issues is wrong because this
                  > discussion is not about the semantics of .NET, the syntax of C# or Visual
                  > Studio features but about C#'s completelyness. Just because people write
                  > programs in C# and these programs work doesn't mean everything in the
                  > language is correct. I might say for one, the job simply isn't done yet.[/color]

                  It doesn't mean everything in the language is as good as it could be,
                  but that's a long way from your view that the language is "crippled"
                  and that it's not "valid" which makes it sound completely unusable.

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too

                  Comment

                  • Eric Gunnerson [MS]

                    #10
                    Re: No operators defined on ushort

                    The runtime only defines operations for number of 4 or 8 byte length. I'm
                    not one of the CLR architects, but my understanding is that this was done
                    for a couple of reasons. First, operations on 4 or 8 byte numbers are more
                    efficient than smaller sizes on many processors. Also, adding in additional
                    support for smaller types would require more work and complexity both in the
                    runtime and the JIT.

                    --
                    Eric Gunnerson

                    Visit the C# product team at http://www.csharp.net
                    Eric's blog is at http://blogs.gotdotnet.com/ericgu/

                    This posting is provided "AS IS" with no warranties, and confers no rights.
                    "Axel Dahmen" <NoSpam@NoOneKn ows.de> wrote in message
                    news:OASi17nmDH A.3316@tk2msftn gp13.phx.gbl...[color=blue]
                    > Well, Yves, I know these documents. Though they explain the effect itself
                    > and how to deal with C#'s incomplete data type implementation, they don't
                    > discuss the fact that the missing operators render C# incomplete.
                    >
                    > Here is my point of view: Mathematically and in Informatics science, a
                    > native scalar data type establishes a K vector space. - In C#, only the
                    > integer data type fulfills this requirement. All other integral C# data
                    > types are crippled so far.
                    >
                    > Regarding this aspect, C# is faulty. To get a truly comprehensive (a
                    > "valid") language, C# needs to add operators to all native scalar data
                    > types.
                    >
                    > I might be wrong, but in case I'm not, the arguments in this discussion
                    > should lead to implementing the missing operators into C#.
                    >
                    >
                    >
                    > -----------------------------------
                    > "Yves Tourchot" <devnetsol_rems pam_@sympatico. ca> schrieb im Newsbeitrag
                    > news:ezamb.1386 6$aw5.427596@ne ws20.bellglobal .com...[color=green]
                    > >
                    > > Take a look at these:
                    > >
                    > >[/color]
                    >[/color]
                    http://msdn.microsoft.com/library/de...us/csref/html/[color=blue]
                    > vclrfshort.asp[color=green]
                    > >[/color]
                    >[/color]
                    http://msdn.microsoft.com/library/de...us/csref/html/[color=blue]
                    > vclrfimplicitnu mericconversion .asp[color=green]
                    > >
                    > >
                    > > "Axel Dahmen" <NoSpam@NoOneKn ows.de> wrote in message
                    > > news:exzuatimDH A.1084@tk2msftn gp13.phx.gbl...[color=darkred]
                    > > > Hi,
                    > > >
                    > > > I want to start a technical discussion on the fact that C# doesn't[/color][/color]
                    > define[color=green][color=darkred]
                    > > > any mathematical operators on other integral types than int.
                    > > >
                    > > > Simple things like the following just aren't possible in C#:
                    > > >
                    > > > ushort a,b,c;
                    > > >
                    > > > a = b = 0;
                    > > > c = a - b;
                    > > >
                    > > > Whatever integral data type you use, you'll always get an error "can't
                    > > > implicitly convert to int." What's the purpose of this?? If the[/color][/color][/color]
                    language[color=blue][color=green][color=darkred]
                    > > > provides types like byte or ushort I want to use them. I want to see
                    > > > overflow errors whenever routines miscalculate something.
                    > > >
                    > > > What the compiler does here is technically and mathematically[/color][/color]
                    > incomplete.[color=green][color=darkred]
                    > > > And it should be fixed ASAP.
                    > > >
                    > > > Anyone?
                    > > >
                    > > > Axel Dahmen
                    > > >
                    > > >
                    > > >
                    > > >[/color]
                    > >
                    > >[/color]
                    >
                    >
                    >
                    >[/color]


                    Comment

                    • mikeb

                      #11
                      Re: No operators defined on ushort

                      Axel Dahmen wrote:
                      [color=blue]
                      > No, they both don't perform integer arithmetic using int (if they do, it's a
                      > a compiler peculiarity, not a language feature).[/color]

                      I'm not sure I call it a feature - it's simply part of the language
                      standard. Please excuse any errors below when I quote from the standard
                      - the PDF I have apparently does not permit copying to the clipboard.

                      In my copy of the C++ standard (ISO/IEC 14882:1998(E)), the sections for
                      various operators (for example 5.6 Multiplicative operators and 5.7
                      Additive operators) say that "the usual arithmetic conversions are
                      performed" on the operands.

                      The usual arithmetic conversions are defined at the start of section 5:

                      =============== =============== =============== =============== =======
                      - if either operand is of type long double, the other shall be
                      converted to long double.
                      - Otherwise, if either operand is double, the other shall be
                      converted to double.
                      - Otherwise, if either operand is float, the other shall be
                      converted to float.
                      - Otherwise, the integral promotions (4.5) shall be performed on
                      both operands.
                      ...
                      ...
                      ...
                      =============== =============== =============== =============== =======

                      Section 4.5 - Integral promotions - indicates that type char, signed
                      charm unsigned char, short int, or unsigned short int can be converted
                      to int if int can represent all values of the source type; otherwise,
                      the source rvalue can be converted to an rvalue of type unsigned int.

                      If you don't have a copy of the standard, the draft standard contains
                      the same language, and is available at:

                      ftp://ftp.research.att.com/dist/c++std/WP/CD2/body.pdf

                      The C language specification has similar language.

                      [color=blue]
                      > And you don't get a warning
                      > either.
                      >
                      > I guess if someone's choosing a particular data type, he's aware of its
                      > peculiarities. In fact, he might be using it *because* of its peculiarities.
                      > If he doesn't, he might simply use int anyway.[/color]

                      This is all true. However in C#, if the programmer wants to do
                      arithmetic on short types, then the programmer will have to use explicit
                      casts in many more cases than a programmer who wants to use ints. This
                      has nothing to do with completeness or correctness of the language;
                      however, it makes the language less convenient in this case.
                      [color=blue]
                      >
                      > The .NET runtime may perform its integral calculations internally by using
                      > int on PC platforms; however, I don't mind. I want to discuss about C# as a
                      > platform independant language and some lack therein.
                      >
                      >
                      > --------------------------------
                      > "mikeb" <mailbox.google @mailnull.com> schrieb im Newsbeitrag
                      > news:#WCOvnomDH A.1728@TK2MSFTN GP09.phx.gbl...
                      >[color=green]
                      >>Axel Dahmen wrote:
                      >>
                      >>[color=darkred]
                      >>>Well, Yves, I know these documents. Though they explain the effect[/color][/color]
                      >
                      > itself
                      >[color=green][color=darkred]
                      >>>and how to deal with C#'s incomplete data type implementation, they[/color][/color]
                      >
                      > don't
                      >[color=green][color=darkred]
                      >>>discuss the fact that the missing operators render C# incomplete.
                      >>>
                      >>>Here is my point of view: Mathematically and in Informatics science, a
                      >>>native scalar data type establishes a K vector space. - In C#, only the
                      >>>integer data type fulfills this requirement. All other integral C# data
                      >>>types are crippled so far.
                      >>>
                      >>>Regarding this aspect, C# is faulty. To get a truly comprehensive (a
                      >>>"valid") language, C# needs to add operators to all native scalar data
                      >>>types.
                      >>>
                      >>>I might be wrong, but in case I'm not, the arguments in this discussion
                      >>>should lead to implementing the missing operators into C#.
                      >>>[/color]
                      >>
                      >>ANSI C and C++ also always perform integer arithmetic using int (or
                      >>larger) types. It's just that in these languages you do not get an
                      >>error when assigning an int into a smaller integral type (you might get
                      >>a warning, though). The C# language designers decided that silently
                      >>truncating integral values was the source of enough bugs that they do
                      >>not allow implicit conversions if the conversion can potentially lose[/color]
                      >
                      > data.
                      >[/color]

                      --
                      mikeb

                      Comment

                      Working...