Hacker News new | ask | show | jobs
by wffurr 249 days 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.
2 comments

    # 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.
It also uses float epsilon and I don't think I have ever seen code where using it was appropriate.
It’s parody
Ignoring the sign of b for big a can't be right.
If we wanted defined behavior we’d build systems with Karnaugh maps all the way down.