1Float(3)                         OCaml library                        Float(3)
2
3
4

NAME

6       Float - Floating-point arithmetic.
7

Module

9       Module   Float
10

Documentation

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.
99
100       On  64-bit  Cygwin,  64-bit  mingw-w64  and MSVC 2017 and earlier, this
101       function may be emulated owing to known bugs on  limitations  on  these
102       platforms.   Note:  since software emulation of the fma is costly, make
103       sure that you are using hardware fma support if performance matters.
104
105
106       Since 4.08.0
107
108
109
110       val rem : float -> float -> float
111
112
113       rem a b returns the remainder of a with respect to b  .   The  returned
114       value  is  a -. n *. b , where n is the quotient a /. b rounded towards
115       zero to an integer.
116
117
118
119       val succ : float -> float
120
121
122       succ x returns the floating point number right after x i.e., the small‐
123       est floating-point number greater than x .  See also Float.next_after .
124
125
126       Since 4.08.0
127
128
129
130       val pred : float -> float
131
132
133       pred  x  returns  the  floating-point  number  right before x i.e., the
134       greatest  floating-point  number   smaller   than   x   .    See   also
135       Float.next_after .
136
137
138       Since 4.08.0
139
140
141
142       val abs : float -> float
143
144
145       abs f returns the absolute value of f .
146
147
148
149       val infinity : float
150
151       Positive infinity.
152
153
154
155       val neg_infinity : float
156
157       Negative infinity.
158
159
160
161       val nan : float
162
163       A  special floating-point value denoting the result of an undefined op‐
164       eration such as 0.0 /. 0.0 .  Stands for 'not a  number'.   Any  float‐
165       ing-point operation with nan as argument returns nan as result.  As for
166       floating-point comparisons, = , < , <= , > and >= return false  and  <>
167       returns true if one or both of their arguments is nan .
168
169
170
171       val pi : float
172
173       The constant pi.
174
175
176
177       val max_float : float
178
179       The largest positive finite value of type float .
180
181
182
183       val min_float : float
184
185       The smallest positive, non-zero, non-denormalized value of type float .
186
187
188
189       val epsilon : float
190
191       The  difference  between  1.0  and  the  smallest exactly representable
192       floating-point number greater than 1.0 .
193
194
195
196       val is_finite : float -> bool
197
198
199       is_finite x is true if and only if x is finite i.e., not  infinite  and
200       not Float.nan .
201
202
203       Since 4.08.0
204
205
206
207       val is_infinite : float -> bool
208
209
210       is_infinite   x  is  true  if  and  only  if  x  is  Float.infinity  or
211       Float.neg_infinity .
212
213
214       Since 4.08.0
215
216
217
218       val is_nan : float -> bool
219
220
221       is_nan x is true if and only if x is not a number (see Float.nan ).
222
223
224       Since 4.08.0
225
226
227
228       val is_integer : float -> bool
229
230
231       is_integer x is true if and only if x is an integer.
232
233
234       Since 4.08.0
235
236
237
238       val of_int : int -> float
239
240       Convert an integer to floating-point.
241
242
243
244       val to_int : float -> int
245
246       Truncate the given floating-point number to an integer.  The result  is
247       unspecified if the argument is nan or falls outside the range of repre‐
248       sentable integers.
249
250
251
252       val of_string : string -> float
253
254       Convert the given string to a float.  The string is read in decimal (by
255       default)  or in hexadecimal (marked by 0x or 0X ).  The format of deci‐
256       mal floating-point numbers is [-] dd.ddd  (e|E)  [+|-]  dd  ,  where  d
257       stands  for  a decimal digit.  The format of hexadecimal floating-point
258       numbers is [-] 0(x|X) hh.hhh (p|P) [+|-] dd , where  h  stands  for  an
259       hexadecimal  digit  and d for a decimal digit.  In both cases, at least
260       one of the integer and fractional parts must  be  given;  the  exponent
261       part  is optional.  The _ (underscore) character can appear anywhere in
262       the string and is ignored.  Depending on the execution platforms, other
263       representations  of  floating-point numbers can be accepted, but should
264       not be relied upon.
265
266
267       Raises Failure if the given string is not a valid representation  of  a
268       float.
269
270
271
272       val of_string_opt : string -> float option
273
274       Same as of_string , but returns None instead of raising.
275
276
277
278       val to_string : float -> string
279
280       Return the string representation of a floating-point number.
281
282
283       type fpclass = fpclass =
284        | FP_normal  (* Normal number, none of the below
285        *)
286        | FP_subnormal  (* Number very close to 0.0, has reduced precision
287        *)
288        | FP_zero  (* Number is 0.0 or -0.0
289        *)
290        | FP_infinite  (* Number is positive or negative infinity
291        *)
292        | FP_nan  (* Not a number: result of an undefined operation
293        *)
294
295
296       The  five  classes  of  floating-point  numbers,  as  determined by the
297       Float.classify_float function.
298
299
300
301       val classify_float : float -> fpclass
302
303       Return the class of the given floating-point number: normal, subnormal,
304       zero, infinite, or not a number.
305
306
307
308       val pow : float -> float -> float
309
310       Exponentiation.
311
312
313
314       val sqrt : float -> float
315
316       Square root.
317
318
319
320       val cbrt : float -> float
321
322       Cube root.
323
324
325       Since 4.13.0
326
327
328
329       val exp : float -> float
330
331       Exponential.
332
333
334
335       val exp2 : float -> float
336
337       Base 2 exponential function.
338
339
340       Since 4.13.0
341
342
343
344       val log : float -> float
345
346       Natural logarithm.
347
348
349
350       val log10 : float -> float
351
352       Base 10 logarithm.
353
354
355
356       val log2 : float -> float
357
358       Base 2 logarithm.
359
360
361       Since 4.13.0
362
363
364
365       val expm1 : float -> float
366
367
368       expm1  x  computes  exp  x -. 1.0 , giving numerically-accurate results
369       even if x is close to 0.0 .
370
371
372
373       val log1p : float -> float
374
375
376       log1p x computes log(1.0 +.  x)  (natural  logarithm),  giving  numeri‐
377       cally-accurate results even if x is close to 0.0 .
378
379
380
381       val cos : float -> float
382
383       Cosine.  Argument is in radians.
384
385
386
387       val sin : float -> float
388
389       Sine.  Argument is in radians.
390
391
392
393       val tan : float -> float
394
395       Tangent.  Argument is in radians.
396
397
398
399       val acos : float -> float
400
401       Arc cosine.  The argument must fall within the range [-1.0, 1.0] .  Re‐
402       sult is in radians and is between 0.0 and pi .
403
404
405
406       val asin : float -> float
407
408       Arc sine.  The argument must fall within the range [-1.0, 1.0]  .   Re‐
409       sult is in radians and is between -pi/2 and pi/2 .
410
411
412
413       val atan : float -> float
414
415       Arc tangent.  Result is in radians and is between -pi/2 and pi/2 .
416
417
418
419       val atan2 : float -> float -> float
420
421
422       atan2 y x returns the arc tangent of y /. x .  The signs of x and y are
423       used to determine the quadrant of the result.  Result is in radians and
424       is between -pi and pi .
425
426
427
428       val hypot : float -> float -> float
429
430
431       hypot  x  y  returns sqrt(x *. x + y *. y) , that is, the length of the
432       hypotenuse of a right-angled triangle with sides of length x  and  y  ,
433       or, equivalently, the distance of the point (x,y) to origin.  If one of
434       x or y is infinite, returns infinity even if the other is nan .
435
436
437
438       val cosh : float -> float
439
440       Hyperbolic cosine.  Argument is in radians.
441
442
443
444       val sinh : float -> float
445
446       Hyperbolic sine.  Argument is in radians.
447
448
449
450       val tanh : float -> float
451
452       Hyperbolic tangent.  Argument is in radians.
453
454
455
456       val acosh : float -> float
457
458       Hyperbolic arc cosine.  The argument must fall within the  range  [1.0,
459       inf] .  Result is in radians and is between 0.0 and inf .
460
461
462       Since 4.13.0
463
464
465
466       val asinh : float -> float
467
468       Hyperbolic  arc  sine.   The  argument and result range over the entire
469       real line.  Result is in radians.
470
471
472       Since 4.13.0
473
474
475
476       val atanh : float -> float
477
478       Hyperbolic arc tangent.  The argument must fall within the range [-1.0,
479       1.0] .  Result is in radians and ranges over the entire real line.
480
481
482       Since 4.13.0
483
484
485
486       val erf : float -> float
487
488       Error  function.   The  argument ranges over the entire real line.  The
489       result is always within [-1.0, 1.0] .
490
491
492       Since 4.13.0
493
494
495
496       val erfc : float -> float
497
498       Complementary error function ( erfc x = 1 -  erf  x  ).   The  argument
499       ranges  over  the entire real line.  The result is always within [-1.0,
500       1.0] .
501
502
503       Since 4.13.0
504
505
506
507       val trunc : float -> float
508
509
510       trunc x rounds x to the nearest integer whose absolute  value  is  less
511       than or equal to x .
512
513
514       Since 4.08.0
515
516
517
518       val round : float -> float
519
520
521       round x rounds x to the nearest integer with ties (fractional values of
522       0.5) rounded away from zero, regardless of the current rounding  direc‐
523       tion.  If x is an integer, +0.  , -0.  , nan , or infinite, x itself is
524       returned.
525
526       On 64-bit mingw-w64, this function may be emulated owing to  a  bug  in
527       the C runtime library (CRT) on this platform.
528
529
530       Since 4.08.0
531
532
533
534       val ceil : float -> float
535
536       Round  above  to  an  integer  value.  ceil f returns the least integer
537       value greater than or equal to f .  The result is returned as a float.
538
539
540
541       val floor : float -> float
542
543       Round below to an integer value.  floor f returns the greatest  integer
544       value less than or equal to f .  The result is returned as a float.
545
546
547
548       val next_after : float -> float -> float
549
550
551       next_after x y returns the next representable floating-point value fol‐
552       lowing x in the direction of y .   More  precisely,  if  y  is  greater
553       (resp.  less)  than  x , it returns the smallest (resp. largest) repre‐
554       sentable number greater (resp. less) than x .  If  x  equals  y  ,  the
555       function  returns y .  If x or y is nan , a nan is returned.  Note that
556       next_after max_float infinity = infinity and that next_after 0.  infin‐
557       ity is the smallest denormalized positive number.  If x is the smallest
558       denormalized positive number, next_after x 0. = 0.
559
560
561
562       Since 4.08.0
563
564
565
566       val copy_sign : float -> float -> float
567
568
569       copy_sign x y returns a float whose absolute value is  that  of  x  and
570       whose  sign  is that of y .  If x is nan , returns nan .  If y is nan ,
571       returns either x or -. x , but it is not specified which.
572
573
574
575       val sign_bit : float -> bool
576
577
578       sign_bit x is true if and only if the sign bit of x is set.  For  exam‐
579       ple  sign_bit  1.   and signbit 0.  are false while sign_bit (-1.)  and
580       sign_bit (-0.)  are true .
581
582
583       Since 4.08.0
584
585
586
587       val frexp : float -> float * int
588
589
590       frexp f returns the pair of the significant and the  exponent  of  f  .
591       When  f is zero, the significant x and the exponent n of f are equal to
592       zero.  When f is non-zero, they are defined by f = x *. 2 ** n and  0.5
593       <= x < 1.0 .
594
595
596
597       val ldexp : float -> int -> float
598
599
600       ldexp x n returns x *. 2 ** n .
601
602
603
604       val modf : float -> float * float
605
606
607       modf f returns the pair of the fractional and integral part of f .
608
609
610       type t = float
611
612
613       An alias for the type of floating-point numbers.
614
615
616
617       val compare : t -> t -> int
618
619
620       compare  x  y returns 0 if x is equal to y , a negative integer if x is
621       less than y , and a positive integer if x is greater than y .   compare
622       treats  nan  as  equal  to  itself and less than any other float value.
623       This treatment of nan ensures that compare defines a total ordering re‐
624       lation.
625
626
627
628       val equal : t -> t -> bool
629
630       The   equal   function   for  floating-point  numbers,  compared  using
631       Float.compare .
632
633
634
635       val min : t -> t -> t
636
637
638       min x y returns the minimum of x and y .  It returns nan when x or y is
639       nan .  Moreover min (-0.) (+0.) = -0.
640
641
642
643       Since 4.08.0
644
645
646
647       val max : float -> float -> float
648
649
650       max x y returns the maximum of x and y .  It returns nan when x or y is
651       nan .  Moreover max (-0.) (+0.) = +0.
652
653
654
655       Since 4.08.0
656
657
658
659       val min_max : float -> float -> float * float
660
661
662       min_max x y is (min x y, max x y) , just more efficient.
663
664
665       Since 4.08.0
666
667
668
669       val min_num : t -> t -> t
670
671
672       min_num x y returns the minimum of x and y treating nan as missing val‐
673       ues.   If  both  x  and  y are nan , nan is returned.  Moreover min_num
674       (-0.) (+0.) = -0.
675
676
677
678       Since 4.08.0
679
680
681
682       val max_num : t -> t -> t
683
684
685       max_num x y returns the maximum of x and y treating nan as missing val‐
686       ues.   If both x and y are nan nan is returned.  Moreover max_num (-0.)
687       (+0.) = +0.
688
689
690
691       Since 4.08.0
692
693
694
695       val min_max_num : float -> float -> float * float
696
697
698       min_max_num x y is (min_num x y, max_num x y) ,  just  more  efficient.
699       Note  that in particular min_max_num x nan = (x, x) and min_max_num nan
700       y = (y, y) .
701
702
703       Since 4.08.0
704
705
706
707       val hash : t -> int
708
709       The hash function for floating-point numbers.
710
711
712       module Array : sig end
713
714
715       Float arrays with packed representation.
716
717
718       module ArrayLabels : sig end
719
720
721       Float arrays with packed representation (labeled functions).
722
723
724
725
726
727OCamldoc                          2022-02-04                          Float(3)
Impressum