Debian Codegraph @

stdlib/templates/float.template.mli

raw symbols SCIP
1 (**************************************************************************)
2 (* *)
3 (* OCaml *)
4 (* *)
5 (* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
6 (* Nicolas Ojeda Bar, LexiFi *)
7 (* *)
8 (* Copyright 2018 Institut National de Recherche en Informatique et *)
9 (* en Automatique. *)
10 (* *)
11 (* All rights reserved. This file is distributed under the terms of *)
12 (* the GNU Lesser General Public License version 2.1, with the *)
13 (* special exception on linking described in the file LICENSE. *)
14 (* *)
15 (**************************************************************************)
16
17 (* NOTE:
18 If this file is float.template.mli, run tools/sync_stdlib_docs after editing
19 it to generate float.mli.
20
21 If this file is float.mli, do not edit it directly -- edit
22 templates/float.template.mli instead.
23 *)
24
25 (** Floating-point arithmetic.
26
27 OCaml's floating-point numbers follow the
28 IEEE 754 standard, using double precision (64 bits) numbers.
29 Floating-point operations never raise an exception on overflow,
30 underflow, division by zero, etc. Instead, special IEEE numbers
31 are returned as appropriate, such as [infinity] for [1.0 /. 0.0],
32 [neg_infinity] for [-1.0 /. 0.0], and [nan] ('not a number')
33 for [0.0 /. 0.0]. These special numbers then propagate through
34 floating-point computations as expected: for instance,
35 [1.0 /. infinity] is [0.0], basic arithmetic operations
36 ([+.], [-.], [*.], [/.]) with [nan] as an argument return [nan], ...
37
38 @since 4.07
39 *)
40
41 val zero : float
42 (** The floating point 0.
43 @since 4.08 *)
44
45 val one : float
46 (** The floating-point 1.
47 @since 4.08 *)
48
49 val minus_one : float
50 (** The floating-point -1.
51 @since 4.08 *)
52
53 external neg : float -> float = "%negfloat"
54 (** Unary negation. *)
55
56 external add : float -> float -> float = "%addfloat"
57 (** Floating-point addition. *)
58
59 external sub : float -> float -> float = "%subfloat"
60 (** Floating-point subtraction. *)
61
62 external mul : float -> float -> float = "%mulfloat"
63 (** Floating-point multiplication. *)
64
65 external div : float -> float -> float = "%divfloat"
66 (** Floating-point division. *)
67
68 external fma : float -> float -> float -> float =
69 "caml_fma_float" "caml_fma" [@@unboxed] [@@noalloc]
70 (** [fma x y z] returns [x * y + z], with a best effort for computing
71 this expression with a single rounding, using either hardware
72 instructions (providing full IEEE compliance) or a software
73 emulation.
74
75 On 64-bit Cygwin, 64-bit mingw-w64 and MSVC 2017 and earlier, this function
76 may be emulated owing to known bugs on limitations on these platforms.
77 Note: since software emulation of the fma is costly, make sure that you are
78 using hardware fma support if performance matters.
79
80 @since 4.08 *)
81
82 external rem : float -> float -> float = "caml_fmod_float" "fmod"
83 [@@unboxed] [@@noalloc]
84 (** [rem a b] returns the remainder of [a] with respect to [b]. The returned
85 value is [a -. n *. b], where [n] is the quotient [a /. b] rounded towards
86 zero to an integer. *)
87
88 val succ : float -> float
89 (** [succ x] returns the floating point number right after [x] i.e.,
90 the smallest floating-point number greater than [x]. See also
91 {!next_after}.
92 @since 4.08 *)
93
94 val pred : float -> float
95 (** [pred x] returns the floating-point number right before [x] i.e.,
96 the greatest floating-point number smaller than [x]. See also
97 {!next_after}.
98 @since 4.08 *)
99
100 external abs : float -> float = "%absfloat"
101 (** [abs f] returns the absolute value of [f]. *)
102
103 val infinity : float
104 (** Positive infinity. *)
105
106 val neg_infinity : float
107 (** Negative infinity. *)
108
109 val nan : float
110 (** A special floating-point value denoting the result of an
111 undefined operation such as [0.0 /. 0.0]. Stands for
112 'not a number'. Any floating-point operation with [nan] as
113 argument returns [nan] as result, unless otherwise specified in
114 IEEE 754 standard. As for floating-point comparisons,
115 [=], [<], [<=], [>] and [>=] return [false] and [<>] returns [true]
116 if one or both of their arguments is [nan].
117
118 [nan] is [quiet_nan] since 5.1; it was a signaling NaN before. *)
119
120 val signaling_nan : float
121 (** Signaling NaN. The corresponding signals do not raise OCaml exception,
122 but the value can be useful for interoperability with C libraries.
123
124 @since 5.1 *)
125
126 val quiet_nan : float
127 (** Quiet NaN.
128
129 @since 5.1 *)
130
131 val pi : float
132 (** The constant pi. *)
133
134 val max_float : float
135 (** The largest positive finite value of type [float]. *)
136
137 val min_float : float
138 (** The smallest positive, non-zero, non-denormalized value of type [float]. *)
139
140 val epsilon : float
141 (** The difference between [1.0] and the smallest exactly representable
142 floating-point number greater than [1.0]. *)
143
144 val is_finite : float -> bool
145 (** [is_finite x] is [true] if and only if [x] is finite i.e., not infinite and
146 not {!nan}.
147
148 @since 4.08 *)
149
150 val is_infinite : float -> bool
151 (** [is_infinite x] is [true] if and only if [x] is {!infinity} or
152 {!neg_infinity}.
153
154 @since 4.08 *)
155
156 val is_nan : float -> bool
157 (** [is_nan x] is [true] if and only if [x] is not a number (see {!nan}).
158
159 @since 4.08 *)
160
161 val is_integer : float -> bool
162 (** [is_integer x] is [true] if and only if [x] is an integer.
163
164 @since 4.08 *)
165
166 external of_int : int -> float = "%floatofint"
167 (** Convert an integer to floating-point. *)
168
169 external to_int : float -> int = "%intoffloat"
170 (** Truncate the given floating-point number to an integer.
171 The result is unspecified if the argument is [nan] or falls outside the
172 range of representable integers. *)
173
174 external of_string : string -> float = "caml_float_of_string"
175 (** Convert the given string to a float. The string is read in decimal
176 (by default) or in hexadecimal (marked by [0x] or [0X]).
177 The format of decimal floating-point numbers is
178 [ [-] dd.ddd (e|E) [+|-] dd ], where [d] stands for a decimal digit.
179 The format of hexadecimal floating-point numbers is
180 [ [-] 0(x|X) hh.hhh (p|P) [+|-] dd ], where [h] stands for an
181 hexadecimal digit and [d] for a decimal digit.
182 In both cases, at least one of the integer and fractional parts must be
183 given; the exponent part is optional.
184 The [_] (underscore) character can appear anywhere in the string
185 and is ignored.
186 Depending on the execution platforms, other representations of
187 floating-point numbers can be accepted, but should not be relied upon.
188 @raise Failure if the given string is not a valid
189 representation of a float. *)
190
191 val of_string_opt: string -> float option
192 (** Same as [of_string], but returns [None] instead of raising. *)
193
194 val to_string : float -> string
195 (** Return a string representation of a floating-point number.
196
197 This conversion can involve a loss of precision. For greater control over
198 the manner in which the number is printed, see {!Printf}.
199
200 This function is an alias for {!Stdlib.string_of_float}. *)
201
202 type fpclass = Stdlib.fpclass =
203 FP_normal (** Normal number, none of the below *)
204 | FP_subnormal (** Number very close to 0.0, has reduced precision *)
205 | FP_zero (** Number is 0.0 or -0.0 *)
206 | FP_infinite (** Number is positive or negative infinity *)
207 | FP_nan (** Not a number: result of an undefined operation *)
208 (** The five classes of floating-point numbers, as determined by
209 the {!classify_float} function. *)
210
211 external classify_float : (float [@unboxed]) -> fpclass =
212 "caml_classify_float" "caml_classify_float_unboxed" [@@noalloc]
213 (** Return the class of the given floating-point number:
214 normal, subnormal, zero, infinite, or not a number. *)
215
216 external pow : float -> float -> float = "caml_power_float" "pow"
217 [@@unboxed] [@@noalloc]
218 (** Exponentiation. *)
219
220 external sqrt : float -> float = "caml_sqrt_float" "sqrt"
221 [@@unboxed] [@@noalloc]
222 (** Square root. *)
223
224 external cbrt : float -> float = "caml_cbrt_float" "caml_cbrt"
225 [@@unboxed] [@@noalloc]
226 (** Cube root.
227
228 @since 4.13
229 *)
230
231 external exp : float -> float = "caml_exp_float" "exp" [@@unboxed] [@@noalloc]
232 (** Exponential. *)
233
234 external exp2 : float -> float = "caml_exp2_float" "caml_exp2"
235 [@@unboxed] [@@noalloc]
236 (** Base 2 exponential function.
237
238 @since 4.13
239 *)
240
241 external log : float -> float = "caml_log_float" "log" [@@unboxed] [@@noalloc]
242 (** Natural logarithm. *)
243
244 external log10 : float -> float = "caml_log10_float" "log10"
245 [@@unboxed] [@@noalloc]
246 (** Base 10 logarithm. *)
247
248 external log2 : float -> float = "caml_log2_float" "caml_log2"
249 [@@unboxed] [@@noalloc]
250 (** Base 2 logarithm.
251
252 @since 4.13
253 *)
254
255 external expm1 : float -> float = "caml_expm1_float" "caml_expm1"
256 [@@unboxed] [@@noalloc]
257 (** [expm1 x] computes [exp x -. 1.0], giving numerically-accurate results
258 even if [x] is close to [0.0]. *)
259
260 external log1p : float -> float = "caml_log1p_float" "caml_log1p"
261 [@@unboxed] [@@noalloc]
262 (** [log1p x] computes [log(1.0 +. x)] (natural logarithm),
263 giving numerically-accurate results even if [x] is close to [0.0]. *)
264
265 external cos : float -> float = "caml_cos_float" "cos" [@@unboxed] [@@noalloc]
266 (** Cosine. Argument is in radians. *)
267
268 external sin : float -> float = "caml_sin_float" "sin" [@@unboxed] [@@noalloc]
269 (** Sine. Argument is in radians. *)
270
271 external tan : float -> float = "caml_tan_float" "tan" [@@unboxed] [@@noalloc]
272 (** Tangent. Argument is in radians. *)
273
274 external acos : float -> float = "caml_acos_float" "acos"
275 [@@unboxed] [@@noalloc]
276 (** Arc cosine. The argument must fall within the range [[-1.0, 1.0]].
277 Result is in radians and is between [0.0] and [pi]. *)
278
279 external asin : float -> float = "caml_asin_float" "asin"
280 [@@unboxed] [@@noalloc]
281 (** Arc sine. The argument must fall within the range [[-1.0, 1.0]].
282 Result is in radians and is between [-pi/2] and [pi/2]. *)
283
284 external atan : float -> float = "caml_atan_float" "atan"
285 [@@unboxed] [@@noalloc]
286 (** Arc tangent.
287 Result is in radians and is between [-pi/2] and [pi/2]. *)
288
289 external atan2 : float -> float -> float = "caml_atan2_float" "atan2"
290 [@@unboxed] [@@noalloc]
291 (** [atan2 y x] returns the arc tangent of [y /. x]. The signs of [x]
292 and [y] are used to determine the quadrant of the result.
293 Result is in radians and is between [-pi] and [pi]. *)
294
295 external hypot : float -> float -> float = "caml_hypot_float" "caml_hypot"
296 [@@unboxed] [@@noalloc]
297 (** [hypot x y] returns [sqrt(x *. x +. y *. y)], that is, the length
298 of the hypotenuse of a right-angled triangle with sides of length
299 [x] and [y], or, equivalently, the distance of the point [(x,y)]
300 to origin. If one of [x] or [y] is infinite, returns [infinity]
301 even if the other is [nan]. *)
302
303 external cosh : float -> float = "caml_cosh_float" "cosh"
304 [@@unboxed] [@@noalloc]
305 (** Hyperbolic cosine. Argument is in radians. *)
306
307 external sinh : float -> float = "caml_sinh_float" "sinh"
308 [@@unboxed] [@@noalloc]
309 (** Hyperbolic sine. Argument is in radians. *)
310
311 external tanh : float -> float = "caml_tanh_float" "tanh"
312 [@@unboxed] [@@noalloc]
313 (** Hyperbolic tangent. Argument is in radians. *)
314
315 external acosh : float -> float = "caml_acosh_float" "caml_acosh"
316 [@@unboxed] [@@noalloc]
317 (** Hyperbolic arc cosine. The argument must fall within the range
318 [[1.0, inf]].
319 Result is in radians and is between [0.0] and [inf].
320
321 @since 4.13
322 *)
323
324 external asinh : float -> float = "caml_asinh_float" "caml_asinh"
325 [@@unboxed] [@@noalloc]
326 (** Hyperbolic arc sine. The argument and result range over the entire
327 real line.
328 Result is in radians.
329
330 @since 4.13
331 *)
332
333 external atanh : float -> float = "caml_atanh_float" "caml_atanh"
334 [@@unboxed] [@@noalloc]
335 (** Hyperbolic arc tangent. The argument must fall within the range
336 [[-1.0, 1.0]].
337 Result is in radians and ranges over the entire real line.
338
339 @since 4.13
340 *)
341
342 external erf : float -> float = "caml_erf_float" "caml_erf"
343 [@@unboxed] [@@noalloc]
344 (** Error function. The argument ranges over the entire real line.
345 The result is always within [[-1.0, 1.0]].
346
347 @since 4.13
348 *)
349
350 external erfc : float -> float = "caml_erfc_float" "caml_erfc"
351 [@@unboxed] [@@noalloc]
352 (** Complementary error function ([erfc x = 1 - erf x]).
353 The argument ranges over the entire real line.
354 The result is always within [[0.0, 2.0]].
355
356 @since 4.13
357 *)
358
359 external trunc : float -> float = "caml_trunc_float" "caml_trunc"
360 [@@unboxed] [@@noalloc]
361 (** [trunc x] rounds [x] to the nearest integer whose absolute value is
362 less than or equal to [x].
363
364 @since 4.08 *)
365
366 external round : float -> float = "caml_round_float" "caml_round"
367 [@@unboxed] [@@noalloc]
368 (** [round x] rounds [x] to the nearest integer with ties (fractional
369 values of 0.5) rounded away from zero, regardless of the current
370 rounding direction. If [x] is an integer, [+0.], [-0.], [nan], or
371 infinite, [x] itself is returned.
372
373 On 64-bit mingw-w64, this function may be emulated owing to a bug in the
374 C runtime library (CRT) on this platform.
375
376 @since 4.08 *)
377
378 external ceil : float -> float = "caml_ceil_float" "ceil"
379 [@@unboxed] [@@noalloc]
380 (** Round above to an integer value.
381 [ceil f] returns the least integer value greater than or equal to [f].
382 The result is returned as a float. *)
383
384 external floor : float -> float = "caml_floor_float" "floor"
385 [@@unboxed] [@@noalloc]
386 (** Round below to an integer value.
387 [floor f] returns the greatest integer value less than or
388 equal to [f].
389 The result is returned as a float. *)
390
391 external next_after : float -> float -> float
392 = "caml_nextafter_float" "caml_nextafter" [@@unboxed] [@@noalloc]
393 (** [next_after x y] returns the next representable floating-point
394 value following [x] in the direction of [y]. More precisely, if
395 [y] is greater (resp. less) than [x], it returns the smallest
396 (resp. largest) representable number greater (resp. less) than [x].
397 If [x] equals [y], the function returns [y]. If [x] or [y] is
398 [nan], a [nan] is returned.
399 Note that [next_after max_float infinity = infinity] and that
400 [next_after 0. infinity] is the smallest denormalized positive number.
401 If [x] is the smallest denormalized positive number,
402 [next_after x 0. = 0.]
403
404 @since 4.08 *)
405
406 external copy_sign : float -> float -> float
407 = "caml_copysign_float" "caml_copysign"
408 [@@unboxed] [@@noalloc]
409 (** [copy_sign x y] returns a float whose absolute value is that of [x]
410 and whose sign is that of [y]. If [x] is [nan], returns [nan].
411 If [y] is [nan], returns either [x] or [-. x], but it is not
412 specified which. *)
413
414 external sign_bit : (float [@unboxed]) -> bool
415 = "caml_signbit_float" "caml_signbit" [@@noalloc]
416 (** [sign_bit x] is [true] if and only if the sign bit of [x] is set.
417 For example [sign_bit 1.] and [signbit 0.] are [false] while
418 [sign_bit (-1.)] and [sign_bit (-0.)] are [true].
419
420 @since 4.08 *)
421
422 external frexp : float -> float * int = "caml_frexp_float"
423 (** [frexp f] returns the pair of the significant
424 and the exponent of [f]. When [f] is zero, the
425 significant [x] and the exponent [n] of [f] are equal to
426 zero. When [f] is non-zero, they are defined by
427 [f = x *. 2 ** n] and [0.5 <= x < 1.0]. *)
428
429 external ldexp : (float [@unboxed]) -> (int [@untagged]) -> (float [@unboxed]) =
430 "caml_ldexp_float" "caml_ldexp_float_unboxed" [@@noalloc]
431 (** [ldexp x n] returns [x *. 2 ** n]. *)
432
433 external modf : float -> float * float = "caml_modf_float"
434 (** [modf f] returns the pair of the fractional and integral
435 part of [f]. *)
436
437 type t = float
438 (** An alias for the type of floating-point numbers. *)
439
440 val compare: t -> t -> int
441 (** [compare x y] returns [0] if [x] is equal to [y], a negative integer if [x]
442 is less than [y], and a positive integer if [x] is greater than
443 [y]. [compare] treats [nan] as equal to itself and less than any other float
444 value. This treatment of [nan] ensures that [compare] defines a total
445 ordering relation. *)
446
447 val equal: t -> t -> bool
448 (** The equal function for floating-point numbers, compared using {!compare}. *)
449
450 val min : t -> t -> t
451 (** [min x y] returns the minimum of [x] and [y]. It returns [nan]
452 when [x] or [y] is [nan]. Moreover [min (-0.) (+0.) = -0.]
453
454 @since 4.08 *)
455
456 val max : float -> float -> float
457 (** [max x y] returns the maximum of [x] and [y]. It returns [nan]
458 when [x] or [y] is [nan]. Moreover [max (-0.) (+0.) = +0.]
459
460 @since 4.08 *)
461
462 val min_max : float -> float -> float * float
463 (** [min_max x y] is [(min x y, max x y)], just more efficient.
464
465 @since 4.08 *)
466
467 val min_num : t -> t -> t
468 (** [min_num x y] returns the minimum of [x] and [y] treating [nan] as
469 missing values. If both [x] and [y] are [nan], [nan] is returned.
470 Moreover [min_num (-0.) (+0.) = -0.]
471
472 @since 4.08 *)
473
474 val max_num : t -> t -> t
475 (** [max_num x y] returns the maximum of [x] and [y] treating [nan] as
476 missing values. If both [x] and [y] are [nan] [nan] is returned.
477 Moreover [max_num (-0.) (+0.) = +0.]
478
479 @since 4.08 *)
480
481 val min_max_num : float -> float -> float * float
482 (** [min_max_num x y] is [(min_num x y, max_num x y)], just more
483 efficient. Note that in particular [min_max_num x nan = (x, x)]
484 and [min_max_num nan y = (y, y)].
485
486 @since 4.08 *)
487
488 val seeded_hash : int -> t -> int
489 (** A seeded hash function for floats, with the same output value as
490 {!Hashtbl.seeded_hash}. This function allows this module to be passed as
491 argument to the functor {!Hashtbl.MakeSeeded}.
492
493 @since 5.1 *)
494
495 val hash : t -> int
496 (** An unseeded hash function for floats, with the same output value as
497 {!Hashtbl.hash}. This function allows this module to be passed as argument
498 to the functor {!Hashtbl.Make}. *)
499
500 module Array : sig
501 FLOATARRAY
502 end
503 (** Float arrays with packed representation. *)
504
505 module ArrayLabels : sig
506 FLOATARRAYLAB
507 end
508 (** Float arrays with packed representation (labeled functions). *)