473,803 Members | 3,306 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2251

Take a look at these:

http://msdn.microsoft.com/library/de...vclrfshort.asp
http://msdn.microsoft.com/library/de...conversion.asp
"Axel Dahmen" <No****@NoOneKn ows.de> wrote in message
news:ex******** ******@tk2msftn gp13.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******** ************@ne ws20.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/
vclrfimplicitnu mericconversion .asp

"Axel Dahmen" <No****@NoOneKn ows.de> wrote in message
news:ex******** ******@tk2msftn gp13.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****@NoOneKn ows.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
"completene ss" for "usefulness ".

--
Jon Skeet - <sk***@pobox.co m>
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******** ************@ne ws20.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/
vclrfimplicitnu mericconversion .asp

"Axel Dahmen" <No****@NoOneKn ows.de> wrote in message
news:ex****** ********@tk2msf tngp13.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****@NoOneKn ows.de> wrote in message
news:ex******** ******@tk2msftn gp13.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.co m> schrieb im Newsbeitrag
news:MP******** *************** *@msnews.micros oft.com...
Axel Dahmen <No****@NoOneKn ows.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
"completene ss" 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******** ******@TK2MSFTN GP09.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****@NoOneKn ows.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.co m>
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****@NoOneKn ows.de> wrote in message
news:OA******** ******@tk2msftn gp13.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******** ************@ne ws20.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/ vclrfimplicitnu mericconversion .asp


"Axel Dahmen" <No****@NoOneKn ows.de> wrote in message
news:ex******** ******@tk2msftn gp13.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

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

Similar topics

14
2534
by: greg | last post by:
Discussion is invited on the following proto-PEP. ------------------------------------------------------------- PEP ??? - Overloadable Boolean Operators ======================================== SUMMARY This PEP proposes an extension to permit objects to define their own
10
1989
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 my doubt is , if u have something like p->a ....where p(say) is a pointer of a user defined type, then u can overload this because p is determined at runtime ???is this right ??? also if "a" is an object of the some user defined class,
11
2606
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 by myself. I know there are already tons of vector and matrix implementations, but I wanted to have one taylored for my needs and without debugging someones else code. Also is's become somewhat personal meanwhile ;-).
4
8024
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 compiler error: error CS0029: Cannot implicitly convert type 'int' to 'ushort'
6
1284
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 to say here? http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDrawingPointClassop_AdditionTopic.asp Can't I use: pt1 = pt2 + pt3
17
2447
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 specific example was to define an "outer product" operator for matrices. (There was even a PEP, number 211, about this.) I gave it some thought, and Googled for previous discussions about this, and came up with this suggestion: User-defined...
4
11046
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 short) 1. now how can i get back value 38,143 from short variable value 27,393 ? 2. how can i check whether the conversion of value happned when i
18
2645
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
1720
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 wide) and add only the unique keys to a List<T>, Dictionary, Array... whatever is fastest because I'll be doing trillions of compares. The key is: ushort myBigKey = new ushort; I want to treat the entire array (all 100 instances) as one long...
0
9700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10546
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10068
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9121
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7603
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.