2
* Number - Generic interface for numbers
3
* Copyright (C) 2007 Bluestorm <bluestorm dot dylc on-the-server gmail dot com>
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2.1 of the License, or (at your option) any later version,
10
* with the special exception on linking described in file LICENSE.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
A common interface for numbers.
25
@author Gabriel Scherer
33
This kind of exception is raised by "safe" numeric modules whenever
34
the number which should be returned is too large to be represented.
36
Non-"safe" numeric modules will return a result which depends on
37
the internal representation. For instance, with module {!Int},
38
[max_num + 1] returns [min_num]. By opposition, with module
39
{!Safe_int}, [max_num + 1] raises [Overflow].
47
This kind of exception is raised by "safe" modules whenever the
48
number which should be returned is not a number.
50
For instance, with module {!Safe_float}, [0.0 / 0.0] raises [NaN].
51
By opposition, with module {!Float}, [0.0 / 0.0] does not interrupt
52
computation and returns a special value [nan].
57
The smallest set of operations supported by every set of numbers.
59
This is presented as record to permit lightweight typeclass-style
74
modulo : 'a -> 'a -> 'a;
76
compare : 'a -> 'a -> int;
79
of_string : string -> 'a;
80
to_string : 'a -> string;
81
of_float: float -> 'a;
82
to_float: 'a -> float;
86
The full set of operations of a type of numbers
99
val modulo : t -> t -> t
100
val pow : t -> t -> t
101
val compare : t -> t -> int
102
val of_int : int -> t
103
val to_int : t -> int
104
val of_float: float -> t
105
val to_float: t -> float
106
val of_string : string -> t
107
val to_string : t -> string
108
val ( + ) : t -> t -> t
109
val ( - ) : t -> t -> t
110
val ( * ) : t -> t -> t
111
val ( / ) : t -> t -> t
112
val ( ** ) : t -> t -> t
113
val ( <> ) : t -> t -> bool
114
val ( >= ) : t -> t -> bool
115
val ( <= ) : t -> t -> bool
116
val ( > ) : t -> t -> bool
117
val ( < ) : t -> t -> bool
118
val ( = ) : t -> t -> bool
120
val operations : t numeric
123
module type Bounded =
130
module type Discrete =
136
val ( -- ): t -> t -> t Enum.t
137
val ( --- ): t -> t -> t Enum.t
145
The smallest set of operations supported by every set of numbers
147
module type NUMERIC_BASE =
155
(** {6 Arithmetic operations}
157
Depending on the implementation, some of these operations
158
{i may} raise exceptions at run-time to represent over/under-flows.*)
163
val add : t -> t -> t
164
val sub : t -> t -> t
165
val mul : t -> t -> t
166
val div : t -> t -> t
167
val modulo : t -> t -> t
168
val pow : t -> t -> t
169
val compare : t -> t -> int
171
(** {6 Conversions} *)
172
val of_int : int -> t
173
(** Convert this number to the closest integer.*)
175
val to_int : t -> int
176
(** Convert an integer to the closest element of set [t].*)
178
val of_string : string -> t
179
(** Convert the representation of a number to the corresponding
180
number. Raises [Invalid_arg] if the string does not represent
181
a valid number of type [t]*)
183
val to_string : t -> string
185
val of_float : float -> t
186
val to_float : t -> float
190
(** Automated definition of operators for a given numeric type.
192
You will only need this if you develop your own numeric modules.*)
195
functor (Base : NUMERIC_BASE) ->
197
val operations : Base.t numeric
198
val ( + ) : Base.t -> Base.t -> Base.t
199
val ( - ) : Base.t -> Base.t -> Base.t
200
val ( * ) : Base.t -> Base.t -> Base.t
201
val ( / ) : Base.t -> Base.t -> Base.t
202
val ( ** ) : Base.t -> Base.t -> Base.t
203
val ( <> ) : Base.t -> Base.t -> bool
204
val ( >= ) : Base.t -> Base.t -> bool
205
val ( <= ) : Base.t -> Base.t -> bool
206
val ( > ) : Base.t -> Base.t -> bool
207
val ( < ) : Base.t -> Base.t -> bool
208
val ( = ) : Base.t -> Base.t -> bool
212
val generic_pow : zero:'a -> one:'a -> div_two:('a -> 'a) -> mod_two:('a -> 'a) -> mul:('a -> 'a -> 'a) -> 'a -> 'a -> 'a