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

int=int*int

Hi
i'm new to csharp

why

int x=999999999;
int y=1;
y=x*x;
Response.Write(y);

does not raise an error and the new value (y) is wrong?

thank you
Nov 16 '05 #1
9 1689
What answer do you expect ?

"JohnZing" <jo**********@S.P.A.M.yahoo.com> wrote in message
news:Xn**********************************@207.46.2 48.16...
Hi
i'm new to csharp

why

int x=999999999;
int y=1;
y=x*x;
Response.Write(y);

does not raise an error and the new value (y) is wrong?

thank you

Nov 16 '05 #2
JohnZing <jo**********@S.P.A.M.yahoo.com> wrote:
why

int x=999999999;
int y=1;
y=x*x;
Response.Write(y);

does not raise an error and the new value (y) is wrong?


It's not. By default, arithmetic is unchecked in C#, so operations are
essentially mod (size of type), in a way that maxvalue+1==minvalue.
That's a very loose way of describing it - I hope you see what I mean.

If you want, you can make the arithmetic checked though.

See section 14.5.12 of the ECMA spec for more information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Because the "Arithmetic Overflow checking" is turned off in your project
settings (default).
Or when building from the command line you have to set the /checked flag.

Willy.

"JohnZing" <jo**********@S.P.A.M.yahoo.com> wrote in message
news:Xn**********************************@207.46.2 48.16...
Hi
i'm new to csharp

why

int x=999999999;
int y=1;
y=x*x;
Response.Write(y);

does not raise an error and the new value (y) is wrong?

thank you

Nov 16 '05 #4
By default the runtime is not checking for arithmetic overflow. To turn
overflow checking on go to the project properties and select the Build page
under Configuration Properties and set Check for Arithmetic
Overflow/Underflow to true.

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada
http://msmvps.com/windsor/
"JohnZing" <jo**********@S.P.A.M.yahoo.com> wrote in message
news:Xn**********************************@207.46.2 48.16...
Hi
i'm new to csharp

why

int x=999999999;
int y=1;
y=x*x;
Response.Write(y);

does not raise an error and the new value (y) is wrong?

thank you

Nov 16 '05 #5
Makes sense to me, but, i think the option should be set true as default.
thank you all.

Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
....
It's not. By default, arithmetic is unchecked in C#, so operations are
essentially mod (size of type), in a way that maxvalue+1==minvalue.
That's a very loose way of describing it - I hope you see what I mean.

If you want, you can make the arithmetic checked though.

See section 14.5.12 of the ECMA spec for more information.


Nov 16 '05 #6
JohnZing wrote:
Makes sense to me, but, i think the option should be set true as
default. thank you all.


Checking an arithmatic statement for overflow would at least double the
amount of time it took to execute. Having that option on by default would
put a tremendous speed penelty on every program, with very little gain, as
most programmers know how to avoid an overflow.
--
Truth,
James Curran [erstwhile-MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
Nov 16 '05 #7
Hey James,
Having that option on by default would put a tremendous speed penelty on
every program, with very little gain, as most programmers know how to
avoid an overflow.
Folks used to say the same about array bounds checking and look where that
got us !

I'm an experienced programmer and I've been caught out by this one so many
times its not funny any more. I reckon it should be the debug project
default at the very least.

Cheers

Doug Forster

"James Curran" <Ja*********@mvps.org> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl... JohnZing wrote:
Makes sense to me, but, i think the option should be set true as
default. thank you all.


Checking an arithmatic statement for overflow would at least double
the --
Truth,
James Curran [erstwhile-MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

Nov 16 '05 #8
if you want overflow check on all arithmatic operations, then just turn it
on. it's only a csc command line switch, not all that difficult to do.

"Doug Forster" wrote:
Hey James,
Having that option on by default would put a tremendous speed penelty on
every program, with very little gain, as most programmers know how to
avoid an overflow.


Folks used to say the same about array bounds checking and look where that
got us !

I'm an experienced programmer and I've been caught out by this one so many
times its not funny any more. I reckon it should be the debug project
default at the very least.

Cheers

Doug Forster

"James Curran" <Ja*********@mvps.org> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
JohnZing wrote:
Makes sense to me, but, i think the option should be set true as
default. thank you all.


Checking an arithmatic statement for overflow would at least double
the --
Truth,
James Curran [erstwhile-MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com


Nov 16 '05 #9
Yes I know that. The point here is that I forget to do it.

"Daniel Jin" <Da*******@discussions.microsoft.com> wrote in message
news:FA**********************************@microsof t.com...
if you want overflow check on all arithmatic operations, then just turn it
on. it's only a csc command line switch, not all that difficult to do.

"Doug Forster" wrote:
Hey James,
> Having that option on by default would put a tremendous speed penelty
> on
> every program, with very little gain, as most programmers know how to
> avoid an overflow.


Folks used to say the same about array bounds checking and look where
that
got us !

I'm an experienced programmer and I've been caught out by this one so
many
times its not funny any more. I reckon it should be the debug project
default at the very least.

Cheers

Doug Forster

"James Curran" <Ja*********@mvps.org> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
> JohnZing wrote:
>> Makes sense to me, but, i think the option should be set true as
>> default. thank you all.
>
> Checking an arithmatic statement for overflow would at least double
> the --
> Truth,
> James Curran [erstwhile-MVP]
> Home: www.noveltheory.com Work: www.njtheater.com
> Blog: www.honestillusion.com Day Job: www.partsearch.com
>
>


Nov 16 '05 #10

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

Similar topics

2
by: CoolPint | last post by:
Can anyone clearly explain the difference between constant reference to pointers and reference to constant pointers? What is const int * & ? Is it a constant reference to a pointer to an...
1
by: akickdoe22 | last post by:
Please help me finish this program. i have completed the addition and the subtraction parts, but i am stuck on the multiplication and division. any suggestions, hints, code, anyhting. it's not a...
5
by: Kemal Ozan | last post by:
Hi, I am studying K&R book. On the multidimensional Arrays chapter they say "int (*daytab) is a pointer to an array of 13 integers. The parenthesis are necessary since brackets have higher...
7
by: Andrej Prsa | last post by:
Hello, everyone! When a const int * argument is passed to a function, i.e. int f (const int *var) { printf ("%d\n", *var); } int main ()
7
by: Jeff K | last post by:
Can you pass an int array by reference to a function and modify selective elements? Here is my code: #include <stdio.h> #define COLUMNSIZE 30 #define ASIZE 5...
1
by: Felix | last post by:
I am a bit confused as to why conversion from float/double to int is handled in c# in the way that it is. It differs from my implementation of C and C++ in a suprising way (to me). Here is a...
4
by: chrisstankevitz | last post by:
This code does not compile on gcc 3.4.4. Should it? Thanks for your help, Chris //================ #include <set> int main()
14
by: yang__lee | last post by:
Hi, You all know typedef typedef struct g { int a; int b; } google;
16
by: Julia | last post by:
Hi, there, In C programming, for pointer, I saw two programming styles: one is connecting '*' with variable, like, 'int *i'; the other is connecting '*' with data type, like, 'int* i' I...
8
by: beagle197 | last post by:
Folks, Attempting to q-sort an array of int pairs, e.g. {{0,1}, {0, 0}, ...} allocated using malloc/calloc, but the arguments I'm passing to qsort are producing the incorrect results (see...
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...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.