How do use the pow function with variables? i.e.
//equations for problem 9 page 99 part b
cT=(hT)/2;
iT=(base*(hT*hT*hT))/12;
LT=(sT*iT)/(8*cT); *math.h file in program
how would i raise "hT" to the 3 power in correct c++ syntax?
How do use the pow function with variables? i.e.
//equations for problem 9 page 99 part b
cT=(hT)/2;
iT=(base*(hT*hT*hT))/12;
LT=(sT*iT)/(8*cT); *math.h file in program
how would i raise "hT" to the 3 power in correct c++ syntax?
: is right that the standard way in C++ is to use pow, and 's warning about overloads matters: the standard pow routines are for floating‑point types, so integer arguments are promoted and the function returns a floating‑point result. See the reference for details: std::pow on cppreference.
For predictable behavior include <cmath> and call std::pow (or bring the overload in with using std::pow). If your values are double/float, pass a floating exponent (for clarity) and avoid unintended integer division (use 2.0 not 2 when you want a fractional result). Example:
#include <cmath>
double height = 4.2;
double base_val = 5.0;
double I = std::pow(height, 3.0) * base_val / 12.0;
If the base is integral and you need an exact integer cube, doing two multiplications (multiply the value by itself twice) is simpler, faster, and avoids floating‑point rounding. If you call std::pow with integer inputs and then store the result back into an integer, perform an explicit cast or rounding so you know how halves are handled. Finally, prefer <cmath> over the old C header and qualify pow with std:: for clarity.
Jump to Post— NathanOliver 429use the pow function.
foo = pow(bar, 3);
use the pow function.
foo = pow(bar, 3); See for a reference. Watch out for the overloads that are allowed (i.e., there is no overload for an integer to the power of integer, so you'll need to do appropriate casting).
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.