473,322 Members | 1,398 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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


Nov 15 '05 #1
10 2219

Take a look at these:

http://msdn.microsoft.com/library/de...vclrfshort.asp
http://msdn.microsoft.com/library/de...conversion.asp
"Axel Dahmen" <No****@NoOneKnows.de> wrote in message
news:ex**************@tk2msftngp13.phx.gbl...
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

Nov 15 '05 #2
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" <de****************@sympatico.ca> schrieb im Newsbeitrag
news:ez********************@news20.bellglobal.com. ..

Take a look at these:

http://msdn.microsoft.com/library/de...us/csref/html/
vclrfshort.asp http://msdn.microsoft.com/library/de...us/csref/html/
vclrfimplicitnumericconversion.asp

"Axel Dahmen" <No****@NoOneKnows.de> wrote in message
news:ex**************@tk2msftngp13.phx.gbl...
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




Nov 15 '05 #3
Axel Dahmen <No****@NoOneKnows.de> wrote:
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#.


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
"completeness" for "usefulness".

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
Axel Dahmen wrote:
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#.

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.


-----------------------------------
"Yves Tourchot" <de****************@sympatico.ca> schrieb im Newsbeitrag
news:ez********************@news20.bellglobal.com. ..
Take a look at these:


http://msdn.microsoft.com/library/de...us/csref/html/
vclrfshort.asp

http://msdn.microsoft.com/library/de...us/csref/html/
vclrfimplicitnumericconversion.asp

"Axel Dahmen" <No****@NoOneKnows.de> wrote in message
news:ex**************@tk2msftngp13.phx.gbl...
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




--
mikeb

Nov 15 '05 #5
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" <No****@NoOneKnows.de> wrote in message
news:ex**************@tk2msftngp13.phx.gbl...
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

Nov 15 '05 #6
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]" <sk***@pobox.com> schrieb im Newsbeitrag
news:MP************************@msnews.microsoft.c om...
Axel Dahmen <No****@NoOneKnows.de> wrote:
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#.


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
"completeness" for "usefulness".




Nov 15 '05 #7
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" <ma************@mailnull.com> schrieb im Newsbeitrag
news:#W**************@TK2MSFTNGP09.phx.gbl...
Axel Dahmen wrote:
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#.


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.





Nov 15 '05 #8
Axel Dahmen <No****@NoOneKnows.de> wrote:
adding the missing operators has practical impact, of course. First of all
it would make an end to the clumsy spelling.
Do you mean casting?
Furthermore, ask yourself: Why
are the smaller integral types implemented into a language anyway?
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.
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.


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #9
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" <No****@NoOneKnows.de> wrote in message
news:OA**************@tk2msftngp13.phx.gbl...
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" <de****************@sympatico.ca> schrieb im Newsbeitrag
news:ez********************@news20.bellglobal.com. ..

Take a look at these:

http://msdn.microsoft.com/library/de...us/csref/html/ vclrfshort.asp

http://msdn.microsoft.com/library/de...us/csref/html/ vclrfimplicitnumericconversion.asp


"Axel Dahmen" <No****@NoOneKnows.de> wrote in message
news:ex**************@tk2msftngp13.phx.gbl...
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



Nov 15 '05 #10
Axel Dahmen wrote:
No, they both don't perform integer arithmetic using int (if they do, it's a
a compiler peculiarity, not a language feature).
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.

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.
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.

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" <ma************@mailnull.com> schrieb im Newsbeitrag
news:#W**************@TK2MSFTNGP09.phx.gbl...
Axel Dahmen wrote:

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#.


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.


--
mikeb

Nov 15 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: greg | last post by:
Discussion is invited on the following proto-PEP. ------------------------------------------------------------- PEP ??? - Overloadable Boolean Operators...
10
by: maadhuu | last post by:
hi i wasnt to know the answer for the following. now ,u can overload all the operators which are basically determined at runtime (coz' of whch operators like sizeof())cannot be overloaded. now...
11
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them...
4
by: JuLiE Dxer | last post by:
Perhaps, I skipped over learning this particular tidbit. I ran across an odd error while trying to add to ushort variables together and assigning it to a 3rd ushort variable. I get this...
6
by: Larry Serflaten | last post by:
I am trying to add 2 points together, and I am not succeeding. It appears the docs say I need a point and a size, but even that fails in the copy of VS 2003 I have. Exactly what are they trying...
17
by: Steve R. Hastings | last post by:
I have been studying Python recently, and I read a comment on one web page that said something like "the people using Python for heavy math really wish they could define their own operators". The...
4
by: rakesh.Mysore | last post by:
HI I have a problem in casting short to ushort in my project. i have a ushort value of 38,143 now it is converted into short value of 27,393 and stored in variable.(when i casted ushort to...
18
by: Zach | last post by:
Can someone list the various macro operators and what they mean. Came across a function macro: #define max(a, b) ((a)>(b)?(a):(b)) What does "?" amd ":" mean in this statement? Zach
8
by: Mark Main | last post by:
I just bought Visual Studio 2008, I'm new to C# and trying to learn it. I'm stuck and would appreciate some help. I need to make the fastest code I can to work with a large key (it's 200 bytes...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.