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
17 Floating-point arithmetic
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 , and any arithmetic operation with nan as argument returns nan as
26 result.
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
97 expression 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
161 operation 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 iff x is finite i.e., not infinite and not
197 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 iff x is Float.infinity or Float.neg_infinity .
208
209
210 Since 4.08.0
211
212
213
214 val is_nan : float -> bool
215
216
217 is_nan x is true iff x is not a number (see Float.nan ).
218
219
220 Since 4.08.0
221
222
223
224 val is_integer : float -> bool
225
226
227 is_integer x is true iff x is an integer.
228
229
230 Since 4.08.0
231
232
233
234 val of_int : int -> float
235
236 Convert an integer to floating-point.
237
238
239
240 val to_int : float -> int
241
242 Truncate the given floating-point number to an integer. The result is
243 unspecified if the argument is nan or falls outside the range of repre‐
244 sentable integers.
245
246
247
248 val of_string : string -> float
249
250 Convert the given string to a float. The string is read in decimal (by
251 default) or in hexadecimal (marked by 0x or 0X ). The format of deci‐
252 mal floating-point numbers is [-] dd.ddd (e|E) [+|-] dd , where d
253 stands for a decimal digit. The format of hexadecimal floating-point
254 numbers is [-] 0(x|X) hh.hhh (p|P) [+|-] dd , where h stands for an
255 hexadecimal digit and d for a decimal digit. In both cases, at least
256 one of the integer and fractional parts must be given; the exponent
257 part is optional. The _ (underscore) character can appear anywhere in
258 the string and is ignored. Depending on the execution platforms, other
259 representations of floating-point numbers can be accepted, but should
260 not be relied upon.
261
262
263 Raises Failure if the given string is not a valid representation of a
264 float.
265
266
267
268 val of_string_opt : string -> float option
269
270 Same as of_string , but returns None instead of raising.
271
272
273
274 val to_string : float -> string
275
276 Return the string representation of a floating-point number.
277
278
279 type fpclass = fpclass =
280 | FP_normal (* Normal number, none of the below
281 *)
282 | FP_subnormal (* Number very close to 0.0, has reduced precision
283 *)
284 | FP_zero (* Number is 0.0 or -0.0
285 *)
286 | FP_infinite (* Number is positive or negative infinity
287 *)
288 | FP_nan (* Not a number: result of an undefined operation
289 *)
290
291
292 The five classes of floating-point numbers, as determined by the
293 Float.classify_float function.
294
295
296
297 val classify_float : float -> fpclass
298
299 Return the class of the given floating-point number: normal, subnormal,
300 zero, infinite, or not a number.
301
302
303
304 val pow : float -> float -> float
305
306 Exponentiation.
307
308
309
310 val sqrt : float -> float
311
312 Square root.
313
314
315
316 val exp : float -> float
317
318 Exponential.
319
320
321
322 val log : float -> float
323
324 Natural logarithm.
325
326
327
328 val log10 : float -> float
329
330 Base 10 logarithm.
331
332
333
334 val expm1 : float -> float
335
336
337 expm1 x computes exp x -. 1.0 , giving numerically-accurate results
338 even if x is close to 0.0 .
339
340
341
342 val log1p : float -> float
343
344
345 log1p x computes log(1.0 +. x) (natural logarithm), giving numeri‐
346 cally-accurate results even if x is close to 0.0 .
347
348
349
350 val cos : float -> float
351
352 Cosine. Argument is in radians.
353
354
355
356 val sin : float -> float
357
358 Sine. Argument is in radians.
359
360
361
362 val tan : float -> float
363
364 Tangent. Argument is in radians.
365
366
367
368 val acos : float -> float
369
370 Arc cosine. The argument must fall within the range [-1.0, 1.0] .
371 Result is in radians and is between 0.0 and pi .
372
373
374
375 val asin : float -> float
376
377 Arc sine. The argument must fall within the range [-1.0, 1.0] .
378 Result is in radians and is between -pi/2 and pi/2 .
379
380
381
382 val atan : float -> float
383
384 Arc tangent. Result is in radians and is between -pi/2 and pi/2 .
385
386
387
388 val atan2 : float -> float -> float
389
390
391 atan2 y x returns the arc tangent of y /. x . The signs of x and y are
392 used to determine the quadrant of the result. Result is in radians and
393 is between -pi and pi .
394
395
396
397 val hypot : float -> float -> float
398
399
400 hypot x y returns sqrt(x *. x + y *. y) , that is, the length of the
401 hypotenuse of a right-angled triangle with sides of length x and y ,
402 or, equivalently, the distance of the point (x,y) to origin. If one of
403 x or y is infinite, returns infinity even if the other is nan .
404
405
406
407 val cosh : float -> float
408
409 Hyperbolic cosine. Argument is in radians.
410
411
412
413 val sinh : float -> float
414
415 Hyperbolic sine. Argument is in radians.
416
417
418
419 val tanh : float -> float
420
421 Hyperbolic tangent. Argument is in radians.
422
423
424
425 val trunc : float -> float
426
427
428 trunc x rounds x to the nearest integer whose absolute value is less
429 than or equal to x .
430
431
432 Since 4.08.0
433
434
435
436 val round : float -> float
437
438
439 round x rounds x to the nearest integer with ties (fractional values of
440 0.5) rounded away from zero, regardless of the current rounding direc‐
441 tion. If x is an integer, +0. , -0. , nan , or infinite, x itself is
442 returned.
443
444
445 Since 4.08.0
446
447
448
449 val ceil : float -> float
450
451 Round above to an integer value. ceil f returns the least integer
452 value greater than or equal to f . The result is returned as a float.
453
454
455
456 val floor : float -> float
457
458 Round below to an integer value. floor f returns the greatest integer
459 value less than or equal to f . The result is returned as a float.
460
461
462
463 val next_after : float -> float -> float
464
465
466 next_after x y returns the next representable floating-point value fol‐
467 lowing x in the direction of y . More precisely, if y is greater
468 (resp. less) than x , it returns the smallest (resp. largest) repre‐
469 sentable number greater (resp. less) than x . If x equals y , the
470 function returns y . If x or y is nan , a nan is returned. Note that
471 next_after max_float infinity = infinity and that next_after 0. infin‐
472 ity is the smallest denormalized positive number. If x is the smallest
473 denormalized positive number, next_after x 0. = 0.
474
475
476
477 Since 4.08.0
478
479
480
481 val copy_sign : float -> float -> float
482
483
484 copy_sign x y returns a float whose absolute value is that of x and
485 whose sign is that of y . If x is nan , returns nan . If y is nan ,
486 returns either x or -. x , but it is not specified which.
487
488
489
490 val sign_bit : float -> bool
491
492
493 sign_bit x is true iff the sign bit of x is set. For example sign_bit
494 1. and signbit 0. are false while sign_bit (-1.) and sign_bit (-0.)
495 are true .
496
497
498 Since 4.08.0
499
500
501
502 val frexp : float -> float * int
503
504
505 frexp f returns the pair of the significant and the exponent of f .
506 When f is zero, the significant x and the exponent n of f are equal to
507 zero. When f is non-zero, they are defined by f = x *. 2 ** n and 0.5
508 <= x < 1.0 .
509
510
511
512 val ldexp : float -> int -> float
513
514
515 ldexp x n returns x *. 2 ** n .
516
517
518
519 val modf : float -> float * float
520
521
522 modf f returns the pair of the fractional and integral part of f .
523
524
525 type t = float
526
527
528 An alias for the type of floating-point numbers.
529
530
531
532 val compare : t -> t -> int
533
534
535 compare x y returns 0 if x is equal to y , a negative integer if x is
536 less than y , and a positive integer if x is greater than y . compare
537 treats nan as equal to itself and less than any other float value.
538 This treatment of nan ensures that compare defines a total ordering
539 relation.
540
541
542
543 val equal : t -> t -> bool
544
545 The equal function for floating-point numbers, compared using
546 Float.compare .
547
548
549
550 val min : t -> t -> t
551
552
553 min x y returns the minimum of x and y . It returns nan when x or y is
554 nan . Moreover min (-0.) (+0.) = -0.
555
556
557
558 Since 4.08.0
559
560
561
562 val max : float -> float -> float
563
564
565 max x y returns the maximum of x and y . It returns nan when x or y is
566 nan . Moreover max (-0.) (+0.) = +0.
567
568
569
570 Since 4.08.0
571
572
573
574 val min_max : float -> float -> float * float
575
576
577 min_max x y is (min x y, max x y) , just more efficient.
578
579
580 Since 4.08.0
581
582
583
584 val min_num : t -> t -> t
585
586
587 min_num x y returns the minimum of x and y treating nan as missing val‐
588 ues. If both x and y are nan , nan is returned. Moreover min_num
589 (-0.) (+0.) = -0.
590
591
592
593 Since 4.08.0
594
595
596
597 val max_num : t -> t -> t
598
599
600 max_num x y returns the maximum of x and y treating nan as missing val‐
601 ues. If both x and y are nan nan is returned. Moreover max_num (-0.)
602 (+0.) = +0.
603
604
605
606 Since 4.08.0
607
608
609
610 val min_max_num : float -> float -> float * float
611
612
613 min_max_num x y is (min_num x y, max_num x y) , just more efficient.
614 Note that in particular min_max_num x nan = (x, x) and min_max_num nan
615 y = (y, y) .
616
617
618 Since 4.08.0
619
620
621
622 val hash : t -> int
623
624 The hash function for floating-point numbers.
625
626
627 module Array : sig end
628
629
630
631
632 module ArrayLabels : sig end
633
634
635
636
637
638
639
640OCamldoc 2020-09-01 Float(3)