// // main.cpp - test driver for quantity library // // Copyright (c) 2001 by Michael S. Kenniston. The most recent // version will be available at www.xnet.com/~msk/quantity. 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 quantity. */ #define QUANTITY_REP_TYPE float #include "cuj/quantity/quantity.hpp" // required #include "cuj/quantity/quantity_io.hpp" // required for operator<< #include "cuj/quantity/other_units.hpp" // optional #include "cuj/quantity/physical_constants.hpp" // optional #include "user_example.hpp" // roll-your-own #include #include #include #include #include #ifndef BOOST_MSVC using cuj::quantity::quantity; using cuj::quantity::meter; using cuj::quantity::kilogram; using cuj::quantity::second; using cuj::quantity::ampere; using cuj::quantity::kelvin; using cuj::quantity::mole; using cuj::quantity::candela; using cuj::quantity::mass_d; using cuj::quantity::length_d; using cuj::quantity::area_d; using cuj::quantity::volume_d; using cuj::quantity::speed_d; using cuj::quantity::force_d; using cuj::quantity::power_d; using cuj::quantity::kilo; using cuj::quantity::mega; using cuj::quantity::mile; using cuj::quantity::watt; using cuj::quantity::nth_power; using cuj::quantity::time_interval_d; using cuj::quantity::frequency_d; using cuj::quantity::power_d; using cuj::quantity::square; using cuj::quantity::cube; using cuj::quantity::nth_root; using cuj::quantity::nth_power; using cuj::quantity::dimensions; using cuj::quantity::c; using cuj::quantity::furlong; using cuj::quantity::fortnight; using cuj::quantity::hour; using cuj::quantity::tera; using cuj::quantity::sqrt; #else using namespace cuj::quantity; #endif using hyperspace::scotty; using hyperspace::warp_factor_d; 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; cout << "+ quan: " << + meter() << endl; cout << "quan + quan: " << kilogram() + kilogram() << endl; quantity< time_interval_d > t( second() ); t += second(); cout << "- quan: " << - ampere() << endl; cout << "quan - quan: " << t - second() << endl; t -= second(); quantity< mass_d > mass( kilogram() ); cout << "num * quan: " << 3 * mass << endl; cout << "quan * num: " << mass * 4 << endl; mass *= 5; cout << "quan *= num: " << mass << endl; cout << "quan * quan: " << kilogram() * kelvin() << endl; quantity< frequency_d > freq; freq = 5 / second(); //* // Mixed double and quantity arithmetic cout << meter() * 1.2f << " is the same as " << 1.2f * meter() << endl; cout << meter() / 2 << " is the reciprocal of " << 2 / meter() << endl; 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.2f * meter() ); quantity< length_d > height( 2.3f * 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.0f * 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 quantity. 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( quantity< power_d >::zero() ); if ( sum == quantity< power_d >::zero() ) cout << "init correctly\n"; sum = quantity< power_d >::zero(); for( iter = powervec.begin(); iter != powervec.end(); iter++ ) { sum += *iter; } cout << "sum = " << sum << endl; if( sum > quantity< power_d >::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; // Misc stuff. cout << "60 cubic meters = " << ( 3 * meter() ) * ( 4 * meter() ) * ( 5 * meter() ) << endl; length = 3 * meter(); cout << "the side is " << length << "\n"; area = length * length; area = square( length ); cout << "the area is " << area << endl; quantity< volume_d > vol = cube( length ); cout << nth_root< 2 >( area ) << endl; cout << "the area is " << nth_power< 2 >( length ) << endl; cout << "the volume is " << nth_power< 3 >( length ) << endl; cout << "the sqrt of the area is " << sqrt( area ) << endl; cout << "the 3rd root of " << 1000 * meter() * meter() * meter() << " is " << nth_root< 3 >( 1000 * meter() * meter() * meter() ) << endl; typedef dimensions< 4, 8, 12, 0, -4, -8, -12 > mongo_d; quantity< mongo_d > nothing( quantity< mongo_d >::zero() ); cout << "mongo = " << nothing << endl; cout << "4th root of mongo = " << nth_root< 4 >( nothing ) << endl; cout << "the speed of light in a vacuum is " << c() / ( tera() * furlong() / fortnight() ) << " terafurlong / fortnight" << endl; cout << 456789 * kilo() * watt() * hour() << "\n"; return 0; }