0
#include <iostream>
#include <boost/units/quantity.hpp>
#include <boost/units/systems/si/length.hpp>

using namespace boost::units;

struct bu1 : base_unit<bu1, length_dimension, 2001> {};
struct bu2 : base_unit<bu2, length_dimension, 2002> {};

BOOST_UNITS_DEFINE_CONVERSION_FACTOR(bu1, bu2, double, 1.5);

#if 1
BOOST_UNITS_DEFINE_CONVERSION_FACTOR(bu2, bu1, double, 4.0);
#endif

int main(int argc, char *argv[])
{
  quantity<bu1::unit_type> output(1 * bu2::unit_type());

  //  prints 4 or 0.67
  std::cout << output.value() << std::endl;

  return 0;
}

The code prints either 4 or 0.666667 depending on whether or not the second conversion factor is defined. Is this supposed to be by design? There are no two such units in physics that require different conversion factors, are there?

Arlen
  • 6,641
  • 4
  • 29
  • 61

1 Answers1

0

This is a garbage-in / garbage-out behaviour. You do realize that 1.0 / 1.5 != 4.0, don't you? The second conversion factor, if you want to be explicit, should have been (2.0/3.0), not 4.

The first conversion factor specification defined worked both ways already. From the documentation:

If the destination unit is a base unit or a unit that contains only one base unit which is raised to the first power (e.g. feet->meters) the reverse (meters->feet in this example) need not be defined explicitly.

One can argue that there is sort of a bug in that it even let you specify the second conversion factor that didn't agree with the first one.

Also, I hope you use <boost/units/io.hpp> in your production code, specifying the name/symbol for your units, rather than dimensionless printouts like output.value(). I understand it was shorter your way for the question, of course...