int=int*int

  • Thread starter Thread starter JohnZing
  • Start date Start date
J

JohnZing

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
 
JohnZing said:
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.
 
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.
 
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.
 
Makes sense to me, but, i think the option should be set true as default.
thank you all.

....
 
JohnZing said:
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.
 
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 said:
JohnZing said:
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
 
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 said:
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 said:
JohnZing said:
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
 
Yes I know that. The point here is that I forget to do it.

Daniel Jin said:
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 said:
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 said:
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
 
Back
Top