Jump to content

/[Math] I need some help with a Schwartzchild radius calculator.


Themohawkninja

Recommended Posts

I'm trying to program a calculator that will give me the Schwartzchild radius of an object with a user-inputted mass, however I am getting a negative value for my answer, and I don't know why.

Variable initialization:

final int c=299792458;

double m, r;

final double G=6.67384;

Equation:

r=(2*G*m)/(c*c);

If you type in 123456789, you get -1.1814554364396923.

When I try the equation in Wolfram Alpha (2*(Gravitational Constant)*123456789)/(c^2), I get the much more logical sounding answer of 1.83 x 10^-19 m/kg. (Which is a density, and not a distance, so that makes even less sense).

Link to comment
Share on other sites

From a quick eyeball I would say that c*c is the issue as the result will not fit into an integer. This is causing an overflow, and results in a negative answer. I would recommend precomputing c*c and storing it as a constant double.

Edited by Rich
Link to comment
Share on other sites

From a quick eyeball I would say that c*c is the issue as the result will not fit into an integer. This is causing an overflow, and results in a negative answer. I would recommend precomputing c*c and storing it as a constant double.

Ah, thanks! The output is the same as Wolfram Alpha, so I'm going to assume that it's correct. I'll have to remember to check for runtime overflow errors from now on.

Link to comment
Share on other sites

Odd... I decided to do the calculation for the Sun, which as I understand should be around 3000 meters, however if you type in the mass of the Sun in kg: 1.9891 x 10^30 (I had to type out all the zeros for the program's input), you get: 2.9539241530930125 E 14, which is WAY bigger than 3000. I can only guess it's an overflow error, since 2^64 is only 20 digits, and if it is, how does one initialize a large enough double in Java to hold the value?

Also, Wolfram Alpha has been giving me answers in Stokes, since the equation doesn't seem to cancel out enough units to leave only meters in the equation.

Edited by Themohawkninja
Link to comment
Share on other sites

Odd... I decided to do the calculation for the Sun, which as I understand should be around 3000 meters, however if you type in the mass of the Sun in kg: 1.9891 x 10^30 (I had to type out all the zeros for the program's input), you get: 2.9539241530930125 E 14, which is WAY bigger than 3000. I can only guess it's an overflow error, and if it is, how does one initialize a large enough (I'm hoping that a 64-bit variable will do) double in Java to hold the value?

The mantissa is correct, but the exponent is wrong. Are you sure you typed the correct number of zeros?

Also, Wolfram Alpha has been giving me answers in Stokes, since the equation doesn't seem to cancel out enough units to leave only meters in the equation.

You must specify the units of the mass so try:

(2*(Gravitational Constant)*(mass of the sun))/(c^2)

or

(2*(Gravitational Constant)*(1.9891 x 10^30kg))/(c^2)

Link to comment
Share on other sites

The mantissa is correct, but the exponent is wrong. Are you sure you typed the correct number of zeros?

If by mantissa you mean the numbers to the right of the decimal point (I had to look it up on Google, as that's a new word to me), I inputted 198,910,000,000,000,000,000,000,000,000 (minus the commas). Looking at it with all of the commas, I might have missed one, but it's hard to count all of those zeros without any commas to format. I doubt that one zero will make much of a difference in this case.

You must specify the units of the mass so try:

(2*(Gravitational Constant)*(mass of the sun))/(c^2)

or

(2*(Gravitational Constant)*(1.9891 x 10^30kg))/(c^2)

I did, I actually had to specify c as m/s (it was assuming that c was a variable). I typed in: (2*(Gravitational Constant)*1989100000000000000000000000000 kg)/(89880000000000000 m/s), and got 2954 m^2/s (my bad, it gave a conversion to stokes which would be 2.954*10^7 Stokes)/

Link to comment
Share on other sites

FOUND IT!

G was set to 6.67... when it is actually 6.67x10^-11. My answers were off so badly, because I was multiplying them by a number that is 11 orders of magnitude off!

EDIT: It does however say that the Earth should have a Schwartzchild radius of ~8.7 meters, which I know isn't right, as the correct answer should be around 0.05 meters (around 2 inches). Some further refinements are needed.

EDIT2: Turns out there were three too many zeros for c^2... it all calculates correctly now.

Edited by Themohawkninja
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...