Perform multiplication in result type for types larger than int

Makes the code do what is commonly expected and avoids needless overflow since
otherwise the multiplication is done as int.
This commit is contained in:
Gunter Labes 2024-07-07 18:34:45 +02:00 committed by Steve Cotton
parent e7c09868b7
commit e7acc0e3b9
8 changed files with 16 additions and 16 deletions

View file

@ -696,7 +696,7 @@ double recruitment::get_average_defense(const std::string& u_type) const {
const t_translation::terrain_code& terrain = entry.first;
int count = entry.second;
int defense = 100 - u_info->movement_type().defense_modifier(terrain);
summed_defense += defense * count;
summed_defense += static_cast<long>(defense) * count;
total_terrains += count;
}
double average_defense = (total_terrains == 0) ? 0.0 :

View file

@ -205,7 +205,7 @@ namespace {
if (search_counter == 0) search_counter = 2;
static std::vector<node> nodes;
nodes.resize(map.w() * map.h());
nodes.resize(static_cast<size_t>(map.w()) * map.h());
indexer index(map.w(), map.h());
comp node_comp(nodes);
@ -290,7 +290,7 @@ DEFINE_FAI_FUNCTION(calculate_map_ownership, 2, 5)
std::vector< std::vector<int>> scores( number_of_teams );
for( std::size_t i = 0; i< number_of_teams; ++i)
scores[i].resize(w*h);
scores[i].resize(static_cast<size_t>(w)*h);
/* // TODO: Do we need this?
for(unit_map::const_iterator i = resources::gameboard->units().begin(); i != resources::gameboard->units().end(); ++i) {

View file

@ -885,7 +885,7 @@ std::string default_map_generator_job::default_generate_map(generator_data data,
* more interesting types than the default.
*/
const height_map temperature_map = generate_height_map(data.width,data.height,
cfg["temperature_iterations"].to_int() * data.width * data.height / default_dimensions,
static_cast<size_t>(cfg["temperature_iterations"].to_int()) * data.width * data.height / default_dimensions,
cfg["temperature_size"], 0, 0);
LOG_NG << "Generated temperature map. " << (SDL_GetTicks() - ticks) << " ticks elapsed";

View file

@ -724,7 +724,7 @@ void grid::set_rows_cols(const unsigned rows, const unsigned cols)
cols_ = cols;
row_grow_factor_.resize(rows);
col_grow_factor_.resize(cols);
children_.resize(rows_ * cols_);
children_.resize(static_cast<size_t>(rows_) * cols_);
}
point grid::child::get_best_size() const

View file

@ -55,8 +55,8 @@ std::function<rect(rect)> prep_minimap_for_rendering(
DBG_DP << "Creating minimap: " << static_cast<int>(map.w() * scale * 0.75) << ", " << map.h() * scale;
const std::size_t map_width = std::max(0, map.w()) * scale * 3 / 4;
const std::size_t map_height = std::max(0, map.h()) * scale;
const std::size_t map_width = static_cast<size_t>(std::max(0, map.w())) * scale * 3 / 4;
const std::size_t map_height = static_cast<size_t>(std::max(0, map.h())) * scale;
// No map!
if(map_width == 0 || map_height == 0) {

View file

@ -300,7 +300,7 @@ static void find_routes(
search_counter = 1;
}
// Initialize the nodes for this search.
nodes.resize(map.w() * map.h());
nodes.resize(static_cast<size_t>(map.w()) * map.h());
findroute_comp node_comp(nodes);
findroute_indexer index(map.w(), map.h());
@ -893,7 +893,7 @@ full_cost_map::full_cost_map(const unit& u, bool force_ignore_zoc,
viewing_team_(viewing_team), see_all_(see_all), ignore_units_(ignore_units)
{
const gamemap& map = resources::gameboard->map();
cost_map = std::vector<std::pair<int, int>>(map.w() * map.h(), std::pair(-1, 0));
cost_map = std::vector<std::pair<int, int>>(static_cast<size_t>(map.w()) * map.h(), std::pair(-1, 0));
add_unit(u);
}
@ -908,7 +908,7 @@ full_cost_map::full_cost_map(bool force_ignore_zoc,
viewing_team_(viewing_team), see_all_(see_all), ignore_units_(ignore_units)
{
const gamemap& map = resources::gameboard->map();
cost_map = std::vector<std::pair<int, int>>(map.w() * map.h(), std::pair(-1, 0));
cost_map = std::vector<std::pair<int, int>>(static_cast<size_t>(map.w()) * map.h(), std::pair(-1, 0));
}
/**

View file

@ -212,7 +212,7 @@ void terrain_builder::tilemap::reload(int x, int y)
{
x_ = x;
y_ = y;
std::vector<terrain_builder::tile> new_tiles((x + 4) * (y + 4));
std::vector<terrain_builder::tile> new_tiles(static_cast<size_t>(x + 4) * (y + 4));
tiles_.swap(new_tiles);
reset();
}

View file

@ -81,19 +81,19 @@ namespace t_translation {
ter_map(const ter_map&) = default;
ter_map(ter_map&&) = default;
ter_map(int w, int h, terrain_code fill = terrain_code()) : data(w * h, fill), w(w), h(h) {}
ter_map(int w, int h, terrain_code fill = terrain_code()) : data(static_cast<size_t>(w) * h, fill), w(w), h(h) {}
ter_map & operator= (const ter_map &) = default;
ter_map & operator= (ter_map &&) = default;
terrain_code& get(int x, int y) { std::size_t index = x * h + y; return data.at(index); }
const terrain_code& get(int x, int y) const { std::size_t index = x * h + y; return data.at(index); }
terrain_code& get(int x, int y) { std::size_t index = static_cast<size_t>(x) * h + y; return data.at(index); }
const terrain_code& get(int x, int y) const { std::size_t index = static_cast<size_t>(x) * h + y; return data.at(index); }
std::vector<terrain_code> data;
int w;
int h;
std::vector<terrain_code>::iterator operator[](int x) { return data.begin() + h * x; }
std::vector<terrain_code>::const_iterator operator[](int x) const { return data.begin() + h * x; }
std::vector<terrain_code>::iterator operator[](int x) { return data.begin() + static_cast<size_t>(h) * x; }
std::vector<terrain_code>::const_iterator operator[](int x) const { return data.begin() + static_cast<size_t>(h) * x; }
};
/**