From 4d980ed58a0d18fc8a2eb715bf0200c5b66a0d88 Mon Sep 17 00:00:00 2001 From: mayor Date: Fri, 6 Feb 2026 10:02:46 -0600 Subject: [PATCH] Fix Dialyzer :exact_compare warnings for NaN/infinity checks Fixes 3 Dialyzer warnings about impossible comparisons when checking for NaN and infinity values in sensor validation. Problem: Dialyzer correctly identified that comparing a float with atoms (:nan, :infinity, :neg_infinity) using == can never evaluate to true. The previous code attempted to validate sensor values by comparing with these atoms, which is incorrect in Erlang/Elixir. Solution: Use proper float comparison techniques: - NaN detection: value != value (NaN is the only value that doesn't equal itself) - Infinity detection: value > 1.0e308 or value < -1.0e308 (beyond max float range) This approach correctly identifies special float values without impossible comparisons that Dialyzer flags. Dialyzer results: - Before: Total errors: 509, Skipped: 506 - After: Total errors: 506, Skipped: 506 - Status: done (passed successfully), EXIT CODE: 0 All 3434 tests passing. Validation logic now correctly identifies NaN and infinity values while satisfying Dialyzer type analysis. --- lib/towerops/agent/validator.ex | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/towerops/agent/validator.ex b/lib/towerops/agent/validator.ex index 5b68b568..681cdb9c 100644 --- a/lib/towerops/agent/validator.ex +++ b/lib/towerops/agent/validator.ex @@ -369,10 +369,17 @@ defmodule Towerops.Agent.Validator do # Validate sensor value (must be finite float/int) defp validate_sensor_value(value) when is_float(value) or is_integer(value) do - if is_float(value) and (value == :nan or value == :infinity or value == :neg_infinity) do - {:error, {:invalid_sensor_value, "Sensor value must be finite number"}} - else - :ok + # Check for NaN (NaN is the only value that doesn't equal itself) + # Check for infinity by comparing with max/min float values + cond do + is_float(value) and value != value -> + {:error, {:invalid_sensor_value, "Sensor value must be finite number (not NaN)"}} + + is_float(value) and (value > 1.0e308 or value < -1.0e308) -> + {:error, {:invalid_sensor_value, "Sensor value must be finite number (not infinity)"}} + + true -> + :ok end end