Open the context
Choose the project folder and name the files or behavior involved.
Decode one unsigned 16-bit binary16 word into a numeric value. Test signed zero, subnormals, infinities and NaNs, rejecting Boolean or out-of-range words without preserving NaN payloads.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
from math import copysign, ldexp
def binary16_value(word):
if type(word) is not int or not 0 <= word <= 65535:
raise ValueError("binary16.word")
sign = -1.0 if word & 0x8000 else 1.0
exponent, fraction = (word >> 10) & 31, word & 1023
if exponent == 31:
return copysign(float("nan") if fraction else float("inf"), sign)
value = ldexp(fraction, -24) if exponent == 0 else ldexp(1024+fraction, exponent-25)
return copysign(value, sign)The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.