24 November 2011

Boost Modf Function - Integer and Fractional Parts

Have been wanting a function in C++ that would handle finding the integer of a floating point number without integer division. The boost::math::modf function finds both the integer and fractional parts.

Also I wanted to try out this source code formatting tool for blogspot. The snippet below is a unit test written in the UnitTest++ framework.


#include <UnitTest++.h>
#include <boost/math/special_functions/modf.hpp>
using namespace boost::math;

SUITE(BOOST_MATH_MODF) {
const double pi = 3.141592653589793;
TEST(modf) {
double* pIntegerPart = new double;
CHECK_CLOSE(0.141592653589793, modf( pi, pIntegerPart ), 1e-15);
CHECK_CLOSE(3.0, *pIntegerPart, 1e-15);
delete pIntegerPart;
}
}

No comments: