Jan 29, 2022 at 1:27pm UTC
Line 8: a has random value and so does x
Line 18. Shouldn't the value go into N ?
Jan 29, 2022 at 2:29pm UTC
People are stuck on printf, and so now C++ also has its own: format.
The stream manipulators look stupid, but are actually a good idea. And pretty darn easy to use:
std::cout << x << (x * (3*x+3) - 1) << "\n" ;
Jan 29, 2022 at 7:42pm UTC
I should clarify: if you wish to have formatting controls on the output, that makes things a little long-winded and clunky-ish. But to get the
%5.3f
output you need to use some manipulators found in <iomanip>.
std::cout << std::fixed << std::setw(5) << std::setprecision(3) << my_float_var;
This is one of the main reasons people like the old printf-style stuff: it is really short to write.
I would personally just use an overloaded manipulator function instead and avoid the overhead of parsing a string. It gets a little long-winded, though:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include <iomanip>
#include <iostream>
namespace alanfromeurope
{
struct fixed
{
int w, p;
};
}
std::ostream & operator << ( std::ostream & outs, const alanfromeurope::fixed & fixed )
{
return outs << std::fixed << std::setw( fixed.w ) << std::setprecision( fixed.p );
}
alanfromeurope::fixed fixed( int width, int precision )
{
return { width, precision };
}
//// And now that that is out of the way, using it == short and pretty! ////
int main()
{
std::cout << fixed(5,3) << 3.14159265 << "\n" ;
}
That is kind of how C++ works: bury the messy stuff in a library somewhere so that things can be short and pretty when you use them.
Last edited on Jan 29, 2022 at 7:43pm UTC
Jan 29, 2022 at 11:02pm UTC
As an aside, I ran into an math expression evaluator library:
https://github.com/ArashPartow/exprtk
I've hacked example-1 to reimplement @alanfromeurope's program above. It needs exprtk.hpp from the project.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include <exprtk/exprtk.hpp>
#include <cstdio>
#include <iostream>
#include <string>
#include <tuple>
template <typename T>
void evaluate(std::tuple<T, T, T> range, const std::string& expression_string)
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
T x;
symbol_table_t symbol_table;
symbol_table.add_variable("x" , x);
symbol_table.add_constants();
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t parser;
parser.compile(expression_string, expression);
for (x = std::get<0>(range); x <= std::get<1>(range); x += std::get<2>(range))
{
const T y = expression.value();
printf("%19.15f\t%19.15f\n" , x, y);
}
}
int main()
{
// almost any expression here - see project readme
const std::string expression_string = "x*(3*x+3) - 1" ;
double min{}, max{}, step{};
std::cout << "min: " ; std::cin >> min;
std::cout << "max: " ; std::cin >> max;
std::cout << "step: " ; std::cin >> step;
evaluate<double >({min, max, step}, expression_string);
return 0;
}
Last edited on Jan 29, 2022 at 11:12pm UTC