core: add cast_int<> function

We had a pattern where doubles were converted to long with
lrint() and then down-cast to a narrower int type.

Because this is unwieldy, introduce a function encapsulating
this.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-09-03 09:52:12 +02:00 committed by bstoeger
parent 110e64bc66
commit 77b12bbccf
10 changed files with 34 additions and 23 deletions

View file

@ -182,7 +182,7 @@ volume_t cylinder_t::gas_volume(pressure_t p) const
{
double bar = p.mbar / 1000.0;
double z_factor = gas_compressibility_factor(gasmix, bar);
return volume_t { .mliter = static_cast<int>(lrint(type.size.mliter * bar_to_atm(bar) / z_factor)) };
return volume_t { .mliter = int_cast<int>(type.size.mliter * bar_to_atm(bar) / z_factor) };
}
int find_best_gasmix_match(struct gasmix mix, const struct cylinder_table &cylinders)
@ -348,7 +348,7 @@ void copy_cylinder_types(const struct dive *s, struct dive *d)
void fill_default_cylinder(const struct dive *dive, cylinder_t *cyl)
{
const std::string &cyl_name = prefs.default_cylinder;
pressure_t pO2 = {.mbar = static_cast<int>(lrint(prefs.modpO2 * 1000.0))};
pressure_t pO2 = {.mbar = int_cast<int>(prefs.modpO2 * 1000.0)};
if (cyl_name.empty())
return;