Hugh Rees-Beaumont
Hugh Rees-Beaumont

Reputation: 9

System overflow when passing large numbers into a function

I'm trying to write a program to simulate the solar system and I'm passing large numbers into a function to calculate the gravitational force. The line of code in question is:

Earth.ptyForce = CalculateForce(Earth.ptyMass, Sun.ptyMass, dblSeparation)

Where:

I just get a system overflow and I suspect it's because the numbers are too large, is there any way around this? I've considered just passing smaller numbers into the function and just scaling them up but I'd rather avoid doing that if possible.

Just in case it's needed the CalculateForce Fucnction is as follows:

Public Function CalculateForce(ByVal intMass1 As Integer, ByVal intMass2 As Integer, ByVal dblDistance As Double)
    dblDistance *= dblDistanceScalar
    Return dblGravitationalConstant * intMass1 * intMass2 / dblDistance ^ 2
End Function

The error specifically reads:

System.OverflowException: 'Arithmetic operation resulted in an overflow.'

Upvotes: 0

Views: 49

Answers (1)

CruleD
CruleD

Reputation: 1173

Your number is too big, 10^30 is larger than 2^32 read this:

https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/numeric-data-types

Upvotes: 2

Related Questions