c++ - c++11: short notation for pow? -
i need paste huge formula c++11 code. formula contains numerous things double type powerfulness of integer:
it boring write:
pow(sin(b), 6) + ... pow(sin(c), 4) + ....
20 times
the best thing overload operator^ double , int, understand not possible c++11. , in formula this:
z0^2 * (something)
according precedence of operators this:
z0 ^ (2 * (something))
and not want.
so possible using trick approximate math notation x powerfulness of y c++ code?
possible toools are: c++11 , boost.
update:
about back upwards code such math notation instead of c++.
ideal solution see like:
const double result = (latex_mode (sin(b_0))^6(a + b) latex_mode_end) (b_0, a, b);
where latex_mode back upwards little subset of latex language without ambiguity. 1)all programmers touch code have mathematical background, read latex without problem 2)formula can copy/paste article without modification, cut down typo errors.
no, can't it. @ least, shouldn't. can simplified otherwise. if formula can described can create table of pairs (a,b) , write code it. example:
vector<pair<unsigned, unsigned>> table = {{1, 2}, {2, 3}, {3, 4}}; unsigned sum = 0; for(const auto& x : table) sum += pow(get<0>(x), get<1>(x));
inspired @5gon12eder's comment wrote function:
template <typename input, typename output = unsigned> output powers(std::initializer_list<input> args) { output result = 0; for(const auto x : args) result += pow(std::get<0>(x), std::get<1>(x)); homecoming result; }
you need additional (standard) library:
#tuple #initializer_listexample of use:
std::pair<unsigned, unsigned> a{1,2}, b{2,3}; std::cout << powers({a, b, {3, 4}, {4,5}}) << '\n';
that prints 1114
, it's correct.
referring edit part, can suggest write function receiving string , parsing. much slower above method. finally, can write authors of compiler.
c++ c++11
No comments:
Post a Comment