~ubuntu-branches/ubuntu/precise/ocaml-batteries/precise

« back to all changes in this revision

Viewing changes to src/core/extlib/number.mli

  • Committer: Bazaar Package Importer
  • Author(s): Stefano Zacchiroli
  • Date: 2010-03-06 16:03:38 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100306160338-spvwiv3uc4jr28hw
Tags: 1.1.0-1
* New upstream release
  - major changes, "diet" version of the library
  - fix old FTBFS error, due to major code changes (Closes: #569455)
* Revamp packaging
  - adapt to new stuff shipped by upstream
  - switch from CDBS to dh
  - adapt dependencies (generally: reduce them)
* debian/patches/
  - remove old debian/patches/{debian-specific-installation-paths,
    debian-specific-info-on-doc-availability} as obsolete
  - new patch 0001-install-fix-for-bytecode-only-build: avoid
    installing *.a files with bytecode only compilation
* debian/libbatteries-ocaml-dev.links: remove file, shortend
  /usr/bin/ocaml-batteries to the top-level no longer exists
* remove debian/README.Debian (previous content is now obsolete)
* bump Standards-Version to 3.8.4 (no changes needed)
* debian/watch: update to match new upstream version convention
* debian/libbatteries-ocaml-{dev,doc}.{docs,examples}: ship only doc
  file from the root dir, other stuff is currently out of date
  (Closes: #514265)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
(* 
2
 
 * Number - Generic interface for numbers
3
 
 * Copyright (C) 2007 Bluestorm <bluestorm dot dylc on-the-server gmail dot com>
4
 
 *               2008 David Teller
5
 
 * 
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.
11
 
 *
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.
16
 
 *
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
20
 
 *)
21
 
 
22
 
(**
23
 
   A common interface for numbers.
24
 
 
25
 
   @author Gabriel Scherer
26
 
   @author David Teller
27
 
*)
28
 
 
29
 
 
30
 
(**
31
 
   Arithmetic overflow.
32
 
 
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.
35
 
 
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].
40
 
   
41
 
*)
42
 
exception Overflow
43
 
 
44
 
(**
45
 
   Not a Number
46
 
 
47
 
   This kind of exception is raised by "safe" modules whenever the
48
 
   number which should be returned is not a number.
49
 
 
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].
53
 
*)
54
 
exception NaN
55
 
 
56
 
(**
57
 
   The smallest set of operations supported by every set of numbers.
58
 
 
59
 
   This is presented as record to permit lightweight typeclass-style
60
 
   computation.
61
 
*)
62
 
type 'a numeric =
63
 
{
64
 
    zero : 'a;
65
 
    one : 'a;
66
 
    neg : 'a -> 'a;
67
 
    succ : 'a -> 'a;
68
 
    pred : 'a -> 'a;
69
 
    abs : 'a -> 'a;
70
 
    add : 'a -> 'a -> 'a;
71
 
    sub : 'a -> 'a -> 'a;
72
 
    mul : 'a -> 'a -> 'a;
73
 
    div : 'a -> 'a -> 'a;
74
 
    modulo : 'a -> 'a -> 'a;
75
 
    pow : 'a -> 'a -> 'a;
76
 
    compare : 'a -> 'a -> int;
77
 
    of_int : int -> 'a;
78
 
    to_int : 'a -> int;
79
 
    of_string : string -> 'a;
80
 
    to_string : 'a -> string;
81
 
    of_float: float -> 'a;
82
 
    to_float: 'a -> float;
83
 
}
84
 
 
85
 
(**
86
 
   The full set of operations of a type of numbers
87
 
*)
88
 
module type Numeric =
89
 
sig  
90
 
  type t
91
 
  val zero : t
92
 
  val one : t
93
 
  val neg : t -> t
94
 
  val abs : t -> t
95
 
  val add : t -> t -> t
96
 
  val sub : t -> t -> t
97
 
  val mul : t -> t -> t
98
 
  val div : t -> t -> t
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
119
 
    
120
 
  val operations : t numeric
121
 
end
122
 
 
123
 
module type Bounded =
124
 
sig
125
 
  type t
126
 
  val min_num: t
127
 
  val max_num: t
128
 
end
129
 
 
130
 
module type Discrete =
131
 
sig
132
 
  type t
133
 
  val to_int: t -> int
134
 
  val succ  : t -> t
135
 
  val pred  : t -> t
136
 
  val ( -- ): t -> t -> t Enum.t
137
 
  val ( --- ): t -> t -> t Enum.t
138
 
end
139
 
 
140
 
(**/**)
141
 
 
142
 
(** {6 Utilities}*)
143
 
 
144
 
(**
145
 
   The smallest set of operations supported by every set of numbers
146
 
*)
147
 
module type NUMERIC_BASE =
148
 
  sig
149
 
    type t
150
 
 
151
 
 
152
 
    val zero : t
153
 
    val one  : t
154
 
 
155
 
  (** {6 Arithmetic operations} 
156
 
      
157
 
      Depending on the implementation, some of these operations
158
 
      {i may} raise exceptions at run-time to represent over/under-flows.*)
159
 
    val neg : t -> t
160
 
    val succ : t -> t
161
 
    val pred : t -> t
162
 
    val abs : t -> t
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
170
 
 
171
 
    (** {6 Conversions} *)
172
 
  val of_int : int -> t
173
 
    (** Convert this number to the closest integer.*)
174
 
 
175
 
  val to_int : t -> int
176
 
    (** Convert an integer to the closest element of set [t].*)
177
 
 
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]*)
182
 
 
183
 
  val to_string : t -> string
184
 
 
185
 
  val of_float : float -> t
186
 
  val to_float : t -> float
187
 
  end
188
 
 
189
 
 
190
 
(** Automated definition of operators for a given numeric type.
191
 
 
192
 
    You will only need this if you develop your own numeric modules.*)
193
 
 
194
 
module MakeNumeric :
195
 
  functor (Base : NUMERIC_BASE) ->
196
 
sig
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
209
 
end
210
 
 
211
 
 
212
 
val generic_pow : zero:'a -> one:'a -> div_two:('a -> 'a) -> mod_two:('a -> 'a) -> mul:('a -> 'a -> 'a) -> 'a -> 'a -> 'a