I have a question which is related to the question (and answers) available over here:
C++ Boost Interval and cos
The solution presented in the linked above works for me for most trigonometrical functions including the hyperbolic once. However, if I try to use the invers of this hyperbolic once, let's take asinh() as an example I get the following compiler error:
error C2784: "boost::numeric::interval<T,Policies>
boost::numeric::asinh(const boost::numeric::interval<T,Policies> &)":
could not deduce template argument for "const
boost::numeric::interval<T,Policies> &" from "const double"
A code that produces the error is
#include "stdafx.h"
#include <boost/numeric/interval.hpp>
#include <boost/numeric/interval/rounded_arith.hpp>
using namespace std;
using namespace boost::numeric::interval_lib;
using namespace boost::numeric;
typedef interval<double, policies<save_state<rounded_transc_std<double> >,
checking_base<double> > > Interval;
int _tmain(int argc, _TCHAR* argv[])
{
Interval test = asinh(Interval(1, 1.1));
return 0;
}
I am using the boost 1_65_1 libary. How to get the invers of the hyperbolic functions running?
Of cause one workaround would be to use the identity
Interval test = log(Interval(1, 1.1) + sqrt(pow(Interval(1, 1.1),2)+1.));
This works perfectly fine and produces correct results. However, it must be possible to use the implemented asinh-function directly.
There might be a MSVC-related bug, because it works for me using GCC or clang:
Live On Coliru (GCC c++03)
Live On Coliru (Clang c++03)
#include <boost/numeric/interval.hpp>
#include <boost/numeric/interval/io.hpp>
#include <boost/numeric/interval/rounded_arith.hpp>
#include <iostream>
namespace bn = boost::numeric;
namespace bni = bn::interval_lib;
typedef bn::interval<double, bni::policies<bni::save_state<bni::rounded_transc_std<double> >,
bni::checking_base<double> > > Interval;
int main()
{
Interval test = asinh(Interval(1, 1.1));
std::cout << test;
}
Prints
[0.881374,0.950347]
Related
Today I used boost-units for the first time and I was really satisfied with the outcome.
Unfortunately, I'm not completely satisfied with the display of the operator<< for a given unit.
The following code prints m s^-2 on the console:
#include <boost/units/systems/si/acceleration.hpp>
#include <boost/units/io.hpp>
#include <iostream>
int main(int argc, char**args) {
using namespace boost::units;
using namespace boost::units::si;
std::cout << acceleration() << std::endl;
return 0;
}
I'm wondering if there is an easy way to change the output to something like m/(s^2) or even m/s^2. I think, that this kind of representation might be easier to read.
I was trying using Boost Phoenix. My aim is to have stl algorithms that take a container instead of range of iterators, as described
here.
However, I am getting a mess of errors on a rather simple code:
#include <boost/phoenix/stl/algorithm.hpp>
#include <iostream>
#include <vector>
#include <algorithm>
namespace phx = boost::phoenix;
int main(int argc, char ** argv) {
std::vector<int> my_arr{ 0, 1, 2, 3 };
phx::for_each(my_arr, [](int i){std::cout << i << std::endl; });
}
I tried it on two platforms (Win7, Ubuntu) where I otherwise use multiple parts of Boost.
The error messages are rather long so I put them in files:
MVC++ November 2013, Boost 1.55
G++4.7.2, Boost 1.53
I can't really make much of the errors apart from templates not being compiled correctly, but I guess I am missing something rather simple.
I think you are using the wrong boost library. The phoenix algorithms are lazy functions which are explained here. So phoenix::for_each does not do anything if it is called, but returns a function object which iterates over the range once it is called. If you simply want to use the STL (and other) algorithms on ranges you can use the boost.range library:
#include <boost/range/algorithm/for_each.hpp>
#include <iostream>
#include <vector>
namespace rng = boost::range;
int main(int argc, char ** argv) {
std::vector<int> my_arr{ 0, 1, 2, 3 };
rng::for_each(my_arr, [](int i){std::cout << i << std::endl; });
}
You simply need to include the phoenix core before including anything else.
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/stl/algorithm.hpp>
... rest of your program
C++ returns invalid value in the following code:
#include <iostream>
#include <vector>
using namespace std;
int f(){
vector< int * > v[2];
return 1;
v[1].push_back(NULL);
}
int main(){
cout << f();
}
The output is:
205960
When I commnet line after return, it works fine:
#include <iostream>
#include <vector>
using namespace std;
int f(){
vector< int * > v[2];
return 1;
//v[1].push_back(NULL);
}
int main(){
cout << f();
}
The output is:
1
I am using code::blocks with mingw32-g++.exe compiler. The mingw version is: gcc version 4.4.1 (TDM-2 mingw32).
Your compiler has a bug. Fortunately, it is also obsolete. You should upgrade — G++ is up to version 4.6.2, which also implements much of C++11, which is very useful.
If you choose to stick with an older compiler, that is also a decision to accept its flaws.
Edit: If you are really stuck with 4.4 (for example due to a PHB), that series is still maintained. You can upgrade to GCC 4.4.6, released just this past April.
I am trying to compile the following code:
#include <iostream>
#include <iterator>
#include <vector>
#include <boost/assign/std/vector.hpp>
#include <boost/optional.hpp>
#include <boost/range/adaptor/indirected.hpp>
#include <boost/range/algorithm/copy.hpp>
int main( int argc, char ** argv )
{
using namespace boost::assign;
using boost::adaptors::indirected;
std::vector<boost::optional<unsigned> > values;
values += 1u,2u,3u;
boost::copy( values | indirected, std::ostream_iterator<unsigned>( std::cout, " " ) );
std::cout << std::endl;
}
However, I got some errors, e.g. that there is no type named element_type in boost::optional<unsigned>. The reference page page, however, says that the single precondition is the existence of the operator*() unary function. Is there a way to make it work?
This is definitely a bug in Boost, but whether that bug is in Boost.Optional or Boost.Iterator is up for debate (I would say the latter, personally).
However, the fix is trivial -- before including any Boost headers, do this:
#include <boost/optional/optional_fwd.hpp>
#include <boost/pointee.hpp>
namespace boost
{
template<typename P>
struct pointee<optional<P> >
{
typedef typename optional<P>::value_type type;
};
}
Then include other Boost headers as necessary.
Please submit a ticket on the Boost Trac, or at the least post a bug report on the Boost Users mailing list.
Look at the private optional.hpp defined in boost iostreams library here. You will see that it defines a typedef T element_type;
However the actual optional.hpp that you are using defined here does not define it. So that is why the compiler is complaining. I don't know why it was overlooked.
Try using the private optional.hpp from iostreams library to solve this issue. I hope this helps.
Like this question already asked, I'd like to initialize a container using STL where the elements are hard-coded in the cleanest manner possible. In this case, the elements are a doubly nested container:
set<vector<int> > A;
And I'd like (for example) to put the following values in:
A = [[0,0,1],[0,1,0],[1,0,0],[0,0,0]];
C++0x fine, using g++ 4.4.1. STL is preferable as I don't use Boost for any other parts of the code (though I wouldn't mind an example with it!).
This does use g++ 4.4.1, with -std=c++0x
#include <set>
#include <vector>
using namespace std;
int main()
{
set<vector<int>> A = {{0,0,1},{0,1,0},{1,0,0},{0,0,0}};
}
#include <boost/assign/list_of.hpp>
#include <vector>
#include <set>
using namespace std;
using namespace boost::assign;
int main()
{
set<vector<int> > A;
A = list_of
(list_of(0)(0)(1))
(list_of(0)(1)(0))
(list_of(1)(0)(0));
(list_of(0)(0)(0));
return 0;
}