3 ms·
Turns out computer math is actually super hard. Basic operations entail all kinds of undefined behavior and such. This code is a bit verbose but otherwise fam
by wffurr 1y ago
Turns out computer math is actually super hard. Basic operations entail all kinds of undefined behavior and such. This code is a bit verbose but otherwise familiar.
- Den_VR 1y agoIf we wanted defined behavior we’d build systems with Karnaugh maps all the way down.
- falcor84 1y ago# Step 3: Preemptively check for catastrophic magnitude differences if abs(a) > sys.float_info.max / 2: logging.warning("Value of a might cause overflow. Returning infinity just to be sure") return math.copysign (float('inf'), a) if abs(b) < sys.float_info.epsilon: logging.warning("Value of b dangerously close to zero. Returning NaN defensively.") return math.nan Does the above code make any sense? I've not worked with this sort of stuff before, but it seems entirely unreasonable to me to check them individually. E.g. if 1 < b < a, then it seems insane to me to return float('inf') for a large but finite a.