473,385 Members | 1,727 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,385 software developers and data experts.

a += b vs a = a + b

Compiles:

short a = 1;
a += 2;

Does not:

short a = 1;
a = a + 2;

(Cannot implicitly convert type 'int' to 'short').

Does this not strike anyone as little incongruent?
I assume the rules for type promotion after arithmetic are documented
somewhere, but it appears to be...

[sbyte|short|int] + [byte|short|int] = int // true for + - * /
[sbyte|short|int|long] + long = long
[sbyte|short|int|long|float] + float = float
[sbyte|short|int|long|float|double] + double = double

(unsigned types behave as per their signed relatives)

Even

a = a + (short)2;

does not compile; you need:

a = (short)(a + 2);

As a C++ programmer, the return of the old "C" cast syntax (which I
haven't used for over 5 years) is definitely the least appealing part
of C#, but there seems to be no way to avoid it.

Nov 17 '05 #1
13 1216
wi******@hotmail.com wrote:
As a C++ programmer, the return of the old "C" cast syntax (which I
haven't used for over 5 years) is definitely the least appealing part
of C#, but there seems to be no way to avoid it.


Although I dislike casts and welcome the belated arrival of generics, I
think it's only fair to point out that C# casts are not as dangerous as
the old C casts. C# casts explode in your face immediately, with an
InvalidCastException and a nice stack trace; whereas those old C casts
had the nasty habit of complying with stupid requests, only to blow up
behind your back some time later, possibly trashing memory along the
way. C# casts are _much_ easier to debug.

An aversion to casts is healthy, but we old-time C programmers fear
them more than we really need to these days :-)

-- Peter Gummer
Nov 17 '05 #2
Peter Gummer wrote:
wi******@hotmail.com wrote:
As a C++ programmer, the return of the old "C" cast syntax (which I
haven't used for over 5 years) is definitely the least appealing part
of C#, but there seems to be no way to avoid it.
Although I dislike casts and welcome the belated arrival of generics, I
think it's only fair to point out that C# casts are not as dangerous as
the old C casts. C# casts explode in your face immediately,


They do? To me "immediately" implies at compile time!
At least 4 or 5 times I tried using C# casts, the compiler should have
been able to detect I wasn't using them correctly (i.e., it was quite
obvious from a static analysis).
with an
InvalidCastException and a nice stack trace; whereas those old C casts
had the nasty habit of complying with stupid requests, only to blow up
behind your back some time later, possibly trashing memory along the
way. C# casts are _much_ easier to debug.


I pretty much always use checked C++ casts that do the same thing (at
least in Debug builds), in the very *rare* occasions I ever need to
explicitly cast. So I don't see that C# has an advantage here
particularly, except that the checking support is built in to the
language.

Nov 17 '05 #3
<wi******@hotmail.com> wrote:
Compiles:

short a = 1;
a += 2;

Does not:

short a = 1;
a = a + 2;

(Cannot implicitly convert type 'int' to 'short').

Does this not strike anyone as little incongruent?
I assume the rules for type promotion after arithmetic are documented
somewhere, but it appears to be...


Yes, they're documented in the language spec, section 14.7.4 (ECMA
numbering):

<quote>
int operator +(int x, int y);
uint operator +(uint x, uint y);
long operator +(long x, long y);
ulong operator +(ulong x, ulong y);
....
float operator +(float x, float y);
double operator +(double x, double y);
</quote>

The reason a+=2 works is due to 14.13.2:

<quote>
An operation of the form x op= y is processed by applying binary
operator overload resolution (§14.2.4) as if the operation was written
x op y. Then,

* If the return type of the selected operator is implicitly
convertible to the type of x, the operation is evaluated as x = x op y,
except that x is evaluated only once.
* Otherwise, if the selected operator is a predefined operator, if
the return type of the selected operator is explicitly convertible to
the type of x, and if y is implicitly convertible to the type of x,
then the operation is evaluated as x = (T)(x op y), where T is the type
of x, except that x is evaluated only once.
* Otherwise, the compound assignment is invalid, and a compile-time
error occurs.
</quote>

In this case, the return type of the operator (int) is explicitly
convertible to short, and 2 is implicitly convertible to short, so it's
evaluated as

a = (short) (a+2);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

Jon wrote:
<wi******@hotmail.com> wrote:
Compiles:

short a = 1;
a += 2;

Does not:

short a = 1;
a = a + 2;

(Cannot implicitly convert type 'int' to 'short').

Does this not strike anyone as little incongruent?
I assume the rules for type promotion after arithmetic are documented
somewhere, but it appears to be...


Yes, they're documented in the language spec, section 14.7.4 (ECMA
numbering):

<quote>
int operator +(int x, int y);
uint operator +(uint x, uint y);
long operator +(long x, long y);
ulong operator +(ulong x, ulong y);
...
float operator +(float x, float y);
double operator +(double x, double y);
</quote>

Ok, so what's the justification? Why the expression (short_var + 2)
more likely to produce something that won't fit into a short than the
expression (short_var +=2)?

But more importantly, why is this "unsafe":

short var = get_a_short();
var = (var - 1) / 2;

But this, fine:

int var = get_an_int();
var = (var * 0x10000) + 0x1000000;

Surely the latter is far more likely to produce a type overflow?

Nov 17 '05 #5
wi******@hotmail.com wrote:
I pretty much always use checked C++ casts that do the same thing (at
least in Debug builds), in the very rare occasions I ever need to
explicitly cast. So I don't see that C# has an advantage here
particularly, except that the checking support is built in to the
language.


The comparison I was making was with C, not C++. C# casts are a big
improvement over C casts.

As to C++, I suspect you're right. I accept your complaint that C#
casts are a backwards step from alternatives available in C++. But, as
I've pointed out, returning to the old C cast _syntax_ does not imply a
return to the old C cast _semantics_: thank goodness!

-- Peter Gummer
Nov 17 '05 #6
wi******@hotmail.com wrote:

<snip>
Ok, so what's the justification? Why the expression (short_var + 2)
more likely to produce something that won't fit into a short than the
expression (short_var +=2)?


I don't think it's actually a safety issue. I think it's a performance
issue. If the type of the expression (shortVar+2) were "short" rather
than "int", the expression would have to be evaluated in 16 bits, which
is slower on many processors than using 32 bits. In the case of doing
+=, the language itself knows (with no extra information such as where
you're using the operator - it only needs to look at the operands of
the operator itself) that the result *must* be converted to 16 bits.

That's only a suspicion, admittedly. I can ask MS to enter the
discussion at this point if you'd like. (I can't guarantee they will,
but I can ask...)

Jon

Nov 17 '05 #7

Jon Skeet [C# MVP] wrote:
wi******@hotmail.com wrote:

<snip>
Ok, so what's the justification? Why the expression (short_var + 2)
more likely to produce something that won't fit into a short than the
expression (short_var +=2)?
I don't think it's actually a safety issue. I think it's a performance
issue. If the type of the expression (shortVar+2) were "short" rather
than "int", the expression would have to be evaluated in 16 bits, which
is slower on many processors than using 32 bits. In the case of doing
+=, the language itself knows (with no extra information such as where
you're using the operator - it only needs to look at the operands of
the operator itself) that the result *must* be converted to 16 bits.


Ok, but that's a compiler optimization issue, nothing to do with the
language.
I would expect that if I were compiling specifically for a machine
where 16 bit arithmetic was faster, it would use that (even for 32 bit
and 64 bit values values).

That's only a suspicion, admittedly. I can ask MS to enter the
discussion at this point if you'd like. (I can't guarantee they will,
but I can ask...)

Well I'm definitely curious. In the whole 3 or 4 days now that I've
written any C# code, I never had to sprinkle my code with so many ugly
(and logically unnecessary) casts!

Nov 17 '05 #8
wi******@hotmail.com wrote:
Well I'm definitely curious. In the whole 3 or 4 days now that I've
written any C# code, I never had to sprinkle my code with so many ugly
(and logically unnecessary) casts!


But why are you using 'short' in the first place? The question you've
raised is interesting, but I always use 'int' for whole numbers unless
I have a very specific reason to do otherwise. Hence, I've never even
encountered this issue ... and so I thank you for drawing it to my
attention ;-)

- Peter Gummer
Nov 17 '05 #9

Peter Gummer wrote:
wi******@hotmail.com wrote:
Well I'm definitely curious. In the whole 3 or 4 days now that I've
written any C# code, I never had to sprinkle my code with so many ugly
(and logically unnecessary) casts!


But why are you using 'short' in the first place? The question you've
raised is interesting, but I always use 'int' for whole numbers unless
I have a very specific reason to do otherwise. Hence, I've never even
encountered this issue ... and so I thank you for drawing it to my
attention ;-)

Why does "short" exist in the language if it's not meant for regular
use?
The values I'm dealing with most definitely fall well within the range
of a short integer.
I also happen to need to store a lot of them, and the memory savings
are definitely worthwhile (well, they were in the original C++ code I
was converting from. Not sure if the same would be true under C#).

Nov 17 '05 #10
<wi******@hotmail.com> wrote:
Ok, so what's the justification? Why the expression (short_var + 2)
more likely to produce something that won't fit into a short than the
expression (short_var +=2)?
I don't think it's actually a safety issue. I think it's a performance
issue. If the type of the expression (shortVar+2) were "short" rather
than "int", the expression would have to be evaluated in 16 bits, which
is slower on many processors than using 32 bits. In the case of doing
+=, the language itself knows (with no extra information such as where
you're using the operator - it only needs to look at the operands of
the operator itself) that the result *must* be converted to 16 bits.


Ok, but that's a compiler optimization issue, nothing to do with the
language.


No, it's not. There's a semantic difference at stake here - if
shortVar+2 should evaluate to a short, that is very different in
observed behaviour from "wrapping" (or throwing an exception, depending
on whether the context is checked or unchecked) and simply overflowing
into the 17th bit.
I would expect that if I were compiling specifically for a machine
where 16 bit arithmetic was faster, it would use that (even for 32 bit
and 64 bit values values).


It can't - not when shortVar+2 could cause overflow into 32 bits.
That's only a suspicion, admittedly. I can ask MS to enter the
discussion at this point if you'd like. (I can't guarantee they will,
but I can ask...)

Well I'm definitely curious. In the whole 3 or 4 days now that I've
written any C# code, I never had to sprinkle my code with so many ugly
(and logically unnecessary) casts!


I'll ask MS tomorrow.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #11

Jon wrote:
<wi******@hotmail.com> wrote:
> Ok, so what's the justification? Why the expression (short_var + 2)
> more likely to produce something that won't fit into a short than the
> expression (short_var +=2)?

I don't think it's actually a safety issue. I think it's a performance
issue. If the type of the expression (shortVar+2) were "short" rather
than "int", the expression would have to be evaluated in 16 bits, which
is slower on many processors than using 32 bits. In the case of doing
+=, the language itself knows (with no extra information such as where
you're using the operator - it only needs to look at the operands of
the operator itself) that the result *must* be converted to 16 bits.


Ok, but that's a compiler optimization issue, nothing to do with the
language.


No, it's not. There's a semantic difference at stake here - if
shortVar+2 should evaluate to a short, that is very different in
observed behaviour from "wrapping" (or throwing an exception, depending
on whether the context is checked or unchecked) and simply overflowing
into the 17th bit.
I would expect that if I were compiling specifically for a machine
where 16 bit arithmetic was faster, it would use that (even for 32 bit
and 64 bit values values).


It can't - not when shortVar+2 could cause overflow into 32 bits.


All I'm saying is that

shortVar = shortVar + 2;

should be logically equivalent to (and just as safe/acceptable as)

shortVar = (short)(shortVar + 2);

After all, even in C++,

int iVar = shortVar + 2;

and

int iVar = shortVar + short(2);

will produce the correct result (no overflowing) even if shortVar is
SHRT_MAX (and even if '2' is stored in a 'short' variable).

Having said this, I'm not sure if this is guaranteed to be true on all
platforms. But at least with VC 7.1 and g++ on a Pentium chip, it's
true.

Nov 17 '05 #12
wi******@hotmail.com wrote:

After all, even in C++,

int iVar = shortVar + 2;

and

int iVar = shortVar + short(2);

will produce the correct result (no overflowing) even if shortVar is
SHRT_MAX (and even if '2' is stored in a 'short' variable).

Having written this, I think I can now see that the type promotion
rules for arithmetical expressions in C# are exactly the same as those
in C/C++.
Given this, and the fact that it's obviously a key "feature" of C# that
types cannot be automatically demoted (you have to explicitly cast from
a 'larger' type to a 'smaller' type), then the difference between
shortVar += 2 and shortVar + 2 is understandable.
The only solution I can see is that type promotion via an arithmetical
expression should not have the same status as a declared type.
That is, while it's true that (shortVar + 2) is type int, it's never
been "declared" int (via a variable or a function prototype), so can
still be silently downgraded to...well...in this case, obviously a
short, but this would really mean that the type of an arithmetical
expression
has both a "value-size" type (i.e. int), and a conversion type (in this
case short).
I wouldn't have an issue with, for instance,

byte b = shortVar + 2;

not compiling.

I guess I'm just going to have to avoid anything else other than ints
if I want my code to be readable. I can't see myself using C# for
anything where using shorts would be really that important anyway.

Nov 17 '05 #13
wi******@hotmail.com wrote:
Why does "short" exist in the language if it's not meant for regular
use?


By all means use it. It sounds like it's appropriate in this case,
since I assume you'd get the same worthwhile memory saving in C# that
you saw in C++.

I didn't say "it's not meant for regular use". I said use short only if
you have a very specific reason; it sounds like you have such a reason.
This was a policy I adopted long ago in standard C, and have seen good
sense in keeping to when programming in a variety of languages: Delphi,
C# and Eiffel. It just avoids issues that seem to crop up in most
languages.

-- Peter Gummer
Nov 18 '05 #14

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

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...

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.