STL comments on Rational toy buying - Less Wrong

11 Post author: p4wnc6 19 October 2011 03:36AM

You are viewing a comment permalink. View the original post to see all comments and the full post content.

Comments (64)

You are viewing a single comment's thread. Show more comments above.

Comment author: [deleted] 22 October 2011 07:58:29AM 2 points [-]

Showing my work:

C:\Temp>type meow.cpp
#include <iostream>
#include <ostream>
#include <boost/units/io.hpp>
#include <boost/units/pow.hpp>
#include <boost/units/quantity.hpp>
#include <boost/units/systems/si.hpp>
#include <boost/units/systems/si/prefixes.hpp>
#include <boost/units/systems/si/codata/universal_constants.hpp>
using namespace std;
using namespace boost::units;
using namespace boost::units::si;
using boost::units::si::constants::codata::G;
typedef quantity<boost::units::si::time> quantity_time;
int main() {
const quantity<mass> external_mass(740 * kilograms / kilo);
quantity<length> remaining_length(17 * centi * meters);
const quantity<length> final_length(2 * centi * meters);
quantity_time elapsed_time;
quantity<velocity> total_velocity;
while (remaining_length > final_length) {
const quantity_time t(1 * second);
const quantity<acceleration> a(G * external_mass / pow<2>(remaining_length));
const quantity<length> l = total_velocity * t + 0.5 * a * pow<2>(t);
elapsed_time += t;
remaining_length -= l;
total_velocity += a * t;
}
cout << elapsed_time << endl;
cout << total_velocity << endl;
}
C:\Temp>g++ -Wall -Wextra meow.cpp -o meow.exe && meow
10882 s
6.6012e-005 m s^-1

The final velocity is about 9 inches per hour.

(It would be quite possible to modify this to do a fully "realistic" simulation, with the two pairs of masses and the rotation. That's more work than I want to do at 1 AM, though.)