0

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:

  • Earth.ptyMass is equal to 5.9736 * 10 ^ 24
  • Sun.ptyMass is equal to 1.989 * 10 ^ 30
  • dblSeparation is only equal to 300 and is scaled inside the function.

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.'

1

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.