mirror of
https://github.com/esphome/esphome.git
synced 2026-09-15 09:08:41 +00:00
[multiple] Single-precision float math, avoid double promotion (batch 3/4) (#17255)
This commit is contained in:
@@ -6,9 +6,9 @@
|
||||
|
||||
namespace esphome::anova {
|
||||
|
||||
float ftoc(float f) { return (f - 32.0) * (5.0f / 9.0f); }
|
||||
float ftoc(float f) { return (f - 32.0f) * (5.0f / 9.0f); }
|
||||
|
||||
float ctof(float c) { return (c * 9.0f / 5.0f) + 32.0; }
|
||||
float ctof(float c) { return (c * 9.0f / 5.0f) + 32.0f; }
|
||||
|
||||
AnovaPacket *AnovaCodec::clean_packet_() {
|
||||
this->packet_.length = strlen((char *) this->packet_.data);
|
||||
|
||||
@@ -205,7 +205,7 @@ void BL0906::read_data_(const uint8_t address, const float reference, sensor::Se
|
||||
// Chip temperature
|
||||
if (reference == BL0906_TREF) {
|
||||
value = (float) to_int32_t(data_s24);
|
||||
value = (value - 64) * 12.5 / 59 - 40;
|
||||
value = (value - 64) * 12.5f / 59 - 40;
|
||||
}
|
||||
sensor->publish_state(value);
|
||||
}
|
||||
|
||||
@@ -204,7 +204,7 @@ void MedianCombinationComponent::handle_new_value(float value) {
|
||||
median = sensor_states[sensor_states_size / 2];
|
||||
} else {
|
||||
// Even number of measurements, use the average of the two middle measurements
|
||||
median = (sensor_states[sensor_states_size / 2] + sensor_states[sensor_states_size / 2 - 1]) / 2.0;
|
||||
median = (sensor_states[sensor_states_size / 2] + sensor_states[sensor_states_size / 2 - 1]) / 2.0f;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ class DemoSensor final : public sensor::Sensor, public PollingComponent {
|
||||
float base = std::isnan(this->state) ? 0.0f : this->state;
|
||||
this->publish_state(base + val * 10);
|
||||
} else {
|
||||
if (val < 0.1) {
|
||||
if (val < 0.1f) {
|
||||
this->publish_state(NAN);
|
||||
} else {
|
||||
this->publish_state(val * 100);
|
||||
|
||||
@@ -10,9 +10,9 @@ class DemoTextSensor final : public text_sensor::TextSensor, public PollingCompo
|
||||
public:
|
||||
void update() override {
|
||||
float val = random_float();
|
||||
if (val < 0.33) {
|
||||
if (val < 0.33f) {
|
||||
this->publish_state("foo");
|
||||
} else if (val < 0.66) {
|
||||
} else if (val < 0.66f) {
|
||||
this->publish_state("bar");
|
||||
} else {
|
||||
this->publish_state("foobar");
|
||||
|
||||
@@ -105,14 +105,14 @@ bool ES7243E::configure_mic_gain_() {
|
||||
|
||||
uint8_t ES7243E::es7243e_gain_reg_value_(float mic_gain) {
|
||||
// reg: 12 - 34.5dB, 13 - 36dB, 14 - 37.5dB
|
||||
mic_gain += 0.5;
|
||||
if (mic_gain <= 33.0) {
|
||||
mic_gain += 0.5f;
|
||||
if (mic_gain <= 33.0f) {
|
||||
return (uint8_t) mic_gain / 3;
|
||||
}
|
||||
if (mic_gain < 36.0) {
|
||||
if (mic_gain < 36.0f) {
|
||||
return 12;
|
||||
}
|
||||
if (mic_gain < 37.0) {
|
||||
if (mic_gain < 37.0f) {
|
||||
return 13;
|
||||
}
|
||||
return 14;
|
||||
|
||||
@@ -607,7 +607,7 @@ haier_protocol::HaierMessage HonClimate::get_control_message() {
|
||||
if (climate_control.target_temperature.has_value()) {
|
||||
float target_temp = climate_control.target_temperature.value();
|
||||
out_data->set_point = ((int) target_temp) - 16; // set the temperature with offset 16
|
||||
out_data->half_degree = (target_temp - ((int) target_temp) >= 0.49) ? 1 : 0;
|
||||
out_data->half_degree = (target_temp - ((int) target_temp) >= 0.49f) ? 1 : 0;
|
||||
}
|
||||
if (out_data->ac_power == 0) {
|
||||
// If AC is off - no presets allowed
|
||||
|
||||
@@ -119,7 +119,7 @@ void INA219Component::setup() {
|
||||
}
|
||||
|
||||
this->calibration_lsb_ = lsb;
|
||||
auto calibration = uint32_t(0.04096f / (0.000001 * lsb * this->shunt_resistance_ohm_));
|
||||
auto calibration = uint32_t(0.04096f / (0.000001f * lsb * this->shunt_resistance_ohm_));
|
||||
ESP_LOGV(TAG, " Using LSB=%" PRIu32 " calibration=%" PRIu32, lsb, calibration);
|
||||
if (!this->write_byte_16(INA219_REGISTER_CALIBRATION, calibration)) {
|
||||
this->mark_failed();
|
||||
|
||||
@@ -315,14 +315,14 @@ class LightColorValues {
|
||||
if (this->color_temperature_ <= 0) {
|
||||
return this->color_temperature_;
|
||||
}
|
||||
return 1000000.0 / this->color_temperature_;
|
||||
return 1000000.0f / this->color_temperature_;
|
||||
}
|
||||
/// Set the color temperature property of these light color values in kelvin.
|
||||
void set_color_temperature_kelvin(float color_temperature) {
|
||||
if (color_temperature <= 0) {
|
||||
return;
|
||||
}
|
||||
this->color_temperature_ = 1000000.0 / color_temperature;
|
||||
this->color_temperature_ = 1000000.0f / color_temperature;
|
||||
}
|
||||
|
||||
/// Get the cold white property of these light color values. In range 0.0 to 1.0.
|
||||
|
||||
@@ -500,12 +500,12 @@ void LTRAlsPs501Component::apply_lux_calculation_(AlsReadings &data) {
|
||||
// method from
|
||||
// https://github.com/fards/Ainol_fire_kernel/blob/83832cf8a3082fd8e963230f4b1984479d1f1a84/customer/drivers/lightsensor/ltr501als.c#L295
|
||||
|
||||
if (ratio < 0.45) {
|
||||
lux = 1.7743 * ch0 + 1.1059 * ch1;
|
||||
} else if (ratio < 0.64) {
|
||||
lux = 3.7725 * ch0 - 1.3363 * ch1;
|
||||
} else if (ratio < 0.85) {
|
||||
lux = 1.6903 * ch0 - 0.1693 * ch1;
|
||||
if (ratio < 0.45f) {
|
||||
lux = 1.7743f * ch0 + 1.1059f * ch1;
|
||||
} else if (ratio < 0.64f) {
|
||||
lux = 3.7725f * ch0 - 1.3363f * ch1;
|
||||
} else if (ratio < 0.85f) {
|
||||
lux = 1.6903f * ch0 - 0.1693f * ch1;
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Impossible ch1/(ch0 + ch1) ratio");
|
||||
lux = 0.0f;
|
||||
|
||||
@@ -23,7 +23,7 @@ void MAX17043Component::update() {
|
||||
if (!this->read_byte_16(MAX17043_VCELL, &raw_voltage)) {
|
||||
this->status_set_warning(LOG_STR("Unable to read MAX17043_VCELL"));
|
||||
} else {
|
||||
float voltage = (1.25 * (float) (raw_voltage >> 4)) / 1000.0;
|
||||
float voltage = (1.25f * (float) (raw_voltage >> 4)) / 1000.0f;
|
||||
this->voltage_sensor_->publish_state(voltage);
|
||||
this->status_clear_warning();
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ static const char *const TAG = "msa3xx";
|
||||
const uint8_t MSA_3XX_PART_ID = 0x13;
|
||||
|
||||
const float GRAVITY_EARTH = 9.80665f;
|
||||
const float LSB_COEFF = 1000.0f / (GRAVITY_EARTH * 3.9); // LSB to 1 LSB = 3.9mg = 0.0039g
|
||||
const float LSB_COEFF = 1000.0f / (GRAVITY_EARTH * 3.9f); // LSB to 1 LSB = 3.9mg = 0.0039g
|
||||
const float G_OFFSET_MIN = -4.5f; // -127...127 LSB = +- 0.4953g = +- 4.857 m/s^2 => +- 4.5 for the safe
|
||||
const float G_OFFSET_MAX = 4.5f; // -127...127 LSB = +- 0.4953g = +- 4.857 m/s^2 => +- 4.5 for the safe
|
||||
|
||||
|
||||
@@ -85,7 +85,7 @@ class OpenThreadSrpComponent final : public Component {
|
||||
public:
|
||||
void set_mdns(esphome::mdns::MDNSComponent *mdns);
|
||||
// This has to run after the mdns component or else no services are available to advertise
|
||||
float get_setup_priority() const override { return this->mdns_->get_setup_priority() - 1.0; }
|
||||
float get_setup_priority() const override { return this->mdns_->get_setup_priority() - 1.0f; }
|
||||
void setup() override;
|
||||
static void srp_callback(otError err, const otSrpClientHostInfo *host_info, const otSrpClientService *services,
|
||||
const otSrpClientService *removed_services, void *context);
|
||||
|
||||
@@ -224,7 +224,7 @@ bool SPA06Component::soft_reset_() {
|
||||
}
|
||||
|
||||
// Temperature conversion formula. See datasheet pg. 14
|
||||
float SPA06Component::convert_temperature_(const float &t_raw_sc) { return this->c0_ * 0.5 + this->c1_ * t_raw_sc; }
|
||||
float SPA06Component::convert_temperature_(const float &t_raw_sc) { return this->c0_ * 0.5f + this->c1_ * t_raw_sc; }
|
||||
// Pressure conversion formula. See datasheet pg. 14
|
||||
float SPA06Component::convert_pressure_(const float &p_raw_sc, const float &t_raw_sc) {
|
||||
float p2_raw_sc = p_raw_sc * p_raw_sc;
|
||||
|
||||
@@ -256,7 +256,7 @@ void TCS34725Component::update() {
|
||||
// increase only if not already maximum
|
||||
// do not use max gain, as ist will not get better
|
||||
if (this->gain_reg_ < 3) {
|
||||
if (((float) raw_c / 655.35 < 20.f) && (this->integration_time_ > 600.f)) {
|
||||
if (((float) raw_c / 655.35f < 20.f) && (this->integration_time_ > 600.f)) {
|
||||
gain_reg_val_new = this->gain_reg_ + 1;
|
||||
// update integration time to new situation
|
||||
integration_time_ideal = integration_time_ideal / 4;
|
||||
@@ -265,7 +265,7 @@ void TCS34725Component::update() {
|
||||
|
||||
// decrease gain, if very high clear values and integration times alreadey low
|
||||
if (this->gain_reg_ > 0) {
|
||||
if (70 < ((float) raw_c / 655.35) && (this->integration_time_ < 200)) {
|
||||
if (70 < ((float) raw_c / 655.35f) && (this->integration_time_ < 200)) {
|
||||
gain_reg_val_new = this->gain_reg_ - 1;
|
||||
// update integration time to new situation
|
||||
integration_time_ideal = integration_time_ideal * 4;
|
||||
|
||||
@@ -593,7 +593,7 @@ void ToshibaClimate::transmit_rac_pt1411hwru_() {
|
||||
message[3] = ~message[2];
|
||||
// Byte 4u: Temp
|
||||
if (this->model_ == MODEL_RAC_PT1411HWRU_F) {
|
||||
temperature = (temperature * 1.8) + 32;
|
||||
temperature = (temperature * 1.8f) + 32;
|
||||
temp_adjd = temperature - TOSHIBA_RAC_PT1411HWRU_TEMP_F_MIN;
|
||||
}
|
||||
|
||||
|
||||
@@ -70,19 +70,19 @@ float UFireISEComponent::measure_ph_(float temperature) {
|
||||
if (mv == -1)
|
||||
return -1;
|
||||
|
||||
ph = fabs(7.0 - (mv / PROBE_MV_TO_PH));
|
||||
ph = fabsf(7.0f - (mv / PROBE_MV_TO_PH));
|
||||
|
||||
// Determine the temperature correction
|
||||
float distance_from_7 = std::abs(7 - roundf(ph));
|
||||
float distance_from_25 = std::floor(std::abs(25 - roundf(temperature)) / 10);
|
||||
float temp_multiplier = (distance_from_25 * distance_from_7) * PROBE_TMP_CORRECTION;
|
||||
if ((ph >= 8.0) && (temperature >= 35))
|
||||
if ((ph >= 8.0f) && (temperature >= 35))
|
||||
temp_multiplier *= -1;
|
||||
if ((ph <= 6.0) && (temperature <= 15))
|
||||
if ((ph <= 6.0f) && (temperature <= 15))
|
||||
temp_multiplier *= -1;
|
||||
|
||||
ph += temp_multiplier;
|
||||
if ((ph <= 0.0) || (ph > 14.0))
|
||||
if ((ph <= 0.0f) || (ph > 14.0f))
|
||||
ph = -1;
|
||||
if (std::isinf(ph))
|
||||
ph = -1;
|
||||
|
||||
@@ -49,7 +49,7 @@ bool XiaomiLYWSD03MMC::parse_device(const esp32_ble_tracker::ESPBTDevice &device
|
||||
}
|
||||
if (res->humidity.has_value() && this->humidity_ != nullptr) {
|
||||
// see https://github.com/custom-components/sensor.mitemp_bt/issues/7#issuecomment-595948254
|
||||
*res->humidity = trunc(*res->humidity);
|
||||
*res->humidity = truncf(*res->humidity);
|
||||
}
|
||||
if (!(xiaomi_ble::report_xiaomi_results(res, addr_str))) {
|
||||
continue;
|
||||
|
||||
Reference in New Issue
Block a user