Why do I keep getting the same value of a variable for each iteration? c++ -
i new c++ , keep getting same value variable 'w'..
#include <iostream> #include <string> #define noexe 10 #define winit 0.9 #define wfinal 0.2 int main() { (int nexeno = 0; nexeno<noexe; nexeno++) { double w; w = (((noexe - nexeno)/noexe)*((noexe - nexeno)/noexe))*(winit - wfinal) + wfinal; std::cout << w << "\n"; } }
and output is
0.9 0.2 0.2 0.2 .......
(noexe - nexeno)/noexe
done entirely in integer arithmetic. since nexeno < noexe
entire previous expression zero.
thus left final term wfinal
.
to force floating point arithmetic, @ least 1 operand must of floating point type (in case double). clean up, , rid of nasty macros, define constants follows:
constexpr int noexe = 10; // use const instead of constexpr c++03 constexpr double noexe_d = noexe; constexpr double winit = 0.9; constexpr double wfinal = 0.2;
and use noexe_d
in calculation instead. i.e. (noexe_d - nexeno)/noexe_d)
.
Comments
Post a Comment