NB. Some models of floating point representations and operations NB. These models handle infinities and nan's NB. Some helping definitions floor =: <. log2 =: 2&^. copy =: # fs2bin =: monad define NB. check for infinities and nan's if. _ = y do. 0 1 1 1 1 1 1 1 1 , 23 copy 0 return. elseif. __ = y do. 1 1 1 1 1 1 1 1 1 , 23 copy 0 return. NB. this case is tricky since (for some reason) NB. _. = _. is false. Perhaps this is because NB. IEEE 754 specifies _1 + 2 ^ 23 different NB. representations for nan. elseif. 0 not_equal y - y do. 0 1 1 1 1 1 1 1 1 , 23 copy 1 return. end. NB. compute the characteristic e =. 1 + floor log2 | y + y = 0 NB. now the mantissa f =. 1 drop (24 copy 2) rep floor (2 ^ 24 - e) * | y NB. finally get the sign and exponent (binary form) se =. (9 copy 2) rep (y not_equal 0) * (256 * y < 0) + e + 126 se , f ) traced_fs2bin =: monad define NB. check for infinities and nan's if. _ = y do. 0 1 1 1 1 1 1 1 1 , 23 copy 0 return. elseif. __ = y do. 1 1 1 1 1 1 1 1 1 , 23 copy 0 return. NB. this case is tricky since (for some reason) NB. _. = _. is false. elseif. 0 not_equal y - y do. 0 1 1 1 1 1 1 1 1 , 23 copy 1 return. end. NB. compute the characteristic display'exponent'; e =. 1 + floor log2 | y + y = 0 if. _ = e do. _ return. end. NB. now the mantissa display'mantissa'; f=.1 drop (24 copy 2) rep floor (2 ^ 24 - e) * | y NB. finally get the sign and exponent (binary form) display'sign-exp'; se =. (9 copy 2) rep (y not_equal 0) * (256 * y < 0) + e + 126 se , f ) bin2fs =: monad define s =. 0 from y e =. base 1 2 3 4 5 6 7 8 from y f =. base 9 drop y if. (0 = e) and 0 = f do. 0 elseif. (255 = e) and 0 = f do. _ * _1 ^ s elseif. (255 = e) and 0 not_equal f do. _. elseif. (0 = e) and 0 not_equal f do. (_1 ^ s) * f * 2 ^ e - 126 elseif. 1 do. (_1 ^ s) * (2 ^ e - 127) * (base 1 , 9 drop y) % 2 ^ 23 end. ) traced_bin2fs =: monad define display's';s =. 0 from y display'e';e =. base 1 2 3 4 5 6 7 8 from y display'f';f =. base 9 drop y if. (0 = e) and 0 = f do. 0 elseif. (255 = e) and 0 = f do. _ * _1 ^ s elseif. (255 = e) and 0 not_equal f do. _. elseif. (0 = e) and 0 not_equal f do. (_1 ^ s) * f * 2 ^ e - 126 elseif. 1 do. (_1 ^ s) * (2 ^ e - 127) * (base 1 , 9 drop y) % 2 ^ 23 end. )