I'm trying to investigate the maximum size of different variable types.
I'm using INT as my starting variable for exploration. I know that the
maximum number that the int variable can take is: 65,535. But i'm
trying to write a program to test this, assuming I didn't know this
number in advance. I came up with the following but have two questions.
Maybe someone can help?
using System;
using System.Collecti ons.Generic;
using System.Text;
namespace ConsoleApplicat ion1
{
class Program
{
static void Main(string[] args)
{
int j = 0;
double i = 0;
for (i = 0; i < 999999999999999 9999; i++)
{
j = (int)i;
Console.WriteLi ne(j.ToString() );
///on error console.write(t here was an error... this
was the error code)
}
}
}
}
q.1.
I want to have an idea of what is going on in my program. As you can
see, in my for loop I am writing the value of j to the screen on a new
line every time it is incremented. This is too slow. It seems to be
taking forever to scroll through the digits. My question is, is there a
way that I can display the value of j every, say, 10 thousand
increments?
q.2.
is there a way I can time how long the loop takes to complete, and at
the end of the loop display this time?
q.3
I'd like to output an error message to the screen when the variable
crashes because it's too small to hold the number being allocated to
it. How do i do this?
Thankyou,
Gary. 29 5082
I'm trying to investigate the maximum size of different variable
types. I'm using INT as my starting variable for exploration. I know
that the maximum number that the int variable can take is: 65,535.
Actually, that's the maximum size of a ushort. int has a maximum upper limit
of 2,147,483,647. In fact, you can see the upper limit by checking the MaxValue
field of int.
But i'm trying to write a program to test this, assuming I didn't know
this number in advance. I came up with the following but have two
questions. Maybe someone can help?
using System;
using System.Collecti ons.Generic;
using System.Text;
namespace ConsoleApplicat ion1
{
class Program
{
static void Main(string[] args)
{
int j = 0;
double i = 0;
for (i = 0; i < 999999999999999 9999; i++)
{
j = (int)i;
Console.WriteLi ne(j.ToString() );
///on error console.write(t here was an error... this
was the error code)
}
}
}
}
q.1.
I want to have an idea of what is going on in my program. As you can
see, in my for loop I am writing the value of j to the screen on a new
line every time it is incremented. This is too slow. It seems to be
taking forever to scroll through the digits. My question is, is there
a
way that I can display the value of j every, say, 10 thousand
increments?
sure. i += 10000
q.2.
is there a way I can time how long the loop takes to complete, and at
the end of the loop display this time?
If you're in .NET 2.0, try the System.Diagnost ics.Stopwatch class.
q.3
I'd like to output an error message to the screen when the variable
crashes because it's too small to hold the number being allocated to
it. How do i do this?
Well, you shouldn't actually need to do this. It'll crash on its own and
display an error message to the console. However, you could wrap your code
in a try/catch block like this:
static void Main(string[] args)
{
try
{
// Your code here...
}
catch (Exception ex)
{
Console.WriteLi ne(ex.Message);
}
}
Best Regards,
Dustin Campbell
Developer Express Inc.
Thankyou very much Dustin your answers have answerd 90% of my questions
and have really helped me alot.
Although changing the for interation from 1, to a higher number works
for this test it doesn't really answer my for loop question, which was
probabally because I didn't put my question right in the first
instance! I was intrigued about the question when it came to me in this
example and still dont really know the answer.
My First Question is: -
With a for loop like so: -
for(i=0; i<9999999999; i++)
{
}
how can I do an action every nth interations of the loop? so for
instance everytime the loop has executed 10,000 times do something?
Also another question has come to me now as I have worked through your
answers.
My code is now displaying the value of of the integer variable to
screen and I'm not having to wait as long as i am incrementing it's
value by 500,000 every iteration. However. When the value gets to:
2145,000,000 all future increments don't generate an error.
Instead each future increment is displayed as: -2147483648.
I'm just wondering why an error isn't generated. And why the value
becomes negative, with a numerical value one greater than the actual
numerical value that the variable is permitted to hold?
Thanks!
Gary-
Dustin Campbell wrote:
I'm trying to investigate the maximum size of different variable
types. I'm using INT as my starting variable for exploration. I know
that the maximum number that the int variable can take is: 65,535.
Actually, that's the maximum size of a ushort. int has a maximum upper limit
of 2,147,483,647. In fact, you can see the upper limit by checking the MaxValue
field of int.
But i'm trying to write a program to test this, assuming I didn't know
this number in advance. I came up with the following but have two
questions. Maybe someone can help?
using System;
using System.Collecti ons.Generic;
using System.Text;
namespace ConsoleApplicat ion1
{
class Program
{
static void Main(string[] args)
{
int j = 0;
double i = 0;
for (i = 0; i < 999999999999999 9999; i++)
{
j = (int)i;
Console.WriteLi ne(j.ToString() );
///on error console.write(t here was an error... this
was the error code)
}
}
}
}
q.1.
I want to have an idea of what is going on in my program. As you can
see, in my for loop I am writing the value of j to the screen on a new
line every time it is incremented. This is too slow. It seems to be
taking forever to scroll through the digits. My question is, is there
a
way that I can display the value of j every, say, 10 thousand
increments?
sure. i += 10000
q.2.
is there a way I can time how long the loop takes to complete, and at
the end of the loop display this time?
If you're in .NET 2.0, try the System.Diagnost ics.Stopwatch class.
q.3
I'd like to output an error message to the screen when the variable
crashes because it's too small to hold the number being allocated to
it. How do i do this?
Well, you shouldn't actually need to do this. It'll crash on its own and
display an error message to the console. However, you could wrap your code
in a try/catch block like this:
static void Main(string[] args)
{
try
{
// Your code here...
}
catch (Exception ex)
{
Console.WriteLi ne(ex.Message);
}
}
Best Regards,
Dustin Campbell
Developer Express Inc.
how about : -
using System;
using System.Collecti ons.Generic;
using System.Text;
namespace ConsoleApplicat ion1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLi ne("Int32 Max= "+Int32.MaxValu e.ToString());
}
}
}
I know it's a bit tongue and cheek, but surely this is all you trying to
achieve?
The MaxValue and MinValue constants are available for all sorts of
different variable types.
MC ga********@mywa y.com wrote:
I'm trying to investigate the maximum size of different variable types.
I'm using INT as my starting variable for exploration. I know that the
maximum number that the int variable can take is: 65,535. But i'm
trying to write a program to test this, assuming I didn't know this
number in advance. I came up with the following but have two questions.
Maybe someone can help?
using System;
using System.Collecti ons.Generic;
using System.Text;
namespace ConsoleApplicat ion1
{
class Program
{
static void Main(string[] args)
{
int j = 0;
double i = 0;
for (i = 0; i < 999999999999999 9999; i++)
{
j = (int)i;
Console.WriteLi ne(j.ToString() );
///on error console.write(t here was an error... this
was the error code)
}
}
}
}
q.1.
I want to have an idea of what is going on in my program. As you can
see, in my for loop I am writing the value of j to the screen on a new
line every time it is incremented. This is too slow. It seems to be
taking forever to scroll through the digits. My question is, is there a
way that I can display the value of j every, say, 10 thousand
increments?
q.2.
is there a way I can time how long the loop takes to complete, and at
the end of the loop display this time?
q.3
I'd like to output an error message to the screen when the variable
crashes because it's too small to hold the number being allocated to
it. How do i do this?
Thankyou,
Gary. ga********@mywa y.com wrote:
I'm trying to investigate the maximum size of different variable types.
I'm using INT as my starting variable for exploration. I know that the
maximum number that the int variable can take is: 65,535. But i'm
trying to write a program to test this, assuming I didn't know this
number in advance. I came up with the following but have two questions.
Maybe someone can help?
using System;
using System.Collecti ons.Generic;
using System.Text;
namespace ConsoleApplicat ion1
{
class Program
{
static void Main(string[] args)
{
int j = 0;
double i = 0;
for (i = 0; i < 999999999999999 9999; i++)
{
j = (int)i;
Console.WriteLi ne(j.ToString() );
///on error console.write(t here was an error... this
was the error code)
}
}
}
}
To start with, System.Int16, Int32 and Int64 all have a static MaxValue
property. Accessing that is much faster than looping until an exception is
thrown.
q.1.
I want to have an idea of what is going on in my program. As you can
see, in my for loop I am writing the value of j to the screen on a new
line every time it is incremented. This is too slow. It seems to be
taking forever to scroll through the digits. My question is, is there a
way that I can display the value of j every, say, 10 thousand
increments?
if (j%10000 == 0)Console.Write Line(j.ToString ());
q.2.
is there a way I can time how long the loop takes to complete, and at
the end of the loop display this time?
As Dustin responded, System.Diagnost ics.Stopwatch should help. If not .NET
2.0, you could create a DateTime object at the start of the loop, another at
the end, set both values to the static DateTime.Now and subract end from
start once the loop is finished. Ex:
DateTime start = DateTime.Now;
for ()
{
....
}
Console.WriteLi ne (DateTime.Now - start);
q.3
I'd like to output an error message to the screen when the variable
crashes because it's too small to hold the number being allocated to
it. How do i do this?
The problem here is it won't crash. if you try to cast a value that is too
large for an Int32 into an Int32, it simply loops. Try the following in
code:
double d = double.MaxValue
int x = (int)d;
This will not cause an exception and will instead set the value of x
to -2147483648. So you would need to check for a negative number and then
behave accordingly. Ex:
if (j < 0)
{
Console.WriteLi ne ("Max value is " + (i - 1));
break;
}
But again, System.Int32.Ma xValue is the better way to get this value.
--
Tom Porterfield
Thankyou very much Dustin your answers have answerd 90% of my
questions and have really helped me alot.
Although changing the for interation from 1, to a higher number works
for this test it doesn't really answer my for loop question, which was
probabally because I didn't put my question right in the first
instance! I was intrigued about the question when it came to me in
this example and still dont really know the answer.
My First Question is: -
With a for loop like so: -
for(i=0; i<9999999999; i++)
{
}
how can I do an action every nth interations of the loop? so for
instance everytime the loop has executed 10,000 times do something?
Use modular arithmetic like this:
for(i=0; i<9999999999; i++)
{
if (i % 10000 == 0)
// do something...
}
Also another question has come to me now as I have worked through your
answers.
My code is now displaying the value of of the integer variable to
screen and I'm not having to wait as long as i am incrementing it's
value by 500,000 every iteration. However. When the value gets to:
2145,000,000 all future increments don't generate an error.
Instead each future increment is displayed as: -2147483648.
I'm just wondering why an error isn't generated. And why the value
becomes negative, with a numerical value one greater than the actual
numerical value that the variable is permitted to hold?
See Tom's excellent post on this thread.
Best Regards,
Dustin Campbell
Developer Express Inc.
Thankyou these are exactly the answers I was looking for. I have two
final questions on the subject maybe someone would be kind enough to
answer them for me.
1.
Why isn't an error generated? Is this a floor with the .net error
checking?
2.
When I experiment with a uint instead of an int, and increment it by
500,000 at a time, i notice the value outputted to the console doesn't
steadily increase, but increases, then decreases, then increases etc...
why is this? what is going on?
Thanks,
Gary-
how can I do an action every nth interations of the loop?
for(...) {
if(i % 10000==0) {
DoSomethingInte resting();
}
}
// perhaps DoSomethingInte resting() here if you always
// want it to fire at the end even if partly through a block
Marc
Thankyou these are exactly the answers I was looking for. I have two
final questions on the subject maybe someone would be kind enough to
answer them for me.
1.
Why isn't an error generated? Is this a floor with the .net error
checking?
Try wrapping the expression with the "checked" keyword. This will cause the
runtime to throw an OverflowExcepti on.
j = checked((int)i) ;
2.
When I experiment with a uint instead of an int, and increment it by
500,000 at a time, i notice the value outputted to the console doesn't
steadily increase, but increases, then decreases, then increases
etc...
why is this? what is going on?
The uint variable is overflowing and then being set to 0 + the overflow amount.
For example:
Best Regards,
Dustin Campbell
Developer Express Inc. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Nathan Sokalski |
last post by:
I want to determine the operating system using ASP (or VBScript inside of
ASP). I tried to get it using the Request.
ServerVariables(EnvironmentVariable) method. On the web page about ASP in
which I found this method, it gave a list of possible values for the
EnvironmentVariable parameter. One of these, which is the one that I tried
to use, was HTTP_UA_OS. However, this value did not seem to work, and when I
displayed a list of possible...
|
by: Grasshopper |
last post by:
Hi,
I am automating Access reports to PDF using PDF Writer 6.0. I've
created a DTS package to run the reports and schedule a job to run this
DTS package. If I PC Anywhere into the server on where the job is
running, the job runs sucessfully, PDF files got generated, everything
is good. If I scheduled the job to run at the time that I am not
logged into the server, Access is not able to print to the printer.
The error is pretty...
|
by: Lokkju |
last post by:
I am pretty much lost here - I am trying to create a managed c++
wrapper for this dll, so that I can use it from c#/vb.net, however, it
does not conform to any standard style of coding I have seen. It is
almost like it is trying to implement it's own COM interfaces...
below is the header, and a link to the dll+code:
Zip file with header, example, and DLL:...
|
by: Jen |
last post by:
I'm using Microsoft's own VB.NET FTP Example:
http://support.microsoft.com/default.aspx?scid=kb;en-us;832679
I can get the program to create directories, change directories, etc.,
but I can't get it to upload a file to the FTP server. I just get a
"Cannot connect to remote server" error after this TRY:
s = New Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp)
|
by: Loane Sharp |
last post by:
Hi there
I use the FileStream object to download a zip file over the internet to my
local disk. The file downloads successfully, but when I attempt to unzip it,
I'm told that the file is in use by another process. This occurs even if I
release the object using fs.Close() and fs = Nothing.
Please help (my code is given below)
Best regards
| |
by: ransoma22 |
last post by:
I developing an application that receive SMS from a connected GSM
handphone, e.g Siemens M55, Nokia 6230,etc through the data cable.
The application(VB.NET) will receive the SMS automatically, process and
output to the screen in my application when a message arrived. But the
problem is how do I read the SMS message immediately when it arrived
without my handphone BeEPINg for new message ? I read up the AT
commands, but when getting down...
|
by: Claudio Grondi |
last post by:
In the process of learning about some deeper details of Python I am
curious if it is possible to write a 'prefix' code assigning to a and b
something special, so, that Python gets trapped in an endless loop in a
line with:
if a==b: print 'OK'
I mean, it would be of much help to me on my way to understanding Python
to know how such prefix code leading to an endless loop can look like
and if it is eventually not possible to write such...
|
by: jkimbler |
last post by:
As part of our QA of hardware and firmware for the company I work for,
we need to automate some testing of devices and firmware. Since not
everybody here knows C#, I'm looking to create a new scripting
language that makes writing automated tests simpler. Really, I'm
looking to kind of abstract the power of the C# language into a
simpler language that's easier to learn. The script files would be
interpreted by a script interpreter...
|
by: jombloboy |
last post by:
I'm trying to find the biggest fibonacci number a double variable can handle, my loop looks like this:
double num1 = 1;
double num2 = 2;
double fib = 0;
{
while ( fib >= 0 )
{
fib = num1 + num2;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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,...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |