2

I am making a scientific calculator in C# Winform application. In that I am using double to store the answers retrieved from the calculation of function e^x. But datatype double is having some limit if it exceeds then it show the overflow related error. So can you tell me what is the largest data type to store numbers in C#.

Example: 100!

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Kishan V. Shah
  • 57
  • 1
  • 2
  • 4
  • 1
    There is something else going wrong if you are getting overflow for 100! in double. The result, about 9.3326215444e+157, is well within the range of finite IEEE 754 64-bit binary numbers. Of course, it cannot be represented exactly as a double. – Patricia Shanahan Jul 21 '14 at 23:56

4 Answers4

7

double is the largest floating point precision data type in C#. You have BigInteger, but not a BigFloat or BigDouble.

There is however in the Base Class Library on CodePlex an implementation of BigRational, which are in fact two BigIntegers:

From there website:

BigRational builds on the BigInteger introduced in .NET Framework 4 to create an arbitrary-precision rational number type. A rational number is a ratio between two integers, and in this implementation BigIntegers are used for the numerator and denominator.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
3

The datatypes with possible values are as follows :

enter image description here

Reference link : https://social.msdn.microsoft.com/Forums/vstudio/en-US/921a8ffc-9829-4145-bdc9-a96c1ec174a5/decimal-vs-double-difference?forum=csharpgeneral

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Sagar Dev Timilsina
  • 1,310
  • 15
  • 32
3

You should consider using System.Numerics.BigInteger data type. It represents an arbitrarily large signed integer. They have virtually no limit at all unlike you might have observed for other number data types available in .NET framework. You can read more about it here.

To use this structure you need to refer System.Numerics.dll in your project and then include below namespace at the top of the code file:

using System.Numerics;

You can go through this post on how to add reference to an assembly in case you get stuck somewhere.

Below is the sample code showing how to parse a very large number string and convert it into BigInteger:

var aVeryVeryHugeNumber = System.Numerics.BigInteger.Parse("31415926535897932384626433832795");

If you try to parse such a string representing a huge number, then it will result in System.OverflowException even with ulong.Parse data type. It fails with below error message:

Value was either too large or too small for a UInt64.

RBT
  • 24,161
  • 21
  • 159
  • 240
-4
static void Main(string[] args)
    {
        Console.WriteLine("Geef een getal in?");
        double getal1 = Convert.ToDouble(Console.ReadLine());
        Console.WriteLine("Geef een tweede getal in?");
        double getal2 = Convert.ToDouble(Console.ReadLine());
        Console.WriteLine("Geef een derde getal in?");
        double getal3 = Convert.ToDouble(Console.ReadLine());

        if (getal1 > getal2 && getal1  > getal3)
        {
            Console.WriteLine(getal1 + "is het grootste getal");

        }
        if (getal2 > getal1 && getal2 > getal3) 
        {
            Console.WriteLine(getal2 + "is het grootste getal");
        }
        if( getal3 > getal2 && getal3 > getal1 )
        {
            Console.WriteLine(getal3 + "is het grootste getal");
        }
    }