Avoiding type confusion with the square (or nth root etc) of some quantity in modern C++ -
immediately after introduction, these recommendations how program in c++ start philosophical assertion 1 should "express ideas directly in code". sounded great idea until came this:
speed s; // there 1 single value std::vector<mass> m; // there may millions/billions of values computetotalkineticenergy();
if there lots of masses, , normal "half-em-vee-squared" laws of physics apply, makes sense square speed outside (implicit, necessary) loop on vector of masses. type of speed s
variable once squared? doesn't notion of expressing ideas "directly in code" lead problems moment want compute logarithm or nth root or similar? in case, 1 define (ugly) type of speedsquared
, general principle applies equally functions applied speed
std::chrono::high_resolution_clock::time_point
?
if care having strong types quantities, use boost.units, or write own units library.
otherwise, convert speed
, mass
values real numbers when kinetic energy calculation, , convert resulting real number energy
value, or something. it's not big deal you're not using "proper" type @ 1 tiny intermediate stage of calculation.
Comments
Post a Comment