Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c39d84b

Browse files
chore: make acceleration due to gravity a constexpr (TheAlgorithms#2982)
* Added constexpr GRAVITY constant (9.80665) as per guidelines * Merge branch 'master' into feature/add-gravity-constant * Update physics/ground_to_ground_projectile_motion.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> * Merge branch 'master' into feature/add-gravity-constant
1 parent c917fc4 commit c39d84b

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

‎physics/ground_to_ground_projectile_motion.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
* @namespace physics
1919
* @brief Physics algorithms
2020
*/
21+
22+
// Define gravity as a constant within guidelines
23+
constexpr double GRAVITY = 9.80665; ///< Standard gravity (m/s^2)
24+
25+
2126
namespace physics {
2227
/**
2328
* @namespace ground_to_ground_projectile_motion
@@ -44,7 +49,7 @@ double degrees_to_radians(double degrees){
4449
* @returns The time that the projectile is in the air for
4550
*/
4651
template <typename T>
47-
T time_of_flight(T initial_velocity, T angle, double gravity = 9.81) {
52+
T time_of_flight(T initial_velocity, T angle, double gravity = GRAVITY) {
4853
double Viy = initial_velocity * (std::sin(degrees_to_radians(angle))); // calculate y component of the initial velocity
4954
return 2.0 * Viy / gravity;
5055
}
@@ -69,7 +74,7 @@ T horizontal_range(T initial_velocity, T angle, T time) {
6974
* @returns The max height that the projectile reaches
7075
*/
7176
template <typename T>
72-
T max_height(T initial_velocity, T angle, double gravity = 9.81) {
77+
T max_height(T initial_velocity, T angle, double gravity = GRAVITY) {
7378
double Viy = initial_velocity * (std::sin(degrees_to_radians(angle))); // calculate y component of the initial velocity
7479
return (std::pow(Viy, 2) / (2.0 * gravity));
7580
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /