none
How can I store a large number like 33,800,242,033,268,500,000,000,000,000,000.00 on a variable? RRS feed

  • Question

  • Hello!

    I'd like to know whether there's a way to store a large number as the title says. I declared some variables as decimal and they worked fine. But for a combination of constants I've got the number 33,800,242,033,268,500,000,000,000,000,000.00 as result and VS showed the following error:

    System.OverflowException: 'Number was either too large or too small for a Decimal'

    Is there a way to store such large number on a different type of variable?

    Saturday, September 29, 2018 9:55 PM

All replies

  • You can find detail of the limitation for a types here 

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/reference-tables-for-types


    Sincerely, Highly skilled coding monkey.

    Saturday, September 29, 2018 10:17 PM
  • Possibly change the units there is a lot of nothing. The number is only ?? sig figs accurate.

    for example:   

         1 GPa = 1 * 10 ^ 9 = 1000000000 Pa

    your value is  3.3800242033265E+30

    Decimal:

    "largest possible value is +/-79,228,162,514,264,337,593,543,950,335  (+/-7.9228162514264337593543950335E+28 "

    https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/data-types/decimal-data-type

    Your value is just over decimal.

    Double positive: "from 4.94065645841246544E-324 through 1.79769313486231570E+308 "

    https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/data-types/double-data-type

    Saturday, September 29, 2018 10:58 PM

  • I'd like to know whether there's a way to store a large number as the title says. I declared some variables as decimal and they worked fine. But for a combination of constants I've got the number 33,800,242,033,268,500,000,000,000,000,000.00 

    Is there a way to store such large number on a different type of variable?

    While not directly applicable to your case - as you use decimals - for 
    future reference when working with very large integer values, see:

    BigInteger Struct
    https://docs.microsoft.com/en-us/dotnet/api/system.numerics.biginteger?view=netframework-4.7.2

    "The BigInteger type is an immutable type that represents an arbitrarily 
    large integer whose value in theory has no upper or lower bounds."

    Example:

    Imports System.Numerics
    Module Module1
        Sub Main()
            Dim positiveString As String = "33800242033268500000000000000000"
            'Dim positiveString As String = "3380024203326850000000000000000000"
            Dim posBigInt As BigInteger = 0
    
            Try
                posBigInt = BigInteger.Parse(positiveString)
                Console.WriteLine(posBigInt)
            Catch ex As FormatException
                Console.WriteLine("Unable to convert the string '{0}' to a BigInteger value.",
                                      positiveString)
            End Try
        End Sub
    End Module
    

    - Wayne

    Saturday, September 29, 2018 11:40 PM

  • I'd like to know whether there's a way to store a large number as the title says. I declared some variables as decimal and they worked fine. But for a combination of constants I've got the number 33,800,242,033,268,500,000,000,000,000,000.00 

    Is there a way to store such large number on a different type of variable?

    While not directly applicable to your case - as you use decimals - for 
    future reference when working with very large integer values, see:

    BigInteger Struct
    https://docs.microsoft.com/en-us/dotnet/api/system.numerics.biginteger?view=netframework-4.7.2

    "The BigInteger type is an immutable type that represents an arbitrarily 
    large integer whose value in theory has no upper or lower bounds."

    Example:

    Imports System.Numerics
    Module Module1
        Sub Main()
            Dim positiveString As String = "33800242033268500000000000000000"
            'Dim positiveString As String = "3380024203326850000000000000000000"
            Dim posBigInt As BigInteger = 0
    
            Try
                posBigInt = BigInteger.Parse(positiveString)
                Console.WriteLine(posBigInt)
            Catch ex As FormatException
                Console.WriteLine("Unable to convert the string '{0}' to a BigInteger value.",
                                      positiveString)
            End Try
        End Sub
    End Module

    - Wayne

    Hello Wayne. Thanks for taking the time to answer my question.

    The problem with the solution you give is that the Number I need to store can be as large as the one I showed or as small as 1.00E-30, so Integer type is not an option since it can't contain decimals.

    Thank you for trying, though.

    Saturday, September 29, 2018 11:59 PM

  • The problem with the solution you give is that the Number I need to store can be as large as the one I showed or as small as 1.00E-30, so Integer type is not an option since it can't contain decimals..

    Yes, I noted in my comments that you are using decimals so that struct
    may not be directly applicable. Note the following however:

    (1) The BigInteger struct can handle negative values as well as positive.

    (2) Depending on what you are doing with the values, you can often work 
    with integers in place of decimals with some manipulation of the values.
    For example, 10099 is 100.99 multiplied by 100. So math can be done using
    integers, and the final result can be presented as a real number. In
    other words, work with an *implied* decimal and display with an explicit 
    decimal.

    - Wayne

    Sunday, September 30, 2018 12:43 AM
  • If money is not an obstacle, see this offering:

    BigNumbers QuickStart Sample (Visual Basic)
    https://www.extremeoptimization.com/QuickStart/VisualBasic/BigNumbers.aspx

    "Illustrates the basic use of the arbitrary precision classes: 
    BigInteger, BigRational, BigFloat in Visual Basic."

    - Wayne

    Sunday, September 30, 2018 12:56 AM
  • Hey Tommy!

    I also thought of changing units (unit is currently 1/cm). But I use the number on several long equations which are set to work with that unit. Changing it will hard work for several days. 

    I guess I'd simply limit the values of other constants in order to avoid such large numbers, which are actually useless in practice and I'd use them just for hypothetical examples.

    Thanks for taking the time to answer. I hope you're doing fine!
    Sunday, September 30, 2018 1:53 AM

  • The problem with the solution you give is that the Number I need to store can be as large as the one I showed or as small as 1.00E-30, so Integer type is not an option since it can't contain decimals..

    Yes, I noted in my comments that you are using decimals so that struct
    may not be directly applicable. Note the following however:

    (1) The BigInteger struct can handle negative values as well as positive.

    (2) Depending on what you are doing with the values, you can often work 
    with integers in place of decimals with some manipulation of the values.
    For example, 10099 is 100.99 multiplied by 100. So math can be done using
    integers, and the final result can be presented as a real number. In
    other words, work with an *implied* decimal and display with an explicit 
    decimal.

    - Wayne

    That's a good one too, similar to what Tommy said about changing units.

    Problem is, that number is calculated from trigonometrical equations with several constants, so decimals could be very important for one case and irrelevant for the next one.

    It's kind of complicated.

    Here I show you one of those equations:

     decDdC4 = Math.Sinh(dblCaracteristicaSistema * dblLongitud * 100) * (Math.Cosh(dblCaracteristicaSistema * dblPosicionCarga4 * 100) * Math.Sin(dblCaracteristicaSistema * (dblLongitud - dblPosicionCarga4) * 100) - Math.Sinh(dblCaracteristicaSistema * dblPosicionCarga4 * 100) * Math.Cos(dblCaracteristicaSistema * (dblLongitud - dblPosicionCarga4) * 100)) + Math.Sin(dblCaracteristicaSistema * dblLongitud * 100) * (Math.Sinh(dblCaracteristicaSistema * (dblLongitud - dblPosicionCarga4) * 100) * Math.Cos(dblCaracteristicaSistema * dblPosicionCarga4 * 100) - Math.Cosh(dblCaracteristicaSistema * (dblLongitud - dblPosicionCarga4) * 100) * Math.Sin(dblCaracteristicaSistema * dblPosicionCarga4 * 100))

    Most of constants are lenghts that come from textboxes, but dblCaracteristicaDelSistema is calculated as follows:

     dblCaracteristicaSistema = ((dblBalasto * (dblAncho * 100)) / (4 * dblModuloElasticidad * dblInercia)) ^ (1 / 4)

    Where,

    dblModuloElasticidad is Elastic Modulus of Concrete Members (According to ACI 15100(f'c)^1/2, Kgf/cm2 units)

    dblInercia is the inertia of the crossed section of the beam (which can be a rectangular section or an inverted "T" section, so Steiner's theorem must be used on the second option)

    So yeah, estimating the magnitude of decDdC4 is not an easy process as you can figure out.

    Anyways, I really appreciate your time and effort to help me. Thanks!


    Sunday, September 30, 2018 2:20 AM
  • Alex,

    "dblModuloElasticidad is Elastic Modulus of Concrete Members (According to ACI 15100(f'c)^1/2, Kgf/cm2 units)"

    Cant you use KN/m2 instead for the constant?

    I don't see why you cant divide the whole equation by 10^9 or any arbitrary factor?

    Where do you get small numbers "as small as 1.00E-30" ?

    What about logarithms ?

    Sunday, September 30, 2018 8:43 AM
  • If money is not an obstacle, see this offering


    Sadly, Money is a big obstacle. I'm from Venezuela.

    Anyways, thanks for the information, Wayne.

    Monday, October 1, 2018 1:31 AM
  • Tommy, Yes, I can use different units, but that includes changing a lot of equations to adapt them to the change. I can also divide the equation by an arbitrary factor (just like changing units), but the result is the same. I use the result of that equation to do some other calculations. So, that means extra-work. As you can see, the equations depends on many variables, and trigonometrical operators, so telling you where do I get those small numbers is not easy. Logarithms will only complicate the, already, long equations. These are all good ideas, and I appreciate your effort to help me. I'm afraid i'm running out of time to finish the software, so I guess I'll just limit values to avoid this kinds of numbers. Thanks for all, Tommy!
    Monday, October 1, 2018 1:46 AM

  • Sadly, Money is a big obstacle. I'm from Venezuela.


    Well, given the financial constraints you could explore the free 
    offerings such as GMP.

    https://gmplib.org/

    However, most seem to target C or C++ so to use one of them you would
    face a learning curve, C/C++ and Basic interoperability, etc. It could 
    most likely be done by creating a DLL in C/C++ that exports the needed
    arbitrary precision methods. Then use that DLL in a VB program.

    But your apparent time constraints may limit the feasibility of that 
    approach for your current project.

    Arbitrary-precision arithmetic
    https://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic

    List of arbitrary-precision arithmetic software
    https://en.wikipedia.org/wiki/List_of_arbitrary-precision_arithmetic_software

    - Wayne

    Monday, October 1, 2018 2:22 AM