Replaced round_double and round_portable with std::round

std::round(double) uses the desired half-away-from-0 method.

(cherry-picked from commit 29fcbd053d)
This commit is contained in:
Charles Dang 2018-04-15 01:34:42 +11:00
parent b32ce37de5
commit 2a6d9454de
7 changed files with 24 additions and 39 deletions

View file

@ -744,7 +744,7 @@ void recruitment::update_average_lawful_bonus() {
++counter;
}
if (counter > 0) {
average_lawful_bonus_ = round_double(static_cast<double>(sum) / counter);
average_lawful_bonus_ = std::round(static_cast<double>(sum) / counter);
}
}
@ -1069,9 +1069,9 @@ struct attack_simulation {
attacker_type(attacker),
defender_type(defender),
attacker_stats(attacker, att_weapon, true, defender, def_weapon,
round_double(defender_defense), average_lawful_bonus),
std::round(defender_defense), average_lawful_bonus),
defender_stats(defender, def_weapon, false, attacker, att_weapon,
round_double(attacker_defense), average_lawful_bonus),
std::round(attacker_defense), average_lawful_bonus),
attacker_combatant(attacker_stats),
defender_combatant(defender_stats)
{
@ -1728,7 +1728,7 @@ void recruitment::update_scouts_wanted() {
// making us get twice as many scouts.
double villages_per_scout = (VILLAGE_PER_SCOUT_MULTIPLICATOR * get_villages_per_scout()) / 2;
scouts_wanted_ = (villages_per_scout > 0) ? round_double(our_share / villages_per_scout) : 0;
scouts_wanted_ = (villages_per_scout > 0) ? std::round(our_share / villages_per_scout) : 0;
if (scouts_wanted_ == 0) {
return;

View file

@ -239,8 +239,8 @@ bool controller_base::handle_scroll(int mousex, int mousey, int mouse_flags, dou
}
// scroll with joystick
dx += round_double(x_axis * scroll_speed);
dy += round_double(y_axis * scroll_speed);
dx += std::round(x_axis * scroll_speed);
dy += std::round(y_axis * scroll_speed);
return get_display().scroll(dx, dy);
}

View file

@ -1881,10 +1881,10 @@ void display::draw_minimap_units()
double u_h = yscaling;
SDL_Rect r {
minimap_location_.x + round_double(u_x)
, minimap_location_.y + round_double(u_y)
, round_double(u_w)
, round_double(u_h)
minimap_location_.x + int(std::round(u_x))
, minimap_location_.y + int(std::round(u_y))
, int(std::round(u_w))
, int(std::round(u_h))
};
sdl::fill_rectangle(r, col);
@ -2041,8 +2041,8 @@ bool display::set_zoom(unsigned int amount, const bool validate_value_and_set_in
//Turn the zoom factor to a double in order to avoid rounding errors.
double zoom_factor = double(new_zoom) / double(zoom_);
xpos_ = round_double(((xpos_ + area.w / 2) * zoom_factor) - (area.w / 2));
ypos_ = round_double(((ypos_ + area.h / 2) * zoom_factor) - (area.h / 2));
xpos_ = std::round(((xpos_ + area.w / 2) * zoom_factor) - (area.w / 2));
ypos_ = std::round(((ypos_ + area.h / 2) * zoom_factor) - (area.h / 2));
zoom_ = new_zoom;
bounds_check_position(xpos_, ypos_);
@ -2158,8 +2158,8 @@ void display::scroll_to_xy(int screenxpos, int screenypos, SCROLL_TYPE scroll_ty
dist_moved += velocity * dt;
if (dist_moved > dist_total) dist_moved = dist_total;
int x_new = round_double(xmove * dist_moved / dist_total);
int y_new = round_double(ymove * dist_moved / dist_total);
int x_new = std::round(xmove * dist_moved / dist_total);
int y_new = std::round(ymove * dist_moved / dist_total);
int dx = x_new - x_old;
int dy = y_new - y_old;
@ -2242,7 +2242,7 @@ void display::scroll_to_tiles(const std::vector<map_location>::const_iterator &
if (scroll_type == ONSCREEN || scroll_type == ONSCREEN_WARP) {
SDL_Rect r = map_area();
int spacing = round_double(add_spacing*hex_size());
int spacing = std::round(add_spacing * hex_size());
r.x += spacing;
r.y += spacing;
r.w -= 2*spacing;

View file

@ -478,7 +478,7 @@ variant variant::operator^(const variant& v) const
return variant(res, DECIMAL_VARIANT);
}
return variant(static_cast<int>(round_portable(std::pow(static_cast<double>(as_int()), v.as_int()))));
return variant(static_cast<int>(std::round(std::pow(static_cast<double>(as_int()), v.as_int()))));
}
variant variant::operator-() const

View file

@ -133,7 +133,7 @@ std::pair<double, double> joystick_manager::get_mouse_axis_pair() {
thrust = get_thrusta_axis();
}
const int radius = round_double(std::sqrt(std::pow(values.first, 2.0f) + std::pow(values.second, 2.0f)));
const int radius = std::round(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;
@ -142,7 +142,7 @@ std::pair<double, double> joystick_manager::get_mouse_axis_pair() {
// TODO do some math to normalize over the value - deadzone.
//const double relation = std::abs( (double)values.first / (double)values.second );
//const int range_x = values.first - round_double(relation * deadzone);
//const int range_x = values.first - std::round(relation * deadzone);
//const int range_y = values.second - ((1.0 - relation) * deadzone);
//double x_value = ((double)(values.first - deadzone) / (double)(32768 - deadzone)) *
@ -169,7 +169,7 @@ std::pair<double, double> joystick_manager::get_scroll_axis_pair() {
thrust = get_thrusta_axis();
}
const int radius = round_double(std::sqrt(std::pow(values.first, 2.0f) + std::pow(values.second, 2.0f)));
const int radius = std::round(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;
@ -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(std::(std::pow(x_axis, 2.0f) + std::pow(y_axis, 2.0f)));
//const int radius = std::round(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;
@ -285,7 +285,7 @@ bool joystick_manager::update_highlighted_hex(map_location& highlighted_hex, con
//const bool greater_threshold2 = radius > threshold2;
highlighted_hex = selected_hex;
highlighted_hex.add(round_double(x_axis / 3200), round_double(y_axis / 3200));
highlighted_hex.add(std::round(x_axis / 3200), std::round(y_axis / 3200));
//if (!greater_threshold) {
// counter_ = 0;
@ -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(std::sqrt(std::pow(x_axis, 2.0f) + std::pow(y_axis, 2.0f)));
const int radius = std::round(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();

View file

@ -441,8 +441,8 @@ stats& attack_context::defender_stats()
void attack_context::attack_expected_damage(double attacker_inflict_, double defender_inflict_)
{
int attacker_inflict = round_double(attacker_inflict_ * stats::decimal_shift);
int defender_inflict = round_double(defender_inflict_ * stats::decimal_shift);
int attacker_inflict = std::round(attacker_inflict_ * stats::decimal_shift);
int defender_inflict = std::round(defender_inflict_ * stats::decimal_shift);
stats &att_stats = attacker_stats(), &def_stats = defender_stats();
att_stats.expected_damage_inflicted += attacker_inflict;
att_stats.expected_damage_taken += defender_inflict;

View file

@ -24,7 +24,6 @@
#include <cstdint>
#include <cstdlib>
#include <limits>
#include <math.h> // cmath may not provide round()
#include <vector>
#include <algorithm>
@ -63,20 +62,6 @@ inline int round_damage(int base_damage, int bonus, int divisor) {
return std::max<int>(1, (base_damage * bonus + rounding) / divisor);
}
// not guaranteed to have exactly the same result on different platforms
inline int round_double(double d) {
#ifdef HAVE_ROUND
return static_cast<int>(round(d)); //surprisingly, not implemented everywhere
#else
return static_cast<int>((d >= 0.0)? std::floor(d + 0.5) : std::ceil(d - 0.5));
#endif
}
// Guaranteed to have portable results across different platforms
inline double round_portable(double d) {
return (d >= 0.0) ? std::floor(d + 0.5) : std::ceil(d - 0.5);
}
template<typename Cmp>
bool in_ranges(const Cmp c, const std::vector<std::pair<Cmp, Cmp>>& ranges)
{