Skip to content

Commit

Permalink
Used stdlib math functions instead of global C versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz committed Mar 22, 2018
1 parent 4abbb7b commit 410398a
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/ai/contexts.cpp
Expand Up @@ -1119,7 +1119,7 @@ double readonly_context_impl::power_projection(const map_location& loc, const mo
}

// The 0.5 power avoids underestimating too much the damage of a wounded unit.
int64_t hp = int(sqrt(double(un.hitpoints()) / un.max_hitpoints()) * 1000);
int64_t hp = int(std::sqrt(double(un.hitpoints()) / un.max_hitpoints()) * 1000);
int64_t most_damage = 0;
for(const attack_type &att : un.attacks())
{
Expand Down
2 changes: 1 addition & 1 deletion src/ai/default/recruitment.cpp
Expand Up @@ -975,7 +975,7 @@ void recruitment::do_combat_analysis(std::vector<data>* leader_data) {
for (const std::string& recruit : leader.recruits) {
double score = compare_unit_types(recruit, enemy_unit);
score *= enemy_unit_hp;
score = pow(score, COMBAT_SCORE_POWER);
score = std::pow(score, COMBAT_SCORE_POWER);
temp_scores[recruit] += score;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/display.cpp
Expand Up @@ -1302,7 +1302,7 @@ void display::scroll_to_xy(int screenxpos, int screenypos, SCROLL_TYPE scroll_ty
int x_old = 0;
int y_old = 0;

const double dist_total = hypot(xmove, ymove);
const double dist_total = std::hypot(xmove, ymove);
double dist_moved = 0.0;

int t_prev = SDL_GetTicks();
Expand Down
28 changes: 14 additions & 14 deletions src/formula/function.cpp
Expand Up @@ -491,21 +491,21 @@ DEFINE_WFL_FUNCTION(str_lower, 1, 1)
DEFINE_WFL_FUNCTION(sin, 1, 1)
{
const double angle = args()[0]->evaluate(variables, fdb).as_decimal() / 1000.0;
const double result = sin(angle * pi<double>() / 180.0);
const double result = std::sin(angle * pi<double>() / 180.0);
return variant(result, variant::DECIMAL_VARIANT);
}

DEFINE_WFL_FUNCTION(cos, 1, 1)
{
const double angle = args()[0]->evaluate(variables, fdb).as_decimal() / 1000.0;
const double result = cos(angle * pi<double>() / 180.0);
const double result = std::cos(angle * pi<double>() / 180.0);
return variant(result, variant::DECIMAL_VARIANT);
}

DEFINE_WFL_FUNCTION(tan, 1, 1)
{
const double angle = args()[0]->evaluate(variables, fdb).as_decimal() / 1000.0;
const double result = tan(angle * pi<double>() / 180.0);
const double result = std::tan(angle * pi<double>() / 180.0);
if(std::isnan(result) || result <= INT_MIN || result >= INT_MAX) {
return variant();
}
Expand All @@ -516,7 +516,7 @@ DEFINE_WFL_FUNCTION(tan, 1, 1)
DEFINE_WFL_FUNCTION(asin, 1, 1)
{
const double num = args()[0]->evaluate(variables, fdb).as_decimal() / 1000.0;
const double result = asin(num) * 180.0 / pi<double>();
const double result = std::asin(num) * 180.0 / pi<double>();
if(std::isnan(result)) {
return variant();
}
Expand All @@ -527,7 +527,7 @@ DEFINE_WFL_FUNCTION(asin, 1, 1)
DEFINE_WFL_FUNCTION(acos, 1, 1)
{
const double num = args()[0]->evaluate(variables, fdb).as_decimal() / 1000.0;
const double result = acos(num) * 180.0 / pi<double>();
const double result = std::acos(num) * 180.0 / pi<double>();
if(std::isnan(result)) {
return variant();
}
Expand All @@ -538,14 +538,14 @@ DEFINE_WFL_FUNCTION(acos, 1, 1)
DEFINE_WFL_FUNCTION(atan, 1, 1)
{
const double num = args()[0]->evaluate(variables, fdb).as_decimal() / 1000.0;
const double result = atan(num) * 180.0 / pi<double>();
const double result = std::atan(num) * 180.0 / pi<double>();
return variant(result, variant::DECIMAL_VARIANT);
}

DEFINE_WFL_FUNCTION(sqrt, 1, 1)
{
const double num = args()[0]->evaluate(variables, fdb).as_decimal() / 1000.0;
const double result = sqrt(num);
const double result = std::sqrt(num);
if(std::isnan(result)) {
return variant();
}
Expand All @@ -556,15 +556,15 @@ DEFINE_WFL_FUNCTION(sqrt, 1, 1)
DEFINE_WFL_FUNCTION(cbrt, 1, 1)
{
const double num = args()[0]->evaluate(variables, fdb).as_decimal() / 1000.0;
const double result = num < 0 ? -pow(-num, 1.0 / 3.0) : pow(num, 1.0 / 3.0);
const double result = num < 0 ? -std::pow(-num, 1.0 / 3.0) : std::pow(num, 1.0 / 3.0);
return variant(result, variant::DECIMAL_VARIANT);
}

DEFINE_WFL_FUNCTION(root, 2, 2)
{
const double base = args()[0]->evaluate(variables, fdb).as_decimal() / 1000.0;
const double root = args()[1]->evaluate(variables, fdb).as_decimal() / 1000.0;
const double result = base < 0 && fmod(root, 2) == 1 ? -pow(-base, 1.0 / root) : pow(base, 1.0 / root);
const double result = base < 0 && std::fmod(root, 2) == 1 ? -std::pow(-base, 1.0 / root) : std::pow(base, 1.0 / root);
if(std::isnan(result)) {
return variant();
}
Expand All @@ -576,7 +576,7 @@ DEFINE_WFL_FUNCTION(log, 1, 2)
{
const double num = args()[0]->evaluate(variables, fdb).as_decimal() / 1000.0;
if(args().size() == 1) {
const double result = log(num);
const double result = std::log(num);
if(std::isnan(result)) {
return variant();
}
Expand All @@ -585,7 +585,7 @@ DEFINE_WFL_FUNCTION(log, 1, 2)
}

const double base = args()[1]->evaluate(variables, fdb).as_decimal() / 1000.0;
const double result = log(num) / log(base);
const double result = std::log(num) / std::log(base);
if(std::isnan(result)) {
return variant();
}
Expand All @@ -596,7 +596,7 @@ DEFINE_WFL_FUNCTION(log, 1, 2)
DEFINE_WFL_FUNCTION(exp, 1, 1)
{
const double num = args()[0]->evaluate(variables, fdb).as_decimal() / 1000.0;
const double result = exp(num);
const double result = std::exp(num);
if(result == 0 || result >= INT_MAX) {
// These are range errors rather than NaNs,
// but I figure it's better than returning INT_MIN.
Expand All @@ -617,7 +617,7 @@ DEFINE_WFL_FUNCTION(hypot, 2, 2)
{
const double x = args()[0]->evaluate(variables, fdb).as_decimal() / 1000.0;
const double y = args()[1]->evaluate(variables, fdb).as_decimal() / 1000.0;
return variant(hypot(x, y), variant::DECIMAL_VARIANT);
return variant(std::hypot(x, y), variant::DECIMAL_VARIANT);
}

DEFINE_WFL_FUNCTION(index_of, 2, 2)
Expand Down Expand Up @@ -677,7 +677,7 @@ DEFINE_WFL_FUNCTION(wave, 1, 1)
{
const int value = args()[0]->evaluate(variables, fdb).as_int() % 1000;
const double angle = 2.0 * pi<double>() * (static_cast<double>(value) / 1000.0);
return variant(static_cast<int>(sin(angle) * 1000.0));
return variant(static_cast<int>(std::sin(angle) * 1000.0));
}

namespace
Expand Down
4 changes: 2 additions & 2 deletions src/formula/variant.cpp
Expand Up @@ -469,7 +469,7 @@ variant variant::operator^(const variant& v) const
{
if(is_decimal() || v.is_decimal()) {

double res = pow(as_decimal() / 1000.0 , v.as_decimal() / 1000.0);
double res = std::pow(as_decimal() / 1000.0 , v.as_decimal() / 1000.0);

if(std::isnan(res)) {
return variant();
Expand All @@ -478,7 +478,7 @@ variant variant::operator^(const variant& v) const
return variant(res, DECIMAL_VARIANT);
}

return variant(static_cast<int>(round_portable(pow(static_cast<double>(as_int()), v.as_int()))));
return variant(static_cast<int>(round_portable(std::pow(static_cast<double>(as_int()), v.as_int()))));
}

variant variant::operator-() const
Expand Down
2 changes: 1 addition & 1 deletion src/gui/widgets/generator.cpp
Expand Up @@ -552,7 +552,7 @@ point table::calculate_best_size() const
*/

std::size_t n_items = get_item_count();
std::size_t max_cols = sqrt(n_items) + 2;
std::size_t max_cols = std::sqrt(n_items) + 2;

std::vector<point> item_sizes;
for(std::size_t i = 0; i < n_items; i++) {
Expand Down
10 changes: 5 additions & 5 deletions src/joystick.cpp
Expand Up @@ -133,7 +133,7 @@ std::pair<double, double> joystick_manager::get_mouse_axis_pair() {
thrust = get_thrusta_axis();
}

const int radius = round_double(sqrt(pow(values.first, 2.0f) + pow(values.second, 2.0f)));
const int radius = round_double(std::sqrt(std::pow(values.first, 2.0f) + std::pow(values.second, 2.0f)));
const int deadzone = preferences::joystick_mouse_deadzone();
const double multiplier = 1.0 + thrust;

Expand Down Expand Up @@ -169,7 +169,7 @@ std::pair<double, double> joystick_manager::get_scroll_axis_pair() {
thrust = get_thrusta_axis();
}

const int radius = round_double(sqrt(pow(values.first, 2.0f) + pow(values.second, 2.0f)));
const int radius = round_double(std::sqrt(std::pow(values.first, 2.0f) + std::pow(values.second, 2.0f)));
const int deadzone = preferences::joystick_scroll_deadzone();
const double multiplier = 1.0 + thrust;

Expand Down Expand Up @@ -218,7 +218,7 @@ std::pair<double, double> joystick_manager::get_cursor_polar_coordinates() {
std::pair<double, double> joystick_manager::get_polar_coordinates(int joystick_xaxis, int xaxis, int joystick_yaxis, int yaxis) {

const std::pair<int, int> values = get_axis_pair(joystick_xaxis, xaxis, joystick_yaxis, yaxis);
const double radius = (sqrt(pow(values.first, 2.0f) + pow(values.second, 2.0f))) / 32768.0;
const double radius = (std::sqrt(std::pow(values.first, 2.0f) + std::pow(values.second, 2.0f))) / 32768.0;
const double angle = (atan2(
static_cast<double>(values.second)
, static_cast<double>(values.first))) * 180.0 / pi<double>();
Expand Down Expand Up @@ -275,7 +275,7 @@ bool joystick_manager::update_highlighted_hex(map_location& highlighted_hex, con
const int x_axis = values.first;
const int y_axis = values.second;

//const int radius = round_double(sqrt(pow(x_axis, 2.0f) + pow(y_axis, 2.0f)));
//const int radius = round_double(std::(std::pow(x_axis, 2.0f) + std::pow(y_axis, 2.0f)));

// const int deadzone = preferences::joystick_cursor_deadzone();
//const int threshold2 = 10*threshold;
Expand Down Expand Up @@ -310,7 +310,7 @@ bool joystick_manager::update_highlighted_hex(map_location& highlighted_hex) {
const int x_axis = values.first;
const int y_axis = values.second;

const int radius = round_double(sqrt(pow(x_axis, 2.0f) + pow(y_axis, 2.0f)));
const int radius = round_double(std::sqrt(std::pow(x_axis, 2.0f) + std::pow(y_axis, 2.0f)));

const int deadzone = preferences::joystick_cursor_deadzone();
const int threshold = deadzone + preferences::joystick_cursor_threshold();
Expand Down
4 changes: 2 additions & 2 deletions src/sdl/utils.cpp
Expand Up @@ -1855,8 +1855,8 @@ surface rotate_any_surface(const surface& surf, float angle, int zoom, int offse
float max_x, max_y;
// convert angle to radiant (angle * 2 * PI) / 360
const float radians = angle * boost::math::constants::pi<float>() / 180;
cosine = cos(radians);
sine = sin(radians);
cosine = std::cos(radians);
sine = std::sin(radians);
// calculate the size of the dst image
src_w = surf->w * zoom;
src_h = surf->h * zoom;
Expand Down
12 changes: 6 additions & 6 deletions src/tests/test_formula_function.cpp
Expand Up @@ -161,14 +161,14 @@ BOOST_AUTO_TEST_CASE(test_formula_function_math)

BOOST_CHECK_EQUAL(formula("log(8,2)").evaluate().as_int(), 3);
BOOST_CHECK_EQUAL(formula("log(12)").evaluate().as_decimal(),
static_cast<int>(round(1000.0 * log(12))));
static_cast<int>(round(1000.0 * std::log(12))));
BOOST_CHECK_EQUAL(formula("exp(3)").evaluate().as_decimal(),
static_cast<int>(round(1000.0 * exp(3))));
static_cast<int>(round(1000.0 * std::exp(3))));
}

BOOST_AUTO_TEST_CASE(test_formula_function_trig)
{
const double pi = 4. * atan(1.);
const double pi = 4. * std::atan(1.);

map_formula_callable variables;

Expand All @@ -178,12 +178,12 @@ BOOST_AUTO_TEST_CASE(test_formula_function_trig)
BOOST_CHECK_EQUAL(
formula("sin(x)")
.evaluate(variables).as_decimal()
, static_cast<int>(round(1000. * sin(x * pi / 180.))));
, static_cast<int>(round(1000. * std::sin(x * pi / 180.))));

BOOST_CHECK_EQUAL(
formula("cos(x)")
.evaluate(variables).as_decimal()
, static_cast<int>(round(1000. * cos(x * pi / 180.))));
, static_cast<int>(round(1000. * std::cos(x * pi / 180.))));

if(x % 90 == 0 && x % 180 != 0) {
BOOST_CHECK(
Expand All @@ -193,7 +193,7 @@ BOOST_AUTO_TEST_CASE(test_formula_function_trig)
BOOST_CHECK_EQUAL(
formula("tan(x)")
.evaluate(variables).as_decimal(),
static_cast<int>(round(1000. * tan(x * pi / 180.))));
static_cast<int>(round(1000. * std::tan(x * pi / 180.))));
}
}
}
Expand Down

0 comments on commit 410398a

Please sign in to comment.