Skip to content

Commit

Permalink
Use namespaced math functions (#39553)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevingranade authored Apr 14, 2020
1 parent 66dad5a commit 7867cd8
Show file tree
Hide file tree
Showing 77 changed files with 319 additions and 306 deletions.
8 changes: 4 additions & 4 deletions src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1037,8 +1037,8 @@ static void butchery_drops_harvest( item *corpse_item, const mtype &mt, player &
// apply skill before converting to items, but only if mass_ratio is defined
roll *= roll_drops();
monster_weight_remaining -= roll;
roll = ceil( static_cast<double>( roll ) /
to_gram( item::find_type( entry.drop )->weight ) );
roll = std::ceil( static_cast<double>( roll ) /
to_gram( item::find_type( entry.drop )->weight ) );
} else {
monster_weight_remaining -= roll * to_gram( ( item::find_type( entry.drop ) )->weight );
}
Expand Down Expand Up @@ -3665,7 +3665,7 @@ void activity_handlers::build_do_turn( player_activity *act, player *p )
const double current_progress = old_counter * base_total_moves / 10000000.0 +
delta_progress;
// Current progress as a percent of base_total_moves to 2 decimal places
pc->counter = round( current_progress / base_total_moves * 10000000.0 );
pc->counter = std::round( current_progress / base_total_moves * 10000000.0 );

p->set_moves( 0 );

Expand Down Expand Up @@ -3787,7 +3787,7 @@ void activity_handlers::craft_do_turn( player_activity *act, player *p )
const double current_progress = craft->item_counter * base_total_moves / 10'000'000.0 +
delta_progress;
// Current progress as a percent of base_total_moves to 2 decimal places
craft->item_counter = round( current_progress / base_total_moves * 10'000'000.0 );
craft->item_counter = std::round( current_progress / base_total_moves * 10'000'000.0 );
p->set_moves( 0 );

// This is to ensure we don't over count skill steps
Expand Down
6 changes: 3 additions & 3 deletions src/activity_item_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,7 @@ static activity_reason_info can_do_activity_there( const activity_id &act, playe
return activity_reason_info::fail( NO_ZONE );
}
// if the vehicle is moving or player is controlling it.
if( abs( veh->velocity ) > 100 || veh->player_in_control( g->u ) ) {
if( std::abs( veh->velocity ) > 100 || veh->player_in_control( g->u ) ) {
return activity_reason_info::fail( NO_ZONE );
}
for( const npc &guy : g->all_npcs() ) {
Expand Down Expand Up @@ -1323,8 +1323,8 @@ static activity_reason_info can_do_activity_there( const activity_id &act, playe
continue;
}
const int max_lift = p.best_nearby_lifting_assist( src_loc );
const int lvl = ceil( units::quantity<double, units::mass::unit_type>( base.weight() ) /
TOOL_LIFT_FACTOR );
const int lvl = std::ceil( units::quantity<double, units::mass::unit_type>( base.weight() ) /
TOOL_LIFT_FACTOR );
const bool use_aid = max_lift >= lvl;
const bool use_str = p.can_lift( base );
if( !( use_aid || use_str ) ) {
Expand Down
4 changes: 2 additions & 2 deletions src/avatar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1702,9 +1702,9 @@ std::string points_left::to_string()
_( "Points left: <color_%s>%d</color>%c<color_%s>%d</color>%c<color_%s>%d</color>=<color_%s>%d</color>" ),
stat_points_left() >= 0 ? "light_gray" : "red", stat_points,
trait_points >= 0 ? '+' : '-',
trait_points_left() >= 0 ? "light_gray" : "red", abs( trait_points ),
trait_points_left() >= 0 ? "light_gray" : "red", std::abs( trait_points ),
skill_points >= 0 ? '+' : '-',
skill_points_left() >= 0 ? "light_gray" : "red", abs( skill_points ),
skill_points_left() >= 0 ? "light_gray" : "red", std::abs( skill_points ),
is_valid() ? "light_gray" : "red", stat_points + trait_points + skill_points );
} else if( limit == ONE_POOL ) {
return string_format( _( "Points left: %4d" ), skill_points_left() );
Expand Down
4 changes: 2 additions & 2 deletions src/avatar_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ bool avatar_action::move( avatar &you, map &m, int dx, int dy, int dz )
veh_closed_door = dpart >= 0 && !veh1->parts[dpart].open;
}

if( veh0 != nullptr && abs( veh0->velocity ) > 100 ) {
if( veh0 != nullptr && std::abs( veh0->velocity ) > 100 ) {
if( veh1 == nullptr ) {
if( query_yn( _( "Dive from moving vehicle?" ) ) ) {
g->moving_vehicle_dismount( dest_loc );
Expand Down Expand Up @@ -619,7 +619,7 @@ void avatar_action::autoattack( avatar &you, map &m )
} );

const tripoint diff = best.pos() - you.pos();
if( abs( diff.x ) <= 1 && abs( diff.y ) <= 1 && diff.z == 0 ) {
if( std::abs( diff.x ) <= 1 && std::abs( diff.y ) <= 1 && diff.z == 0 ) {
move( you, m, tripoint( diff.xy(), 0 ) );
return;
}
Expand Down
22 changes: 12 additions & 10 deletions src/bionics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ bool Character::activate_bionic( int b, bool eff_only )
int vehwindspeed = 0;
if( optional_vpart_position vp = g->m.veh_at( pos() ) ) {
// vehicle velocity in mph
vehwindspeed = abs( vp->vehicle().velocity / 100 );
vehwindspeed = std::abs( vp->vehicle().velocity / 100 );
}
const oter_id &cur_om_ter = overmap_buffer.ter( global_omt_location() );
/* cache g->get_temperature( player location ) since it is used twice. No reason to recalc */
Expand Down Expand Up @@ -1095,7 +1095,7 @@ bool Character::burn_fuel( int b, bool start )
const optional_vpart_position vp = g->m.veh_at( pos() );
if( vp ) {
// vehicle velocity in mph
vehwindspeed = abs( vp->vehicle().velocity / 100 );
vehwindspeed = std::abs( vp->vehicle().velocity / 100 );
}
const double windpower = get_local_windpower( g->weather.windspeed + vehwindspeed,
overmap_buffer.ter( global_omt_location() ), pos(), g->weather.winddirection,
Expand Down Expand Up @@ -1175,7 +1175,7 @@ void Character::passive_power_gen( int b )
const optional_vpart_position vp = g->m.veh_at( pos() );
if( vp ) {
// vehicle velocity in mph
vehwindspeed = abs( vp->vehicle().velocity / 100 );
vehwindspeed = std::abs( vp->vehicle().velocity / 100 );
}
const double windpower = get_local_windpower( g->weather.windspeed + vehwindspeed,
overmap_buffer.ter( global_omt_location() ), pos(), g->weather.winddirection,
Expand Down Expand Up @@ -1553,13 +1553,14 @@ void Character::bionics_uninstall_failure( int difficulty, int success, float ad
{
// "success" should be passed in as a negative integer representing how far off we
// were for a successful removal. We use this to determine consequences for failing.
success = abs( success );
success = std::abs( success );

// failure level is decided by how far off the character was from a successful removal, and
// this is scaled up or down by the ratio of difficulty/skill. At high skill levels (or low
// difficulties), only minor consequences occur. At low skill levels, severe consequences
// are more likely.
const int failure_level = static_cast<int>( sqrt( success * 4.0 * difficulty / adjusted_skill ) );
const int failure_level = static_cast<int>( std::sqrt( success * 4.0 * difficulty /
adjusted_skill ) );
const int fail_type = std::min( 5, failure_level );

if( fail_type <= 0 ) {
Expand Down Expand Up @@ -1617,13 +1618,14 @@ void Character::bionics_uninstall_failure( monster &installer, player &patient,

// "success" should be passed in as a negative integer representing how far off we
// were for a successful removal. We use this to determine consequences for failing.
success = abs( success );
success = std::abs( success );

// failure level is decided by how far off the monster was from a successful removal, and
// this is scaled up or down by the ratio of difficulty/skill. At high skill levels (or low
// difficulties), only minor consequences occur. At low skill levels, severe consequences
// are more likely.
const int failure_level = static_cast<int>( sqrt( success * 4.0 * difficulty / adjusted_skill ) );
const int failure_level = static_cast<int>( std::sqrt( success * 4.0 * difficulty /
adjusted_skill ) );
const int fail_type = std::min( 5, failure_level );

bool u_see = sees( patient );
Expand Down Expand Up @@ -1780,7 +1782,7 @@ int bionic_manip_cos( float adjusted_skill, int bionic_difficulty )
// to reserve bionics for characters with the appropriate skill. For more difficult bionics, the
// curve flattens out just above 80%
chance_of_success = static_cast<int>( ( 100 * skill_difficulty_parameter ) /
( skill_difficulty_parameter + sqrt( 1 / skill_difficulty_parameter ) ) );
( skill_difficulty_parameter + std::sqrt( 1 / skill_difficulty_parameter ) ) );

return chance_of_success;
}
Expand Down Expand Up @@ -2258,13 +2260,13 @@ void Character::bionics_install_failure( const bionic_id &bid, const std::string
{
// "success" should be passed in as a negative integer representing how far off we
// were for a successful install. We use this to determine consequences for failing.
success = abs( success );
success = std::abs( success );

// failure level is decided by how far off the character was from a successful install, and
// this is scaled up or down by the ratio of difficulty/skill. At high skill levels (or low
// difficulties), only minor consequences occur. At low skill levels, severe consequences
// are more likely.
int failure_level = static_cast<int>( sqrt( success * 4.0 * difficulty / adjusted_skill ) );
int failure_level = static_cast<int>( std::sqrt( success * 4.0 * difficulty / adjusted_skill ) );
int fail_type = ( failure_level > 5 ? 5 : failure_level );
bool drop_cbm = false;
add_msg( m_neutral, _( "The installation is a failure." ) );
Expand Down
2 changes: 1 addition & 1 deletion src/calendar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ moon_phase get_moon_phase( const time_point &p )
const int num_middays = to_days<int>( p - calendar::turn_zero + 1_days / 2 );
const time_duration nearest_midnight = num_middays * 1_days;
const double phase_change = nearest_midnight / moon_phase_duration;
const int current_phase = static_cast<int>( round( phase_change * MOON_PHASE_MAX ) ) %
const int current_phase = static_cast<int>( std::round( phase_change * MOON_PHASE_MAX ) ) %
static_cast<int>( MOON_PHASE_MAX );
return static_cast<moon_phase>( current_phase );
}
Expand Down
8 changes: 4 additions & 4 deletions src/cata_tiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1441,11 +1441,11 @@ void cata_tiles::get_window_tile_counts( const int width, const int height, int
int &rows ) const
{
if( tile_iso ) {
columns = ceil( static_cast<double>( width ) / tile_width ) * 2 + 4;
rows = ceil( static_cast<double>( height ) / ( tile_width / 2.0 - 1 ) ) * 2 + 4;
columns = std::ceil( static_cast<double>( width ) / tile_width ) * 2 + 4;
rows = std::ceil( static_cast<double>( height ) / ( tile_width / 2.0 - 1 ) ) * 2 + 4;
} else {
columns = ceil( static_cast<double>( width ) / tile_width );
rows = ceil( static_cast<double>( height ) / tile_height );
columns = std::ceil( static_cast<double>( width ) / tile_width );
rows = std::ceil( static_cast<double>( height ) / tile_height );
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/cata_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ bool match_include_exclude( const std::string &text, std::string filter )

double logarithmic( double t )
{
return 1 / ( 1 + exp( -t ) );
return 1 / ( 1 + std::exp( -t ) );
}

double logarithmic_range( int min, int max, int pos )
Expand Down
40 changes: 20 additions & 20 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -685,11 +685,9 @@ int Character::sight_range( int light_level ) const
* log(LIGHT_AMBIENT_LOW / light_level) <= LIGHT_TRANSPARENCY_OPEN_AIR * distance
* log(LIGHT_AMBIENT_LOW / light_level) * (1 / LIGHT_TRANSPARENCY_OPEN_AIR) <= distance
*/
int range = static_cast<int>( -log( get_vision_threshold( static_cast<int>( g->m.ambient_light_at(
pos() ) ) ) /
static_cast<float>( light_level ) ) *
int range = static_cast<int>( -std::log( get_vision_threshold( static_cast<int>
( g->m.ambient_light_at( pos() ) ) ) / static_cast<float>( light_level ) ) *
( 1.0 / LIGHT_TRANSPARENCY_OPEN_AIR ) );
// int range = log(light_level * LIGHT_AMBIENT_LOW) / LIGHT_TRANSPARENCY_OPEN_AIR;

// Clamp to [1, sight_max].
return clamp( range, 1, sight_max );
Expand Down Expand Up @@ -1687,7 +1685,7 @@ void Character::recalc_sight_limits()
static float threshold_for_range( float range )
{
constexpr float epsilon = 0.01f;
return LIGHT_AMBIENT_MINIMAL / exp( range * LIGHT_TRANSPARENCY_OPEN_AIR ) - epsilon;
return LIGHT_AMBIENT_MINIMAL / std::exp( range * LIGHT_TRANSPARENCY_OPEN_AIR ) - epsilon;
}

float Character::get_vision_threshold( float light_level ) const
Expand Down Expand Up @@ -2616,8 +2614,8 @@ units::mass Character::weight_carried_with_tweaks( const item_tweaks &tweaks ) c
}
// Exclude wielded item if using lifting tool
if( weaponweight + ret > weight_capacity() ) {
const float liftrequirement = ceil( units::to_gram<float>( weaponweight ) / units::to_gram<float>
( TOOL_LIFT_FACTOR ) );
const float liftrequirement = std::ceil( units::to_gram<float>( weaponweight ) /
units::to_gram<float>( TOOL_LIFT_FACTOR ) );
if( g->new_game || best_nearby_lifting_assist() < liftrequirement ) {
ret += weaponweight;
}
Expand Down Expand Up @@ -4454,7 +4452,7 @@ void Character::update_health( int external_modifiers )
// For small differences, it changes 4 points per day
// For large ones, up to ~40% of the difference per day
int health_change = effective_healthy_mod - get_healthy() + external_modifiers;
mod_healthy( sgn( health_change ) * std::max( 1, abs( health_change ) / 10 ) );
mod_healthy( sgn( health_change ) * std::max( 1, std::abs( health_change ) / 10 ) );

// And healthy_mod decays over time.
// Slowly near 0, but it's hard to overpower it near +/-100
Expand Down Expand Up @@ -5133,7 +5131,7 @@ void Character::update_bodytemp()
int vehwindspeed = 0;
const optional_vpart_position vp = g->m.veh_at( pos() );
if( vp ) {
vehwindspeed = abs( vp->vehicle().velocity / 100 ); // vehicle velocity in mph
vehwindspeed = std::abs( vp->vehicle().velocity / 100 ); // vehicle velocity in mph
}
const oter_id &cur_om_ter = overmap_buffer.ter( global_omt_location() );
bool sheltered = g->is_sheltered( pos() );
Expand Down Expand Up @@ -5247,8 +5245,8 @@ void Character::update_bodytemp()
frostbite_timer[bp] -= std::max( 5, h_radiation );
}
// 111F (44C) is a temperature in which proteins break down: https://en.wikipedia.org/wiki/Burn
blister_count += h_radiation - 111 > 0 ? std::max( static_cast<int>( sqrt( h_radiation - 111 ) ),
0 ) : 0;
blister_count += h_radiation - 111 > 0 ?
std::max( static_cast<int>( std::sqrt( h_radiation - 111 ) ), 0 ) : 0;

const bool pyromania = has_trait( trait_PYROMANIA );
// BLISTERS : Skin gets blisters from intense heat exposure.
Expand Down Expand Up @@ -5414,7 +5412,8 @@ void Character::update_bodytemp()
rounding_error = 1;
}
if( temp_cur[bp] != temp_conv[bp] ) {
temp_cur[bp] = static_cast<int>( temp_difference * exp( -0.002 ) + temp_conv[bp] + rounding_error );
temp_cur[bp] = static_cast<int>( temp_difference * std::exp( -0.002 ) + temp_conv[bp] +
rounding_error );
}
// This statement checks if we should be wearing our bonus warmth.
// If, after all the warmth calculations, we should be, then we have to recalculate the temperature.
Expand All @@ -5428,7 +5427,8 @@ void Character::update_bodytemp()
}
if( temp_before != temp_conv[bp] ) {
temp_difference = temp_before - temp_conv[bp];
temp_cur[bp] = static_cast<int>( temp_difference * exp( -0.002 ) + temp_conv[bp] + rounding_error );
temp_cur[bp] = static_cast<int>( temp_difference * std::exp( -0.002 ) + temp_conv[bp] +
rounding_error );
}
}
int temp_after = temp_cur[bp];
Expand Down Expand Up @@ -6874,7 +6874,7 @@ std::string Character::get_weight_description() const

units::mass Character::bodyweight() const
{
return units::from_kilogram( get_bmi() * pow( height() / 100.0f, 2 ) );
return units::from_kilogram( get_bmi() * std::pow( height() / 100.0f, 2 ) );
}

units::mass Character::bionics_weight() const
Expand Down Expand Up @@ -6978,9 +6978,9 @@ int Character::get_bmr() const
Values are for males, and average!
*/
const int equation_constant = 5;
return ceil( metabolic_rate_base() * activity_level * ( units::to_gram<int>
( bodyweight() / 100.0 ) +
( 6.25 * height() ) - ( 5 * age() ) + equation_constant ) );
return std::ceil( metabolic_rate_base() * activity_level * ( units::to_gram<int>
( bodyweight() / 100.0 ) +
( 6.25 * height() ) - ( 5 * age() ) + equation_constant ) );
}

void Character::increase_activity_level( float new_level )
Expand Down Expand Up @@ -8734,7 +8734,7 @@ void Character::apply_persistent_morale()
}
// The penalty starts at 1 at min_time and scales up to max_unhappiness at max_time.
const float t = ( total_time - min_time ) / ( max_time - min_time );
const int pen = ceil( lerp_clamped( 0, max_unhappiness, t ) );
const int pen = std::ceil( lerp_clamped( 0, max_unhappiness, t ) );
if( pen > 0 ) {
add_morale( MORALE_PERM_NOMAD, -pen, -pen, 1_minutes, 1_minutes, true );
}
Expand Down Expand Up @@ -9297,7 +9297,7 @@ std::list<item> Character::use_charges( const itype_id &what, int qty,
qty -= std::min( qty, bio );
}

int adv = charges_of( "adv_UPS_off", static_cast<int>( ceil( qty * 0.6 ) ) );
int adv = charges_of( "adv_UPS_off", static_cast<int>( std::ceil( qty * 0.6 ) ) );
if( adv > 0 ) {
std::list<item> found = use_charges( "adv_UPS_off", adv );
res.splice( res.end(), found );
Expand Down Expand Up @@ -9503,7 +9503,7 @@ int Character::heartrate_bpm() const
//Based on get_max_healthy that already has bmi factored
const int healthy = get_max_healthy();
//a bit arbitary formula that can use some love
float healthy_modifier = -0.05f * round( healthy / 20.0f );
float healthy_modifier = -0.05f * std::round( healthy / 20.0f );
heartbeat += average_heartbeat * healthy_modifier;
//Pain simply adds 2% per point after it reaches 5 (that's arbitary)
const int cur_pain = get_perceived_pain();
Expand Down
2 changes: 1 addition & 1 deletion src/computer_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ void computer_session::action_amigara_log()
reset_terminal();
print_line( _( "SITE %d%d%d\n"
"PERTINENT FOREMAN LOGS WILL BE PREPENDED TO NOTES" ),
g->get_levx(), g->get_levy(), abs( g->get_levz() ) );
g->get_levx(), g->get_levy(), std::abs( g->get_levz() ) );
print_text( "%s", SNIPPET.random_from_category( "amigara4" ).value_or( translation() ) );
print_gibberish_line();
print_gibberish_line();
Expand Down
Loading

0 comments on commit 7867cd8

Please sign in to comment.