package kdl

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

KDL numbers.

type t = [
  1. | `Int of int
  2. | `Int_raw of string
  3. | `Float_raw of string
]

Numeric KDL value.

Although the KDL spec does not differentiate integers and floats, a number is parsed as `Float_raw (stored as string) if it is written in the e scientific notation or contains the . decimal separator. If an integer is too large for the OCaml int, it is parsed as `Int_raw instead of `Int. The strings are not normalized in any way. `Float_raw also includes special values: "inf", "-inf", "nan".

In general, one should not pattern match over specific constructors of the variant, and instead use the to_* functions below while matching over #Kdl.number as num.

val to_string : [< t ] -> string

Convert the number to string.

val to_float : [< t ] -> float

Convert the number to float, potentially losing precision.

Failure might be raised in case `Int_raw or `Float_raw contain an invalid literal, which should not happen if they were constructed by the parsing functions.

val to_int : [< t ] -> int option

Convert the number to int. Results in None if `Int_raw or `Float_raw are invalid literals or do not fit in int, or if `Float_raw is not a whole number. `Float_raw str is parsed as a float that must be in the [-(2^53-1), (2^53-1)] interval.

val to_int_exn : [< t ] -> int

Raising version of to_int.

val to_int32 : [< t ] -> int32 option

Convert the number to int32. The to_int semantics apply.

val to_int32_exn : [< t ] -> int32

Raising version of to_int32.

val to_int64 : [< t ] -> int64 option

Convert the number to int64. The to_int semantics apply.

val to_int64_exn : [< t ] -> int64

Raising version of to_int64.

val to_nativeint : [< t ] -> nativeint option

Convert the number to nativeint. The to_int semantics apply.

val to_nativeint_exn : [< t ] -> nativeint

Raising version of to_nativeint.

val to_int_unsigned : [< t ] -> int option

Unsigned version of the to_int conversion.

val to_int32_unsigned : [< t ] -> int32 option

Unsigned version of the to_int32 conversion.

val to_int64_unsigned : [< t ] -> int64 option

Unsigned version of the to_int64 conversion.

val to_nativeint_unsigned : [< t ] -> nativeint option

Unsigned version of the to_nativeint conversion.

val of_float : float -> [> t ]
val of_int : int -> [> t ]
val of_int32 : int32 -> [> t ]
val of_int64 : int64 -> [> t ]
val of_nativeint : nativeint -> [> t ]
val equal : [< t ] -> [< t ] -> bool

Note that number values may not necessarily be normalized.

OCaml

Innovation. Community. Security.