RSS

================================================================================

Sad Code

================================================================================

Whether you write code for a living, as tools to get the job done, or just for fun, you probably have some very sad, lonely code that's been abandoned.

Instead of letting it slowly wilt away forgotten, set it free! Send it to sadcode@me.com with a short descriptive name. I'll post it in your formatting.

If something needs fixing, in code or otherwise, send a note that something is up.

If it works 99% of the time, tell me and I'll tag it as Adopt Me!

================================================================================

Source code posted here is copyright of its author(s), should they chose to identify themselves in the code. Unless otherwise specified, all content here is distributed under a Creative Commons Attribution Non-Commercial Share-Alike license.

================================================================================

Last login: 04/15/2009 00:01:00

Fortran 95: Escape Velocity of Select Celestial Bodies

! David Vann
! Given user input of a celestial body (Earth, Moon, Ceres, or Jupiter) and a list of
! needed dimensions, calculate and display the escape velocity
! Input Variables: body, used to choose which celestial body's escape velocity to calculate
! Output Variables: e_velocity_mantissa, e_velocity_exponent, used to calculate the
! mantissa and exponent of the escape velocity separately because of odd compiler
! errors which shouldn't happen that were caused by using numbers that are too large
PROGRAM escape_velocity
! Declare variables and initialize to zero
IMPLICIT NONE
! Will not compile if an undeclared variable is used (such as a typo)
INTEGER, PARAMETER :: DBL = SELECTED_REAL_KIND(p=15, r=100)
INTEGER :: body = 0
INTEGER, PARAMETER :: Earth = 1, Moon = 2, Ceres = 3, Jupiter = 4
REAL(KIND=DBL) :: e_velocity_mantissa = 0, e_velocity_exponent = 0, e_velocity_exponent_extra = 0,  mass_mantissa = 00
REAL(KIND=DBL) :: mass_exponent = 0, radius_mantissa = 0, radius_exponent = 0
REAL(KIND=DBL), PARAMETER :: G_mantissa = 6.673, G_exponent = -11
! G = Gravitational constant in Nm**2 * kg**-2
! Tell user how to enter input
WRITE(*,*) 'Enter a celestial body:'
WRITE(*,*) 'Earth   =', Earth
WRITE(*,*) 'Moon    =', Moon
WRITE(*,*) 'Ceres   =', Ceres
WRITE(*,*) 'Jupiter =', Jupiter
! Get input
READ(*,*) body
! Determine what the input means
SELECT CASE (body)
CASE (Earth)
mass_mantissa = 6.0
mass_exponent = 24
! mass in kilograms, broken down to avoid too large a number in an arithemetic operation error
radius_mantissa = 6.4
radius_exponent = 6
! radius in meters
WRITE(*,*) 'Earth selected.'
CASE (Moon)
mass_mantissa = 7.4
mass_exponent = 22
radius_mantissa = 1.7
radius_exponent = 6
WRITE(*,*) 'Moon selected.'
CASE (Ceres)
mass_mantissa = 8.7
mass_exponent = 20
radius_mantissa = 4.7
radius_exponent = 5
WRITE(*,*) 'Ceres selected.'
CASE (Jupiter)
mass_mantissa = 1.9
mass_exponent = 27
radius_mantissa = 7.1
radius_exponent = 7
WRITE(*,*) 'Jupiter selected.'
CASE DEFAULT
WRITE(*,*) 'USER ERROR: INVALID INPUT'
STOP
END SELECT
! calculate using set values
e_velocity_mantissa = SQRT( 2 * G_mantissa * mass_mantissa / radius_mantissa )
e_velocity_exponent = ( mass_exponent / 2 ) - ( radius_exponent / 2 ) + ( G_exponent / 2 )
DO WHILE ( e_velocity_mantissa < 1 )
e_velocity_mantissa = e_velocity_exponent * 10
e_velocity_exponent = e_velocity_exponent + 1
END DO
DO WHILE ( e_velocity_mantissa >= 10 )
e_velocity_mantissa = e_velocity_exponent / 10
e_velocity_exponent = e_velocity_exponent - 1
END DO
WRITE(*,*) 'With a mass of', mass_mantissa, '* 10 ^', mass_exponent
WRITE(*,*) 'and a radius of', radius_mantissa, '* 10 ^', radius_exponent
WRITE(*,*) 'the escape velocity is:', e_velocity_mantissa, '* 10 ^', e_velocity_exponent, 'meters per second or'
e_velocity_mantissa = e_velocity_mantissa * 10**e_velocity_exponent
WRITE(*,*) e_velocity_mantissa, 'meters per second'
END PROGRAM escape_velocity

Comments (View)
permalink

--------------------------------------------------------------------------------

pg 1 of 15

================================================================================

================================================================================

================================================================================

Designed: Robert Boylan
Powered: Tumblr