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
19 OCaml's floating-point numbers follow the IEEE 754 standard, using dou‐
20 ble precision (64 bits) numbers. Floating-point operations never raise
21 an exception on overflow, underflow, division by zero, etc. Instead,
22 special IEEE numbers are returned as appropriate, such as infinity for
23 1.0 /. 0.0 , neg_infinity for -1.0 /. 0.0 , and nan ('not a number')
24 for 0.0 /. 0.0 . These special numbers then propagate through float‐
25 ing-point computations as expected: for instance, 1.0 /. infinity is
26 0.0 , and any arithmetic operation with nan as argument returns nan as
27 result.
28
29
30 Since 4.07.0
31
32
33
34
35
36
37 val neg : float -> float
38
39 Unary negation.
40
41
42
43 val add : float -> float -> float
44
45 Floating-point addition.
46
47
48
49 val sub : float -> float -> float
50
51 Floating-point subtraction.
52
53
54
55 val mul : float -> float -> float
56
57 Floating-point multiplication.
58
59
60
61 val div : float -> float -> float
62
63 Floating-point division.
64
65
66
67 val rem : float -> float -> float
68
69
70 rem a b returns the remainder of a with respect to b . The returned
71 value is a -. n *. b , where n is the quotient a /. b rounded towards
72 zero to an integer.
73
74
75
76 val abs : float -> float
77
78
79 abs f returns the absolute value of f .
80
81
82
83 val infinity : float
84
85 Positive infinity.
86
87
88
89 val neg_infinity : float
90
91 Negative infinity.
92
93
94
95 val nan : float
96
97 A special floating-point value denoting the result of an undefined
98 operation such as 0.0 /. 0.0 . Stands for 'not a number'. Any float‐
99 ing-point operation with nan as argument returns nan as result. As for
100 floating-point comparisons, = , < , <= , > and >= return false and <>
101 returns true if one or both of their arguments is nan .
102
103
104
105 val pi : float
106
107 The constant pi.
108
109
110
111 val max_float : float
112
113 The largest positive finite value of type float .
114
115
116
117 val min_float : float
118
119 The smallest positive, non-zero, non-denormalized value of type float .
120
121
122
123 val epsilon : float
124
125 The difference between 1.0 and the smallest exactly representable
126 floating-point number greater than 1.0 .
127
128
129
130 val of_int : int -> float
131
132 Convert an integer to floating-point.
133
134
135
136 val to_int : float -> int
137
138 Truncate the given floating-point number to an integer. The result is
139 unspecified if the argument is nan or falls outside the range of repre‐
140 sentable integers.
141
142
143
144 val of_string : string -> float
145
146 Convert the given string to a float. The string is read in decimal (by
147 default) or in hexadecimal (marked by 0x or 0X ). The format of deci‐
148 mal floating-point numbers is [-] dd.ddd (e|E) [+|-] dd , where d
149 stands for a decimal digit. The format of hexadecimal floating-point
150 numbers is [-] 0(x|X) hh.hhh (p|P) [+|-] dd , where h stands for an
151 hexadecimal digit and d for a decimal digit. In both cases, at least
152 one of the integer and fractional parts must be given; the exponent
153 part is optional. The _ (underscore) character can appear anywhere in
154 the string and is ignored. Depending on the execution platforms, other
155 representations of floating-point numbers can be accepted, but should
156 not be relied upon. Raise Failure float_of_string if the given string
157 is not a valid representation of a float.
158
159
160
161 val of_string_opt : string -> float option
162
163 Same as of_string , but returns None instead of raising.
164
165
166
167 val to_string : float -> string
168
169 Return the string representation of a floating-point number.
170
171
172 type fpclass = Pervasives.fpclass =
173 | FP_normal (* Normal number, none of the below
174 *)
175 | FP_subnormal (* Number very close to 0.0, has reduced precision
176 *)
177 | FP_zero (* Number is 0.0 or -0.0
178 *)
179 | FP_infinite (* Number is positive or negative infinity
180 *)
181 | FP_nan (* Not a number: result of an undefined operation
182 *)
183
184
185 The five classes of floating-point numbers, as determined by the
186 Float.classify_float function.
187
188
189
190 val classify_float : float -> fpclass
191
192 Return the class of the given floating-point number: normal, subnormal,
193 zero, infinite, or not a number.
194
195
196
197 val pow : float -> float -> float
198
199 Exponentiation.
200
201
202
203 val sqrt : float -> float
204
205 Square root.
206
207
208
209 val exp : float -> float
210
211 Exponential.
212
213
214
215 val log : float -> float
216
217 Natural logarithm.
218
219
220
221 val log10 : float -> float
222
223 Base 10 logarithm.
224
225
226
227 val expm1 : float -> float
228
229
230 expm1 x computes exp x -. 1.0 , giving numerically-accurate results
231 even if x is close to 0.0 .
232
233
234
235 val log1p : float -> float
236
237
238 log1p x computes log(1.0 +. x) (natural logarithm), giving numeri‐
239 cally-accurate results even if x is close to 0.0 .
240
241
242
243 val cos : float -> float
244
245 Cosine. Argument is in radians.
246
247
248
249 val sin : float -> float
250
251 Sine. Argument is in radians.
252
253
254
255 val tan : float -> float
256
257 Tangent. Argument is in radians.
258
259
260
261 val acos : float -> float
262
263 Arc cosine. The argument must fall within the range [-1.0, 1.0] .
264 Result is in radians and is between 0.0 and pi .
265
266
267
268 val asin : float -> float
269
270 Arc sine. The argument must fall within the range [-1.0, 1.0] .
271 Result is in radians and is between -pi/2 and pi/2 .
272
273
274
275 val atan : float -> float
276
277 Arc tangent. Result is in radians and is between -pi/2 and pi/2 .
278
279
280
281 val atan2 : float -> float -> float
282
283
284 atan2 y x returns the arc tangent of y /. x . The signs of x and y are
285 used to determine the quadrant of the result. Result is in radians and
286 is between -pi and pi .
287
288
289
290 val hypot : float -> float -> float
291
292
293 hypot x y returns sqrt(x *. x + y *. y) , that is, the length of the
294 hypotenuse of a right-angled triangle with sides of length x and y ,
295 or, equivalently, the distance of the point (x,y) to origin. If one of
296 x or y is infinite, returns infinity even if the other is nan .
297
298
299
300 val cosh : float -> float
301
302 Hyperbolic cosine. Argument is in radians.
303
304
305
306 val sinh : float -> float
307
308 Hyperbolic sine. Argument is in radians.
309
310
311
312 val tanh : float -> float
313
314 Hyperbolic tangent. Argument is in radians.
315
316
317
318 val ceil : float -> float
319
320 Round above to an integer value. ceil f returns the least integer
321 value greater than or equal to f . The result is returned as a float.
322
323
324
325 val floor : float -> float
326
327 Round below to an integer value. floor f returns the greatest integer
328 value less than or equal to f . The result is returned as a float.
329
330
331
332 val copysign : float -> float -> float
333
334
335 copysign x y returns a float whose absolute value is that of x and
336 whose sign is that of y . If x is nan , returns nan . If y is nan ,
337 returns either x or -. x , but it is not specified which.
338
339
340
341 val frexp : float -> float * int
342
343
344 frexp f returns the pair of the significant and the exponent of f .
345 When f is zero, the significant x and the exponent n of f are equal to
346 zero. When f is non-zero, they are defined by f = x *. 2 ** n and 0.5
347 <= x < 1.0 .
348
349
350
351 val ldexp : float -> int -> float
352
353
354 ldexp x n returns x *. 2 ** n .
355
356
357
358 val modf : float -> float * float
359
360
361 modf f returns the pair of the fractional and integral part of f .
362
363
364 type t = float
365
366
367 An alias for the type of floating-point numbers.
368
369
370
371 val compare : t -> t -> int
372
373
374 compare x y returns 0 if x is equal to y , a negative integer if x is
375 less than y , and a positive integer if x is greater than y . compare
376 treats nan as equal to itself and less than any other float value.
377 This treatment of nan ensures that compare defines a total ordering
378 relation.
379
380
381
382 val equal : t -> t -> bool
383
384 The equal function for floating-point numbers, compared using
385 Float.compare .
386
387
388
389 val hash : t -> int
390
391 The hash function for floating-point numbers.
392
393
394 module Array : sig end
395
396
397
398
399
400
401
402OCamldoc 2019-02-02 Float(3)