I am trying to use the Boost.Units library in my conversion of a frequency (for instance, 20kHz) into the duration of its period (50000 nanoseconds). I don't want to use floating point.
The trouble I am having is that simply taking the reciprocal 1/f causes zero-truncation, so even after converting to nanoseconds the result comes out 0ns. If I instead convert to megahertz first and then take the reciprocal, again the first step causes zero-truncation.
One workaround I have found is to use boost::rational as the value_type of the quantity. This works, but it strikes me as ugly. Is there any better solution? I'm pretty new to the Units library and it's a bit overwhelming so far.
My solution for now:
typedef make_scaled_unit<si::time, scale<10, static_rational<-9> > >::type nanosecond;
typedef quantity<si::frequency,rational<unsigned>> freq_quantity;
typedef quantity<nanosecond,rational<unsigned>> time_quantity;
rational<unsigned> val(20);
freq_quantity Fs(val*si::kilo*si::hertz);
cout << Fs << endl;
cout << static_cast<time_quantity>(rational<unsigned>(1)/Fs) << endl;