float_of_string leve l'exception Failure si la chaine n'est pas un flottant valide (PR#581)

git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@3961 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
master
Xavier Leroy 2001-10-30 16:52:05 +00:00
parent acd469c220
commit d6bbde1118
2 changed files with 14 additions and 6 deletions

View File

@ -101,9 +101,13 @@ CAMLprim value format_float(value fmt, value arg)
return res;
}
CAMLprim value float_of_string(value s)
CAMLprim value float_of_string(value vs)
{
return copy_double(atof(String_val(s)));
char * s = String_val(vs);
char * ends;
double d = strtod((const char *) s, &ends);
if (ends != s + string_length(vs)) failwith("float_of_string");
return copy_double(d);
}
CAMLprim value int_of_float(value f)

View File

@ -383,11 +383,16 @@ external int_of_float : float -> int = "%intoffloat"
val infinity: float
(** Negative infinity. *)
val neg_infinity: float
(** A special floating-point value denoting the result of an
undefined operation such as [0.0 /. 0.0]. Stands for
``not a number''. *)
val nan: float
(** The largest positive finite value of type [float]. *)
val max_float: float
(** The smallest positive, non-zero, non-denormalized value of type [float]. *)
val min_float: float
(** The smallest positive float [x] such that [1.0 +. x <> 1.0]. *)
val epsilon_float: float
(** The five classes of floating-point numbers, as determined by
the {!Pervasives.classify_float} function. *)
@ -462,9 +467,8 @@ external int_of_string : string -> int = "int_of_string"
(** Return the string representation of a floating-point number. *)
val string_of_float : float -> string
(** Convert the given string to a float.
The result is unspecified if the given string is not
a valid representation of a float. *)
(** Convert the given string to a float. Raise [Failure "float_of_string"]
if the given string is not a valid representation of a float. *)
external float_of_string : string -> float = "float_of_string"