floating point - IEEE 64 and 32 bit float validation in OCaml -
i have string matching following regex \-?[0-9]*\.[0-9]+
supposedly represents ieee floating point number. single or double precision , know type in advance. need check if interpreted valid value in given precission. like:
val is_valid_float: string -> bool val is_valid_double: string -> bool
for double precision numbers, can parse using float_of_string
, catch exception. unsure how deal single precision.
@jonathanchan's comments enlightening, more might say.
however, i'm not sure mean validation.
is "1.0000000000000001" valid float?
val f : string = "1.0000000000000001" # float_of_string f;; - : float = 1. #
there's no exception indicate number can't represented distinct 1.0.
if ignore issues of precision, might not difficult test against representable range string operation.
as @jonathanchan points out, best answer depends on how sure need (and want sure of, exactly).
Comments
Post a Comment