Skip to main content
GameDev.net gamedev.net
🔒 Locked

.NET Book Zero p.53

Started by Vod Jul 24, 2007 at 9:02 PM 3 replies 1.9k views
Original Post
Vod
Vod

int i = 50000;
Console.WriteLine(i * i);

Why is the output -1794967296?
Strewth
Strewth
The minimum and maximum value of an int is –2,147,483,648...2,147,483,647

You are trying to store 2,500,000,000 and because you haven't used the checked operator it is overflowing.
Blake's 7 RPG - a new project
daviangel
daviangel
Yup either use a larger data type that can hold your values or use the checked csc compiler flag.
use csc /? at the command line for a list of all the compiler flags.
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
DarkTimes
DarkTimes
You can also use the checked operator if you want to make sure it doesn't overflow.

int i = 50000;try{    Console.WriteLine(checked(i * i));}catch (OverflowException){    Console.WriteLine("Overflow Exception");    // TODO: Write clever exception     // handling code.}


As you can see this code throws a System.OverflowException when the output overflows.

[Edited by - DarkTimes on July 25, 2007 4:18:19 AM]
Vod
Vod
ah ok
thanks :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.