Skip to content

Commit

Permalink
minor: Fix floating point literals (#1873)
Browse files Browse the repository at this point in the history
* floating point literals must use the f suffix when working with float variables
* when a double is expected, do not force to use float computation by using a float literal
  • Loading branch information
obones authored Jul 13, 2024
1 parent 7e4cef2 commit ce56acc
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 44 deletions.
4 changes: 2 additions & 2 deletions src/devices/acurite.c
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ static int acurite_5n1_decode(r_device *decoder, bitbuffer_t *bitbuffer, uint8_t
int wind_speed_raw = ((bb[3] & 0x1F) << 3)| ((bb[4] & 0x70) >> 4);
float wind_speed_kph = 0;
if (wind_speed_raw > 0) {
wind_speed_kph = wind_speed_raw * 0.8278 + 1.0;
wind_speed_kph = wind_speed_raw * 0.8278f + 1.0f;
}

if (message_type == ACURITE_MSGTYPE_5N1_WINDSPEED_WINDDIR_RAINFALL) {
Expand Down Expand Up @@ -826,7 +826,7 @@ static int acurite_atlas_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsig
if ((bb[4] & 0x30) != 0)
exception++;

float tempf = (temp_raw - 400) * 0.1;
float tempf = (temp_raw - 400) * 0.1f;
if (tempf < -40.0 || tempf > 158.0) {
decoder_logf(decoder, 1, __func__, "Atlas 0x%04X Ch %s, invalid temperature: %0.1f F",
sensor_id, channel_str, tempf);
Expand Down
2 changes: 1 addition & 1 deletion src/devices/blueline.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ static int blueline_decode(r_device *decoder, bitbuffer_t *bitbuffer)
const uint8_t temperature = offset_payload_u8[1];
const uint8_t flags = offset_payload_u8[0] >> 2;
const uint8_t battery = (flags & 0x20) >> 5;
const float temperature_C = (0.436 * temperature) - 30.36;
const float temperature_C = (0.436f * temperature) - 30.36f;
/* clang-format off */
data = data_make(
"model", "", DATA_STRING, "Blueline-PowerCost",
Expand Down
2 changes: 1 addition & 1 deletion src/devices/bresser_5in1.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ static int bresser_5in1_decode(r_device *decoder, bitbuffer_t *bitbuffer)
/* check if the message is from a Bresser Professional Rain Gauge */
if ((sensor_type >= 0x39) && (sensor_type <= 0x3b)) {
// rescale the rain sensor readings
rain = rain * 2.5;
rain = rain * 2.5f;
/* clang-format off */
data = data_make(
"model", "", DATA_STRING, "Bresser-ProRainGauge",
Expand Down
4 changes: 2 additions & 2 deletions src/devices/ced7000.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ static int ced7000_decode(r_device *decoder, bitbuffer_t *bitbuffer)
/* Read the values */
int id = (b[1] & 0xF) * 1000 + (b[1] >> 4) * 100 + (b[0] & 0xF) * 10 + (b[0] >> 4);
int count = (b[2] & 0xF) * 10 + (b[2] >> 4);
float final = (b[5] >> 4) * 100 + (b[4] & 0xF) * 10 + (b[4] >> 4) + (b[3] & 0xF) * 0.1 + (b[3] >> 4) * 0.01;
float split = (b[7] & 0xF) * 100 + (b[7] >> 4) * 10 + (b[6] & 0xF) + (b[6] >> 4) * 0.1 + (b[5] & 0xF) * 0.01;
float final = (b[5] >> 4) * 100 + (b[4] & 0xF) * 10 + (b[4] >> 4) + (b[3] & 0xF) * 0.1 + (b[3] >> 4) * 0.01f;
float split = (b[7] & 0xF) * 100 + (b[7] >> 4) * 10 + (b[6] & 0xF) + (b[6] >> 4) * 0.1 + (b[5] & 0xF) * 0.01f;

/* clang-format off */
data_t *data = data_make(
Expand Down
4 changes: 2 additions & 2 deletions src/devices/danfoss.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ static int danfoss_cfr_callback(r_device *decoder, bitbuffer_t *bitbuffer)
default: str_sw = "ERROR";
}

float temp_meas = (float)bytes[5] + (float)bytes[4] / 256.0;
float temp_setp = (float)bytes[7] + (float)bytes[6] / 256.0;
float temp_meas = (float)bytes[5] + (float)bytes[4] / 256.0f;
float temp_setp = (float)bytes[7] + (float)bytes[6] / 256.0f;

/* clang-format off */
data = data_make(
Expand Down
2 changes: 1 addition & 1 deletion src/devices/efergy_optical.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ static int efergy_optical_callback(r_device *decoder, bitbuffer_t *bitbuffer)
// - red led (every 30s): bytes[3]=64 (0100 0000)
// - orange led (every 60s): bytes[3]=80 (0101 0000)
// - green led (every 90s): bytes[3]=96 (0110 0000)
float seconds = (((bytes[3] & 0x30) >> 4) + 1) * 30.0;
float seconds = (((bytes[3] & 0x30) >> 4) + 1) * 30.0f;

int pulsecount = bytes[8];

Expand Down
2 changes: 1 addition & 1 deletion src/devices/emontx.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ static int emontx_callback(r_device *decoder, bitbuffer_t *bitbuffer)
if (crc != words[13])
continue; // DECODE_FAIL_MIC

vrms = (float)words[4] / 100.0;
vrms = (float)words[4] / 100.0f;

/* clang-format off */
data = data_make(
Expand Down
2 changes: 1 addition & 1 deletion src/devices/emos_e6016.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static int emos_e6016_decode(r_device *decoder, bitbuffer_t *bitbuffer)
int temp_raw = (int16_t)(((b[8] & 0x0f) << 12) | (b[9] << 4)); // use sign extend
float temp_c = (temp_raw >> 4) * 0.1f;
int humidity = b[10];
float speed_ms = b[11] * 0.295;
float speed_ms = b[11] * 0.295f;
int dir_raw = (((b[12] & 0xf0) >> 4));
float dir_deg = dir_raw * 22.5f;

Expand Down
6 changes: 3 additions & 3 deletions src/devices/lacrosse_tx31u.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,16 @@ static int lacrosse_tx31u_decode(r_device *decoder, bitbuffer_t *bitbuffer)
} break;
case WIND_AVG: {
if ( !no_ext_sensor ) {
float wind_dir = nib1 * 22.5 ; // compass direction in degrees
float wind_avg = ((nib2<<4) + nib3) * 0.1f * 3.6; // wind values are decimal m/sec, convert to km/hr
float wind_dir = nib1 * 22.5f ; // compass direction in degrees
float wind_avg = ((nib2<<4) + nib3) * 0.1f * 3.6f; // wind values are decimal m/sec, convert to km/h
data = data_dbl(data, "wind_dir_deg", "Wind direction", "%.1f", wind_dir);
data = data_dbl(data, "wind_avg_km_h", "Wind speed", "%.1f km/h", wind_avg);
}
} break;
case WIND_MAX: {
int wind_input_lost = CHECK_BIT(nib1, 0); // a sensor was attached, but now not detected
if ( !no_ext_sensor && !wind_input_lost ) {
float wind_max = ((nib2<<4) + nib3) * 0.1f * 3.6; // wind values are decimal m/sec, convert to km/hr
float wind_max = ((nib2<<4) + nib3) * 0.1f * 3.6f; // wind values are decimal m/sec, convert to km/h
data = data_dbl(data, "wind_max_km_h", "Wind gust", "%.1f km/h", wind_max);
}
} break;
Expand Down
8 changes: 4 additions & 4 deletions src/devices/oregon_scientific.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ static unsigned cm180i_power(uint8_t const *msg, unsigned int offset)
val = (msg[4+offset*2] << 8) | (msg[3+offset*2] & 0xF0);
// tested across situations varying from 700 watt to more than 8000 watt to
// get same value as showed in physical CM180 panel (exactly equals to 1+1/160)
val *= 1.00625;
val *= 1.00625f;
return val;
}

Expand Down Expand Up @@ -130,7 +130,7 @@ static unsigned cm180_power(uint8_t const *msg)
val = (msg[4] << 8) | (msg[3] & 0xF0);
// tested across situations varying from 700 watt to more than 8000 watt to
// get same value as showed in physical CM180 panel (exactly equals to 1+1/160)
val *= 1.00625;
val *= 1.00625f;
return val;
}

Expand Down Expand Up @@ -841,7 +841,7 @@ static int oregon_scientific_v3_decode(r_device *decoder, bitbuffer_t *bitbuffer

unsigned ipower = cm180_power(msg);
uint64_t itotal = cm180_total(msg);
float total_energy = itotal / 3600.0 / 1000.0;
float total_energy = itotal / 3600.0f / 1000.0f;
if (valid == 0) {
/* clang-format off */
data = data_make(
Expand Down Expand Up @@ -878,7 +878,7 @@ static int oregon_scientific_v3_decode(r_device *decoder, bitbuffer_t *bitbuffer
if (msg_len >= 140) itotal= cm180i_total(msg);

// Convert `itotal` which is in Ws (or J) to kWh unit.
float total_energy = itotal / 3600.0 / 1000.0;
float total_energy = itotal / 3600.0f / 1000.0f;

if (valid == 0) {
/* clang-format off */
Expand Down
2 changes: 1 addition & 1 deletion src/devices/tpms_eezrv.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ static int tpms_eezrv_decode(r_device *decoder, bitbuffer_t *bitbuffer)
int infl_detected = (flags1 & 0x20) >> 5; // inflating - reports every 15 - 20 sec

int fast_leak = fast_leak_detected && !infl_detected;
float pressure_kPa = (((flags2 & 0x01) << 8) + b[3]) * 2.5;
float pressure_kPa = (((flags2 & 0x01) << 8) + b[3]) * 2.5f;

// Low batt = 0x8000;
int low_batt = flags1 >> 7; // Low batt flag is MSB (activated at V < 3.15 V)(Device fails at V < 3.10 V)
Expand Down
4 changes: 2 additions & 2 deletions src/devices/tpms_kia.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ static int tpms_kia_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsigned r
char raw[9 * 2 + 1]; // 9 bytes in hex notation
snprintf(raw, sizeof(raw), "%02x%02x%02x%02x%02x%02x%02x%02x%02x", b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8]);

float pressure_float = pressure / 5.0;
float temperature_float = temperature - 50.0;
float pressure_float = pressure / 5.0f;
float temperature_float = temperature - 50.0f;

/* clang-format off */
data_t *data = data_make(
Expand Down
2 changes: 1 addition & 1 deletion src/pulse_detect.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pulse_detect_t *pulse_detect_create(void)
return NULL;
}

pulse_detect_set_levels(pulse_detect, 0, 0.0, -12.1442, 9.0, 0);
pulse_detect_set_levels(pulse_detect, 0, 0.0f, -12.1442f, 9.0f, 0);

return pulse_detect;
}
Expand Down
32 changes: 16 additions & 16 deletions src/pulse_slicer.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static int account_event(r_device *device, bitbuffer_t *bits, char const *demod_

int pulse_slicer_pcm(pulse_data_t const *pulses, r_device *device)
{
float samples_per_us = pulses->sample_rate / 1.0e6;
float samples_per_us = pulses->sample_rate / 1.0e6f;
int s_short = device->short_width * samples_per_us;
int s_long = device->long_width * samples_per_us;
int s_reset = device->reset_limit * samples_per_us;
Expand All @@ -84,8 +84,8 @@ int pulse_slicer_pcm(pulse_data_t const *pulses, r_device *device)
}

// precision reciprocals
float f_short = device->short_width > 0.0 ? 1.0 / (device->short_width * samples_per_us) : 0;
float f_long = device->long_width > 0.0 ? 1.0 / (device->long_width * samples_per_us) : 0;
float f_short = device->short_width > 0.0f ? 1.0f / (device->short_width * samples_per_us) : 0;
float f_long = device->long_width > 0.0f ? 1.0f / (device->long_width * samples_per_us) : 0;

int events = 0;
bitbuffer_t bits = {0};
Expand Down Expand Up @@ -120,7 +120,7 @@ int pulse_slicer_pcm(pulse_data_t const *pulses, r_device *device)
min_count = count;
preamble_len = count;
if (device->verbose > 1) {
float to_us = 1e6 / pulses->sample_rate;
float to_us = 1e6f / pulses->sample_rate;
print_logf(LOG_INFO, __func__, "Exact bit width (in us) is %.2f vs %.2f (pulse width %.2f vs %.2f), %d bit preamble",
to_us / f_long, to_us * s_long,
to_us / f_short, to_us * s_short, count);
Expand Down Expand Up @@ -169,7 +169,7 @@ int pulse_slicer_pcm(pulse_data_t const *pulses, r_device *device)
min_count = count;
preamble_len = count;
if (device->verbose > 1) {
float to_us = 1e6 / pulses->sample_rate;
float to_us = 1e6f / pulses->sample_rate;
print_logf(LOG_INFO, __func__, "Exact bit width (in us) is %.2f vs %.2f, %d bit preamble",
to_us / f_short, to_us * s_short, count);
}
Expand Down Expand Up @@ -212,10 +212,10 @@ int pulse_slicer_pcm(pulse_data_t const *pulses, r_device *device)

for (unsigned n = 0; n < pulses->num_pulses; ++n) {
// Determine number of high bit periods for NRZ coding, where bits may not be separated
int highs = (pulses->pulse[n]) * f_short + 0.5;
int highs = (pulses->pulse[n]) * f_short + 0.5f;
// Determine number of low bit periods in current gap length (rounded)
// for RZ subtract the nominal bit-gap
int lows = (pulses->gap[n] + s_short - s_long) * f_long + 0.5;
int lows = (pulses->gap[n] + s_short - s_long) * f_long + 0.5f;

// Add run of ones (1 for RZ, many for NRZ)
for (int i = 0; i < highs; ++i) {
Expand Down Expand Up @@ -258,7 +258,7 @@ int pulse_slicer_pcm(pulse_data_t const *pulses, r_device *device)

int pulse_slicer_ppm(pulse_data_t const *pulses, r_device *device)
{
float samples_per_us = pulses->sample_rate / 1.0e6;
float samples_per_us = pulses->sample_rate / 1.0e6f;

int s_short = device->short_width * samples_per_us;
int s_long = device->long_width * samples_per_us;
Expand Down Expand Up @@ -337,7 +337,7 @@ int pulse_slicer_ppm(pulse_data_t const *pulses, r_device *device)

int pulse_slicer_pwm(pulse_data_t const *pulses, r_device *device)
{
float samples_per_us = pulses->sample_rate / 1.0e6;
float samples_per_us = pulses->sample_rate / 1.0e6f;

int s_short = device->short_width * samples_per_us;
int s_long = device->long_width * samples_per_us;
Expand Down Expand Up @@ -450,7 +450,7 @@ int pulse_slicer_pwm(pulse_data_t const *pulses, r_device *device)

int pulse_slicer_manchester_zerobit(pulse_data_t const *pulses, r_device *device)
{
float samples_per_us = pulses->sample_rate / 1.0e6;
float samples_per_us = pulses->sample_rate / 1.0e6f;

int s_short = device->short_width * samples_per_us;
int s_long = device->long_width * samples_per_us;
Expand Down Expand Up @@ -537,7 +537,7 @@ static inline int pulse_slicer_get_symbol(pulse_data_t const *pulses, unsigned i

int pulse_slicer_dmc(pulse_data_t const *pulses, r_device *device)
{
float samples_per_us = pulses->sample_rate / 1.0e6;
float samples_per_us = pulses->sample_rate / 1.0e6f;

int s_short = device->short_width * samples_per_us;
int s_long = device->long_width * samples_per_us;
Expand Down Expand Up @@ -597,7 +597,7 @@ int pulse_slicer_dmc(pulse_data_t const *pulses, r_device *device)

int pulse_slicer_piwm_raw(pulse_data_t const *pulses, r_device *device)
{
float samples_per_us = pulses->sample_rate / 1.0e6;
float samples_per_us = pulses->sample_rate / 1.0e6f;

int s_short = device->short_width * samples_per_us;
int s_long = device->long_width * samples_per_us;
Expand All @@ -618,7 +618,7 @@ int pulse_slicer_piwm_raw(pulse_data_t const *pulses, r_device *device)
}

// precision reciprocal
float f_short = device->short_width > 0.0 ? 1.0 / (device->short_width * samples_per_us) : 0;
float f_short = device->short_width > 0.0f ? 1.0f / (device->short_width * samples_per_us) : 0;

int w;

Expand Down Expand Up @@ -659,7 +659,7 @@ int pulse_slicer_piwm_raw(pulse_data_t const *pulses, r_device *device)

int pulse_slicer_piwm_dc(pulse_data_t const *pulses, r_device *device)
{
float samples_per_us = pulses->sample_rate / 1.0e6;
float samples_per_us = pulses->sample_rate / 1.0e6f;

int s_short = device->short_width * samples_per_us;
int s_long = device->long_width * samples_per_us;
Expand Down Expand Up @@ -715,7 +715,7 @@ int pulse_slicer_piwm_dc(pulse_data_t const *pulses, r_device *device)

int pulse_slicer_nrzs(pulse_data_t const *pulses, r_device *device)
{
float samples_per_us = pulses->sample_rate / 1.0e6;
float samples_per_us = pulses->sample_rate / 1.0e6f;

int s_short = device->short_width * samples_per_us;
int s_long = device->long_width * samples_per_us;
Expand Down Expand Up @@ -775,7 +775,7 @@ int pulse_slicer_nrzs(pulse_data_t const *pulses, r_device *device)

int pulse_slicer_osv1(pulse_data_t const *pulses, r_device *device)
{
float samples_per_us = pulses->sample_rate / 1.0e6;
float samples_per_us = pulses->sample_rate / 1.0e6f;

int s_short = device->short_width * samples_per_us;
int s_long = device->long_width * samples_per_us;
Expand Down
12 changes: 6 additions & 6 deletions src/r_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ void r_init_cfg(r_cfg_t *cfg)
if (!cfg->demod)
FATAL_CALLOC("r_init_cfg()");

cfg->demod->level_limit = 0.0;
cfg->demod->min_level = -12.1442;
cfg->demod->min_snr = 9.0;
cfg->demod->level_limit = 0.0f;
cfg->demod->min_level = -12.1442f;
cfg->demod->min_snr = 9.0f;
// Pulse detect will only print LOG_NOTICE and lower.
cfg->demod->detect_verbosity = LOG_WARNING;

Expand Down Expand Up @@ -339,8 +339,8 @@ void calc_rssi_snr(r_cfg_t *cfg, pulse_data_t *pulse_data)
float ook_high_estimate = pulse_data->ook_high_estimate > 0 ? pulse_data->ook_high_estimate : 1;
float ook_low_estimate = pulse_data->ook_low_estimate > 0 ? pulse_data->ook_low_estimate : 1;
float asnr = ook_high_estimate / ook_low_estimate;
float foffs1 = (float)pulse_data->fsk_f1_est / INT16_MAX * cfg->samp_rate / 2.0;
float foffs2 = (float)pulse_data->fsk_f2_est / INT16_MAX * cfg->samp_rate / 2.0;
float foffs1 = (float)pulse_data->fsk_f1_est / INT16_MAX * cfg->samp_rate / 2.0f;
float foffs2 = (float)pulse_data->fsk_f2_est / INT16_MAX * cfg->samp_rate / 2.0f;
pulse_data->freq1_hz = (foffs1 + cfg->center_frequency);
pulse_data->freq2_hz = (foffs2 + cfg->center_frequency);
pulse_data->centerfreq_hz = cfg->center_frequency;
Expand All @@ -366,7 +366,7 @@ void calc_rssi_snr(r_cfg_t *cfg, pulse_data_t *pulse_data)
char *time_pos_str(r_cfg_t *cfg, unsigned samples_ago, char *buf)
{
if (cfg->report_time == REPORT_TIME_SAMPLES) {
double s_per_sample = 1.0 / cfg->samp_rate;
double s_per_sample = 1.0f / cfg->samp_rate;
return sample_pos_str(cfg->demod->sample_file_pos - samples_ago * s_per_sample, buf);
}
else {
Expand Down

0 comments on commit ce56acc

Please sign in to comment.