Showing posts with label boost. Show all posts
Showing posts with label boost. Show all posts

04 May 2012

Finding The Exponent In Scientific Notation - Harder Than It Seems

Was needing a method to find the exponent of a number (which means that value's scientific notation exponent.) I found simpler ways for several special cases but in general this seems to work for my needs.

The farther below UnitTest++ test code uses Table I.

Table I: Used to see what the exponent is,

Debug : value = 0.0001 = 1e-4 : log10 = -4
Debug : value = 0.0005 = 5e-4 : log10 = -3.30103
Debug : value = 0.001 = 1e-3 : log10 = -3
Debug : value = 0.005 = 5e-3 : log10 = -2.30103
Debug : value = 0.01 = 1e-2 : log10 = -2
Debug : value = 0.05 = 5e-2 : log10 = -1.30103
Debug : value = 0.1 = 1e-1 : log10 = -1
Debug : value = 0.5 = 5e-1 : log10 = -0.30103
Debug : value = 1 = 1e+0 : log10 = 0
Debug : value = 2 = 2e+0 : log10 = 0.30103
Debug : value = 10 = 1e+1 : log10 = 1
Debug : value = 20 = 2e+1 : log10 = 1.30103
Debug : value = 100 = 1e+2 : log10 = 2
Debug : value = 200 = 2e+2 : log10 = 2.30103
Debug : value = 1000 = 1e+3 : log10 = 3
Debug : value = 2000 = 2e+3 : log10 = 3.30103
Debug : value = 10000 = 1e+4 : log10 = 4

The function being tested is at lines 10-13. If uses the boost modf(). Note there is some C++11 in here.
1:  #include <UnitTest++.h> 
2: #include <vector>
3: #include <boost/math/special_functions/modf.hpp>
4: namespace bm = boost::math;
5:
6: SUITE(FIND_EXPONENT) {
7: typedef double type_t;
8: const type_t epsilon = 1e-15;
9:
10: type_t findExponent(type_t value){
11: type_t intPart;
12: return ( bm::modf<type_t>( log10(fabs(value)) , &intPart ) < 0 ) ? --intPart : intPart;
13: }
14: TEST(Function_findExponent_withTableI) {
15: std::vector<type_t> values =
16: { 1e-4,5e-4, 1e-3,5e-3, 1e-2,5e-2, 1e-1,5e-1, 1e+0,2e+0, 1e+1,2e+1, 1e+2,2e+2, 1e+3,2e+3, 1e+4,2e+4 };
17: std::vector<type_t> exponents =
18: { -4,-4, -3,-3, -2,-2, -1,-1, 0,0, +1,+1, +2,+2, +3,+3, +4,+4 };
19: CHECK_EQUAL(exponents.size(), values.size());
20:
21: for(unsigned i=0; i<values.size(); ++i){
22: values.at(i) = findExponent(values.at(i));
23: }
24: CHECK_ARRAY_EQUAL(exponents,values,values.size());
25: }
26: }
27:

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;
}
}

20 June 2009

Boost C++ Libraries: Changing the face of C++

We have been using Boost C++ libraries (Boost Wikipedia) in our Monte Carlo Application ToolKit (MCATK) software effort. Though we had heard of it for awhile, we had not investigated it.

Thanks to a Scott Meyer's talk at SDWest 2008, one of the team decided it was worth investigating and found there were several boost libraries that could be useful and help simplify our design.

Several Boost libraries have been accepted for incorporation into both the Technical Report 1 (TR1) and C++0x. Scott Meyer's list of the five most important C++ software includes Boost and Scott has a training course for TR1 and Boost.

Quotes from the Boost home page:

"...one of the most highly regarded and expertly designed C++ library projects in the world." — Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

"Item 55: Familiarize yourself with Boost." — Scott Meyers, Effective C++, 3rd Ed.

"The obvious solution for most programmers is to use a library that provides an elegant and efficient platform independent to needed services. Examples are BOOST..." — Bjarne Stroustrup, Abstraction, libraries, and efficiency in C++


A few of the boost libraries that MCATK make use of:

boost::function
boost::bind
boost::signal
boost::mpi
boost::serialization
boost::filesystem
boost::shared_pointer
boost::array


It has changed how we think about using C++ and development.