1Float(3) OCaml library Float(3)
2
3
4
6 Float - Floating-point arithmetic.
7
9 Module Float
10
12 Module Float
13 : sig end
14
15
16 Floating-point arithmetic.
17
18 OCaml's floating-point numbers follow the IEEE 754 standard, using dou‐
19 ble precision (64 bits) numbers. Floating-point operations never raise
20 an exception on overflow, underflow, division by zero, etc. Instead,
21 special IEEE numbers are returned as appropriate, such as infinity for
22 1.0 /. 0.0 , neg_infinity for -1.0 /. 0.0 , and nan ('not a number')
23 for 0.0 /. 0.0 . These special numbers then propagate through float‐
24 ing-point computations as expected: for instance, 1.0 /. infinity is
25 0.0 , basic arithmetic operations ( +. , -. , *. , /. ) with nan as
26 an argument return nan , ...
27
28
29 Since 4.07.0
30
31
32
33
34
35
36 val zero : float
37
38 The floating point 0.
39
40
41 Since 4.08.0
42
43
44
45 val one : float
46
47 The floating-point 1.
48
49
50 Since 4.08.0
51
52
53
54 val minus_one : float
55
56 The floating-point -1.
57
58
59 Since 4.08.0
60
61
62
63 val neg : float -> float
64
65 Unary negation.
66
67
68
69 val add : float -> float -> float
70
71 Floating-point addition.
72
73
74
75 val sub : float -> float -> float
76
77 Floating-point subtraction.
78
79
80
81 val mul : float -> float -> float
82
83 Floating-point multiplication.
84
85
86
87 val div : float -> float -> float
88
89 Floating-point division.
90
91
92
93 val fma : float -> float -> float -> float
94
95
96 fma x y z returns x * y + z , with a best effort for computing this ex‐
97 pression with a single rounding, using either hardware instructions
98 (providing full IEEE compliance) or a software emulation. Note: since
99 software emulation of the fma is costly, make sure that you are using
100 hardware fma support if performance matters.
101
102
103 Since 4.08.0
104
105
106
107 val rem : float -> float -> float
108
109
110 rem a b returns the remainder of a with respect to b . The returned
111 value is a -. n *. b , where n is the quotient a /. b rounded towards
112 zero to an integer.
113
114
115
116 val succ : float -> float
117
118
119 succ x returns the floating point number right after x i.e., the small‐
120 est floating-point number greater than x . See also Float.next_after .
121
122
123 Since 4.08.0
124
125
126
127 val pred : float -> float
128
129
130 pred x returns the floating-point number right before x i.e., the
131 greatest floating-point number smaller than x . See also
132 Float.next_after .
133
134
135 Since 4.08.0
136
137
138
139 val abs : float -> float
140
141
142 abs f returns the absolute value of f .
143
144
145
146 val infinity : float
147
148 Positive infinity.
149
150
151
152 val neg_infinity : float
153
154 Negative infinity.
155
156
157
158 val nan : float
159
160 A special floating-point value denoting the result of an undefined op‐
161 eration such as 0.0 /. 0.0 . Stands for 'not a number'. Any float‐
162 ing-point operation with nan as argument returns nan as result. As for
163 floating-point comparisons, = , < , <= , > and >= return false and <>
164 returns true if one or both of their arguments is nan .
165
166
167
168 val pi : float
169
170 The constant pi.
171
172
173
174 val max_float : float
175
176 The largest positive finite value of type float .
177
178
179
180 val min_float : float
181
182 The smallest positive, non-zero, non-denormalized value of type float .
183
184
185
186 val epsilon : float
187
188 The difference between 1.0 and the smallest exactly representable
189 floating-point number greater than 1.0 .
190
191
192
193 val is_finite : float -> bool
194
195
196 is_finite x is true if and only if x is finite i.e., not infinite and
197 not Float.nan .
198
199
200 Since 4.08.0
201
202
203
204 val is_infinite : float -> bool
205
206
207 is_infinite x is true if and only if x is Float.infinity or
208 Float.neg_infinity .
209
210
211 Since 4.08.0
212
213
214
215 val is_nan : float -> bool
216
217
218 is_nan x is true if and only if x is not a number (see Float.nan ).
219
220
221 Since 4.08.0
222
223
224
225 val is_integer : float -> bool
226
227
228 is_integer x is true if and only if x is an integer.
229
230
231 Since 4.08.0
232
233
234
235 val of_int : int -> float
236
237 Convert an integer to floating-point.
238
239
240
241 val to_int : float -> int
242
243 Truncate the given floating-point number to an integer. The result is
244 unspecified if the argument is nan or falls outside the range of repre‐
245 sentable integers.
246
247
248
249 val of_string : string -> float
250
251 Convert the given string to a float. The string is read in decimal (by
252 default) or in hexadecimal (marked by 0x or 0X ). The format of deci‐
253 mal floating-point numbers is [-] dd.ddd (e|E) [+|-] dd , where d
254 stands for a decimal digit. The format of hexadecimal floating-point
255 numbers is [-] 0(x|X) hh.hhh (p|P) [+|-] dd , where h stands for an
256 hexadecimal digit and d for a decimal digit. In both cases, at least
257 one of the integer and fractional parts must be given; the exponent
258 part is optional. The _ (underscore) character can appear anywhere in
259 the string and is ignored. Depending on the execution platforms, other
260 representations of floating-point numbers can be accepted, but should
261 not be relied upon.
262
263
264 Raises Failure if the given string is not a valid representation of a
265 float.
266
267
268
269 val of_string_opt : string -> float option
270
271 Same as of_string , but returns None instead of raising.
272
273
274
275 val to_string : float -> string
276
277 Return the string representation of a floating-point number.
278
279
280 type fpclass = fpclass =
281 | FP_normal (* Normal number, none of the below
282 *)
283 | FP_subnormal (* Number very close to 0.0, has reduced precision
284 *)
285 | FP_zero (* Number is 0.0 or -0.0
286 *)
287 | FP_infinite (* Number is positive or negative infinity
288 *)
289 | FP_nan (* Not a number: result of an undefined operation
290 *)
291
292
293 The five classes of floating-point numbers, as determined by the
294 Float.classify_float function.
295
296
297
298 val classify_float : float -> fpclass
299
300 Return the class of the given floating-point number: normal, subnormal,
301 zero, infinite, or not a number.
302
303
304
305 val pow : float -> float -> float
306
307 Exponentiation.
308
309
310
311 val sqrt : float -> float
312
313 Square root.
314
315
316
317 val exp : float -> float
318
319 Exponential.
320
321
322
323 val log : float -> float
324
325 Natural logarithm.
326
327
328
329 val log10 : float -> float
330
331 Base 10 logarithm.
332
333
334
335 val expm1 : float -> float
336
337
338 expm1 x computes exp x -. 1.0 , giving numerically-accurate results
339 even if x is close to 0.0 .
340
341
342
343 val log1p : float -> float
344
345
346 log1p x computes log(1.0 +. x) (natural logarithm), giving numeri‐
347 cally-accurate results even if x is close to 0.0 .
348
349
350
351 val cos : float -> float
352
353 Cosine. Argument is in radians.
354
355
356
357 val sin : float -> float
358
359 Sine. Argument is in radians.
360
361
362
363 val tan : float -> float
364
365 Tangent. Argument is in radians.
366
367
368
369 val acos : float -> float
370
371 Arc cosine. The argument must fall within the range [-1.0, 1.0] . Re‐
372 sult is in radians and is between 0.0 and pi .
373
374
375
376 val asin : float -> float
377
378 Arc sine. The argument must fall within the range [-1.0, 1.0] . Re‐
379 sult is in radians and is between -pi/2 and pi/2 .
380
381
382
383 val atan : float -> float
384
385 Arc tangent. Result is in radians and is between -pi/2 and pi/2 .
386
387
388
389 val atan2 : float -> float -> float
390
391
392 atan2 y x returns the arc tangent of y /. x . The signs of x and y are
393 used to determine the quadrant of the result. Result is in radians and
394 is between -pi and pi .
395
396
397
398 val hypot : float -> float -> float
399
400
401 hypot x y returns sqrt(x *. x + y *. y) , that is, the length of the
402 hypotenuse of a right-angled triangle with sides of length x and y ,
403 or, equivalently, the distance of the point (x,y) to origin. If one of
404 x or y is infinite, returns infinity even if the other is nan .
405
406
407
408 val cosh : float -> float
409
410 Hyperbolic cosine. Argument is in radians.
411
412
413
414 val sinh : float -> float
415
416 Hyperbolic sine. Argument is in radians.
417
418
419
420 val tanh : float -> float
421
422 Hyperbolic tangent. Argument is in radians.
423
424
425
426 val trunc : float -> float
427
428
429 trunc x rounds x to the nearest integer whose absolute value is less
430 than or equal to x .
431
432
433 Since 4.08.0
434
435
436
437 val round : float -> float
438
439
440 round x rounds x to the nearest integer with ties (fractional values of
441 0.5) rounded away from zero, regardless of the current rounding direc‐
442 tion. If x is an integer, +0. , -0. , nan , or infinite, x itself is
443 returned.
444
445
446 Since 4.08.0
447
448
449
450 val ceil : float -> float
451
452 Round above to an integer value. ceil f returns the least integer
453 value greater than or equal to f . The result is returned as a float.
454
455
456
457 val floor : float -> float
458
459 Round below to an integer value. floor f returns the greatest integer
460 value less than or equal to f . The result is returned as a float.
461
462
463
464 val next_after : float -> float -> float
465
466
467 next_after x y returns the next representable floating-point value fol‐
468 lowing x in the direction of y . More precisely, if y is greater
469 (resp. less) than x , it returns the smallest (resp. largest) repre‐
470 sentable number greater (resp. less) than x . If x equals y , the
471 function returns y . If x or y is nan , a nan is returned. Note that
472 next_after max_float infinity = infinity and that next_after 0. infin‐
473 ity is the smallest denormalized positive number. If x is the smallest
474 denormalized positive number, next_after x 0. = 0.
475
476
477
478 Since 4.08.0
479
480
481
482 val copy_sign : float -> float -> float
483
484
485 copy_sign x y returns a float whose absolute value is that of x and
486 whose sign is that of y . If x is nan , returns nan . If y is nan ,
487 returns either x or -. x , but it is not specified which.
488
489
490
491 val sign_bit : float -> bool
492
493
494 sign_bit x is true if and only if the sign bit of x is set. For exam‐
495 ple sign_bit 1. and signbit 0. are false while sign_bit (-1.) and
496 sign_bit (-0.) are true .
497
498
499 Since 4.08.0
500
501
502
503 val frexp : float -> float * int
504
505
506 frexp f returns the pair of the significant and the exponent of f .
507 When f is zero, the significant x and the exponent n of f are equal to
508 zero. When f is non-zero, they are defined by f = x *. 2 ** n and 0.5
509 <= x < 1.0 .
510
511
512
513 val ldexp : float -> int -> float
514
515
516 ldexp x n returns x *. 2 ** n .
517
518
519
520 val modf : float -> float * float
521
522
523 modf f returns the pair of the fractional and integral part of f .
524
525
526 type t = float
527
528
529 An alias for the type of floating-point numbers.
530
531
532
533 val compare : t -> t -> int
534
535
536 compare x y returns 0 if x is equal to y , a negative integer if x is
537 less than y , and a positive integer if x is greater than y . compare
538 treats nan as equal to itself and less than any other float value.
539 This treatment of nan ensures that compare defines a total ordering re‐
540 lation.
541
542
543
544 val equal : t -> t -> bool
545
546 The equal function for floating-point numbers, compared using
547 Float.compare .
548
549
550
551 val min : t -> t -> t
552
553
554 min x y returns the minimum of x and y . It returns nan when x or y is
555 nan . Moreover min (-0.) (+0.) = -0.
556
557
558
559 Since 4.08.0
560
561
562
563 val max : float -> float -> float
564
565
566 max x y returns the maximum of x and y . It returns nan when x or y is
567 nan . Moreover max (-0.) (+0.) = +0.
568
569
570
571 Since 4.08.0
572
573
574
575 val min_max : float -> float -> float * float
576
577
578 min_max x y is (min x y, max x y) , just more efficient.
579
580
581 Since 4.08.0
582
583
584
585 val min_num : t -> t -> t
586
587
588 min_num x y returns the minimum of x and y treating nan as missing val‐
589 ues. If both x and y are nan , nan is returned. Moreover min_num
590 (-0.) (+0.) = -0.
591
592
593
594 Since 4.08.0
595
596
597
598 val max_num : t -> t -> t
599
600
601 max_num x y returns the maximum of x and y treating nan as missing val‐
602 ues. If both x and y are nan nan is returned. Moreover max_num (-0.)
603 (+0.) = +0.
604
605
606
607 Since 4.08.0
608
609
610
611 val min_max_num : float -> float -> float * float
612
613
614 min_max_num x y is (min_num x y, max_num x y) , just more efficient.
615 Note that in particular min_max_num x nan = (x, x) and min_max_num nan
616 y = (y, y) .
617
618
619 Since 4.08.0
620
621
622
623 val hash : t -> int
624
625 The hash function for floating-point numbers.
626
627
628 module Array : sig end
629
630
631 Float arrays with packed representation.
632
633
634 module ArrayLabels : sig end
635
636
637 Float arrays with packed representation (labeled functions).
638
639
640
641
642
643OCamldoc 2021-07-22 Float(3)