473,831 Members | 2,320 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
10 2252
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******** ******@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.


--
mikeb

Nov 15 '05 #11

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

Similar topics

14
2537
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
1990
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
2609
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
8026
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
2450
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
11075
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
2648
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
1722
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
9642
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10777
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...
1
10534
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10207
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...
1
7748
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
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5619
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5780
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3076
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.