
The first thing I'm trying to do is determine how to represent a velocity in miles per hour. I haven't been able to figure it out from the documentation. Any hints would be appreciated. I also need to figure out the best way to get string representations of quantities that include their units for output. --- Mark Volkmann

AMDG Mark Volkmann wrote:
The first thing I'm trying to do is determine how to represent a velocity in miles per hour. I haven't been able to figure it out from the documentation. Any hints would be appreciated.
// Get the associated base units
#include
I also need to figure out the best way to get string representations of quantities that include their units for output.
#include

On Jan 8, 2009, at 5:08 PM, Steven Watanabe wrote:
AMDG
Mark Volkmann wrote:
The first thing I'm trying to do is determine how to represent a velocity in miles per hour. I haven't been able to figure it out from the documentation. Any hints would be appreciated.
// Get the associated base units #include
#include // Create short typedefs for the components. typedef boost::units::us::mile_base_unit::unit_type mile_unit; typedef boost::units::metric::hour_base_unit::unit_type hour_unit; // make the unit for miles/hour typedef boost::units::divide_typeof_helper ::type miles_per_hour; quantity
mph = 80.0*miles_per_hour(); I also need to figure out the best way to get string representations of quantities that include their units for output.
#include
will give you operator<<.
Thanks!
Why does the the following simple code output "605mi h^-1" instead of
"605 mi/h"? Why is the "^-1" at the end?
#include <iostream>
#include

Thanks!
Why does the the following simple code output "605mi h^-1" instead of "605 mi/h"? Why is the "^-1" at the end?
#include <iostream> #include
#include #include using namespace boost::units; using namespace std;
typedef us::mile_base_unit::unit_type mile_unit; typedef metric::hour_base_unit::unit_type hour_unit; typedef divide_typeof_helper
::type miles_per_hour; int main() { quantity
mph = 605.0 * miles_per_hour(); cout << mph << endl; return 0; }
The output code does not give special status to exponents of -1, but just treats them consistently. If you want specialized output, see examples/composite_output.cpp for an example. Matthias

On Jan 9, 2009, at 1:16 PM, Matthias Schabel wrote:
Thanks!
Why does the the following simple code output "605 mi h^-1" instead of "605 mi/h"? Why is the "^-1" at the end?
#include <iostream> #include
#include #include using namespace boost::units; using namespace std;
typedef us::mile_base_unit::unit_type mile_unit; typedef metric::hour_base_unit::unit_type hour_unit; typedef divide_typeof_helper
::type miles_per_hour; int main() { quantity
mph = 605.0 * miles_per_hour(); cout << mph << endl; return 0; } The output code does not give special status to exponents of -1, but just treats them consistently. If you want specialized output, see examples/composite_output.cpp for an example.
Why is the exponent -1 in this example? I could understand if it was zero, but I don't think -1 makes sense. --- Mark Volkmann

Typically a {-1} exponent just means invert; i.e. h^{-1} = 1/h On Fri, Jan 9, 2009 at 3:59 PM, Mark Volkmann wrote:
On Jan 9, 2009, at 1:16 PM, Matthias Schabel wrote:
Thanks!
Why does the the following simple code output "605 mi h^-1" instead of "605 mi/h"? Why is the "^-1" at the end?
#include <iostream> #include
#include #include using namespace boost::units; using namespace std;
typedef us::mile_base_unit::unit_type mile_unit; typedef metric::hour_base_unit::unit_type hour_unit; typedef divide_typeof_helper
::type miles_per_hour; int main() { quantity
mph = 605.0 * miles_per_hour(); cout << mph << endl; return 0; } The output code does not give special status to exponents of -1, but just treats them consistently. If you want specialized output, see examples/composite_output.cpp for an example.
Why is the exponent -1 in this example? I could understand if it was zero, but I don't think -1 makes sense.
--- Mark Volkmann
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

I could be misunderstanding something fundamental about Boost::Units. Is it possible to declare a class field or method parameter with a type that indicates it represents a velocity without specifying whether the value is in meters per second or miles per hour? I'd like to allow any kind of velocity to be specified and perform operations independent of the units being used. For example, if I set the velocity of some object to 60 miles per hour and then call a method to determine how far it will travel in 2 hours, I'd like to get back 120 miles. I'd also like to be able to set the velocity to 10 meters per second, ask the same question, and get back 72000 meters. --- Mark Volkmann

I could be misunderstanding something fundamental about Boost::Units. Is it possible to declare a class field or method parameter with a type that indicates it represents a velocity without specifying whether the value is in meters per second or miles per hour?
I'd like to allow any kind of velocity to be specified and perform operations independent of the units being used. For example, if I set the velocity of some object to 60 miles per hour and then call a method to determine how far it will travel in 2 hours, I'd like to get back 120 miles. I'd also like to be able to set the velocity to 10 meters per second, ask the same question, and get back 72000 meters.
You are misunderstanding something fundamental. Boost.Units was designed to allow compile-time unit computations with no runtime overhead. In order to achieve this, all unit information is encoded in the unit type. The best way to write units code that is independent of the specific units is to use templated functions; see the functions "work" and "idealGasLaw" defined in examples/kitchen_sink.cpp. It is also possible to do runtime conversions; see examples/ runtime_conversion_factor.cpp and examples/runtime_unit.cpp. Matthias
participants (4)
-
David Conner
-
Mark Volkmann
-
Matthias Schabel
-
Steven Watanabe