Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Calculate Your Weight On Various Heavenly Bodies

by Ovid (Cardinal)
on May 16, 2007 at 10:57 UTC ( [id://615742]=CUFP: print w/replies, xml ) Need Help??

OK, so it's not terribly unique, but I thought it was a bit of fun. The background (and the explanation of some odd bits) is at my personal blog. I wrote this because I was curious about my weight on the new Earth which was discovered. The program takes your current weight (in any units) as an argument. Defaults to my weight :)

#!/usr/bin/perl use strict; use warnings; my $weight = shift || 189; # you can manually change the weight here, + if you prefer # for simplicity's sake, we assume Audrey and Johnny are spherical my @data = ( # body mass in kg radius in km [ 'Mercury', 3.302 * 10**23, 2439.7 ], [ 'Venus', 4.8685 * 10**24, 6051.9 ], [ 'Earth', 5.9736 * 10**24, 6378.137 ], [ 'Moon', 7.3477 * 10**22, 1748.14 ], [ 'Mars', 6.4185 * 10**23, 3402.5 ], [ 'Ceres', 9.46 * 10**20, 950 ], # rough es +timates [ 'Jupiter', 1.8986 * 10**27, 71492 ], [ 'Saturn', 5.6846 * 10**26, 60286 ], [ 'Uranus', 8.6832 * 10**25, 25559 ], [ 'Neptune', 10.243 * 10**25, 24764 ], [ 'Pluto', 1.305 * 10**22, 1195 ], # rough es +timates [ 'Eris', 1.6 * 10**22, 1200 ], # rough es +timates [ 'Audrey Hepburn', 49.8951607, 0.0017018 ], [ 'Johnny Depp', 70.3068174, 0.001778 ], ); my ( %mass_of, %radius_of ); foreach my $data (@data) { my $body = $data->[0]; $mass_of{$body} = $data->[1]; $radius_of{$body} = $data->[2]; } $mass_of{Hope} = 5 * $mass_of{Earth}; $radius_of{Hope} = 1.5 * $radius_of{Earth}; $mass_of{'Heavy Earth'} = 2 * $mass_of{Earth}; $radius_of{'Heavy Earth'} = $radius_of{Earth}; $mass_of{'Fat Earth'} = $mass_of{Earth}; $radius_of{'Fat Earth'} = 2 * $radius_of{Earth}; print "If you weighed $weight units ...\n"; foreach my $body ( map( { $_->[0] } @data ), 'Hope', 'Heavy Earth', 'F +at Earth' ) { printf "... you would weigh %.2f units on '$body'\n" => weight_on( +$body); } sub weight_on { my $body = shift; return $weight * ( gravity($body) / gravity('Earth') ); } sub gravity { my $body = shift; return density($body) * $radius_of{$body}; } sub density { my $body = shift; return $mass_of{$body} / $radius_of{$body}**3; }

Cheers,
Ovid

New address of my CGI Course.

Replies are listed 'Best First'.
Re: Calculate Your Weight On Various Heavenly Bodies
by Discipulus (Canon) on May 16, 2007 at 12:27 UTC
    I really hope your default is in lbs...but
    if ($default->is_KG()){ require Rope; my $exerc = Rope->new(weight=>'+5'); $exerc->skipping until $default < 100; }
    kidding..

      I really hope your default is in lbs

      I'm a big fan of using units. There wasn't really anything (update: unit-self-aware) on CPAN at the time, so I wrote Math::Units::PhysicalValue, which would make the initial weight so much clearer.

      use Math::Units::PhysicalValue qw(PV); my $weight = PV "170 lb"; my $mass = $weight + "0 kg"; print "My mass on earth at sea level is $mass.\n";

      -Paul

        I feel obligated to point out here that your mass is not dependent on your position. :-) (Oddly enough, though, nobody seems to want to figure their weight in Newtons.)



        If God had meant us to fly, he would *never* have given us the railroads.
            --Michael Flanders

      Yes, that default is definitely pounds, not stone, kilograms, ounces (avoirdupois or otherwise), or anything else. It's still too high of a default, though.

      Cheers,
      Ovid

      New address of my CGI Course.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://615742]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2025-03-21 06:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (63 votes). Check out past polls.