Hi, I understand that 32bit processors can only natively handle integers that are of a magnitude less than 4,294,967,296. Exactly what happens if I was to write a C# program that tried to mutiply/divide numbers larger than that? Will the compiler take care of the issue for me or will I be unable to do that without writing complex logic? I'm new to application programming (all I know is PHP, MySQL, XML, XHTML and CSS but I've been working on learning C#) so I'm not sure what will happen. I'm guessing the compiler will take care of it for me but I want to know that prior to writing the application.``
| | Shining Arcanine | Rather than simply using a int (Int32), you can actually use a long (Int64) which can handle integers up to 9,223,372,036,854,775,807.
If you write operations that cause numbers to be greater than their holding type, one of two things will happen:
1. If 'Check for arithmetric overflow/underflow' is checked (Project Properties -> Build -> Advanced) or if the statement is within a checked statement than an OverflowException will be thrown.
2. If the above option is unchecked or if the statement is within an unchecked statement than no exception will be thrown and the operation will overflow/underflow.
For example, in the following statements the first will throw an OverflowException, whereas in the second value will contain the lowest possible value that an long (Int64) can contain (-9,223,372,036,854,775,808). The third statement will depend on whether the above option is checked or not:
1. long value = checked(Int64.MaxValue + 1);
2. long value = unchecked(Int64.MaxValue + 1);
3. long value = Int64.MaxValue + 1;
VS Code Analysis Team | My Blog: http://davidkean.net | FxCop Blog: http://blogs.msdn.com/FxCop | | David M. Kean | I'd like to process numbers approximately 10^(10^10) digits long so unfortunately Int64 is too small for the numbers I'd like to process. So far I've been using Double but it is also too small for what I'd like to do.
| | Shining Arcanine | A double will store numbers that large, but only to a certain number of significant figures. To store a number precisely you need enough bits to store the number in binary. So 10^10^10 is 1x10^100. To figure how many bits you need, see what power of two is bigger than the number, fiddling with calculator I get 2^333 = 1.7*10^100
so you would need 333 bits to fit integers of this magnitude, ( int333 ). There are no default types for this, so if you want precision you will have to make a big number class, and do all the methods by hand. Something like this: http://www.cs.sunysb.edu/~skiena/392/lectures/week5/
| | jo0ls | jo0ls, thanks for the information. The magnitude I need to be able to calculate numbers at actually was 10^10000000000 (hence my use of parenthesises) so I'll need Int16777216. Writing all of the methods for it by hand will be fun, especially with relatively little C# experience and knowledge. I'll get started on it tomorrow. Thanks again.
| | Shining Arcanine |
|