// // main.cpp - test driver for quantity library // // Copyright (c) 2001 by Michael S. Kenniston. The most recent // version will be available at www.boost.org. Permission is granted // to use this code without restriction so long as this copyright // notice appears in all source files. // This code is provided as-is, with no warrantee of correctness. /* This program demonstrates simple use of the "quantity" library, which allows compile-time type-safety for the dimensionality (meter vs. kilogram) and units (meter vs. foot) of variables representing physical quantities. */ #include "quantity.hpp" #include "quantity_types.hpp" #include "si_units.hpp" // optional #include "imperial_units.hpp" // optional #include "physical_constants.hpp" // optional #include "custom.hpp" // roll-your-own #include #include #include #include using namespace quantities; using namespace std; int main() { cout << "Demonstration of quantity library." << endl; // Output of built-in units. cout << "meter = " << meter() << endl; cout << "kilogram = " << kilogram() << endl; cout << "second = " << second() << endl; cout << "ampere = " << ampere() << endl; cout << "kelvin = " << kelvin() << endl; cout << "mole = " << mole() << endl; cout << "candela = " << candela() << endl; // Mixed double and quantity arithmetic cout << meter() * 1.2 << " is the same as " << 1.2 * meter() << endl; cout << meter() / 2 << " is the reciprocal of " << 2 / meter() << endl; quantity< mass_d > mass( 2 * kilogram() ); mass = 3 * kilogram(); mass = kilogram() * 3.5; mass *= 4; mass = mass / 5; mass /= 6; cout << mass << endl; // quantity arithmetic quantity< length_d > length( 4 * meter() ); length = length + 5 * meter(); length += 6 * meter(); length = 7 * meter() - length; length -= 8 * meter(); cout << length << endl; length = -length; quantity< length_d > width( 1.2 * meter() ); quantity< length_d > height( 2.3 * meter() ); quantity< area_d > area( length * width ); cout << "volume = " << height * area << endl; quantity< speed_d > speed = length / second(); length = speed * second(); // Relational operators; bool b; b = ( length == width ); b = ( length != width ); b = ( length > width ); b = ( length >= width ); b = ( length < width ); b = ( length <= width ); // Output of units with a range of exponents. cout << "kitchen sink = " << 123456789 * meter() * meter() * meter() * kilogram() * kilogram() * second() / kelvin() / mole() / mole() / candela() / candela() / candela() << endl; // Explicit unit conversion is required on input // (multiply by the desired unit). double d = 4.5; // could be read from input length = d * kilo() * meter(); length = d * mile(); // Implicit conversion to SI units is the default on output. cout << "1 mile = " << mile() << endl; // For explicit unit conversion on output, divide by the desired unit. cout << "1 kilometer = " << kilo() * meter() / mile() << " mile" << endl; // quantity operations can yield a double. double r = length / width; r = speed * ( second() / meter() ); // Arrays and vectors of quantity are allowed. quantity< force_d > forcearray[ 10 ]; vector< quantity< power_d > > powervec; // Standard algorithms can be used on physical quantities. for( int i = 0; i < 10; i++ ) { powervec.push_back( watt() * double( rand()) / RAND_MAX ); } sort( powervec.begin(), powervec.end() ); cout << "power vector = " << endl; vector< quantity< power_d > >::iterator iter; for( iter = powervec.begin(); iter != powervec.end(); iter++ ) { cout << " " << *iter / watt() << " W" << endl; } quantity< power_d > sum( zero() ); if ( sum == zero() ) cout << "init correctly\n"; sum = zero(); for( iter = powervec.begin(); iter != powervec.end(); iter++ ) { sum += *iter; } cout << "sum = " << sum << endl; if( sum > zero() ) cout << "sum is greater than zero\n"; // Custom types and units can be defined and used. quantity< warp_factor_d > full_power = 3.27 * mega() * scotty(); cout << "full power = " << full_power << " = " << full_power / scotty() << " Sc" << endl; cout << "60 cubic meters = " << ( 3 * meter() ) * ( 4 * meter() ) * ( 5 * meter() ) << endl; // PROHIBITED /* quantity< length_d > bad( 4 ); cout << "bad is " << bad << "\n"; speed = 4; r = length; speed = length; cout << speed + 4; cout << 4 - speed; cout << speed + length; cout << speed - 4; cout << 4 - speed; cout << speed - length; b = ( speed == 4 ); b = ( speed != 4 ); b = ( speed < 4 ); b = ( speed <= 4 ); b = ( speed > 4 ); b = ( speed >= 4 ); b = ( 4 == speed ); b = ( 4 != speed ); b = ( 4 < speed ); b = ( 4 <= speed ); b = ( 4 > speed ); b = ( 4 >= speed ); b = ( speed == length ); b = ( speed != length ); b = ( speed < length ); b = ( speed <= length ); b = ( speed > length ); b = ( speed >= length ); */ return 0; }