Replaced round_double and round_portable with std::round

std::round(double) uses the desired half-away-from-0 method.
This commit is contained in:
Charles Dang 2018-04-15 01:34:42 +11:00
parent 606981bd22
commit 29fcbd053d
5 changed files with 12 additions and 27 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

@ -1064,8 +1064,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_);
@ -1189,8 +1189,8 @@ void display::scroll_to_xy(int screenxpos, int screenypos, SCROLL_TYPE scroll_ty
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;
@ -1292,7 +1292,7 @@ void display::scroll_to_tiles(const std::vector<map_location>::const_iterator& b
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

@ -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)
{