package batteries
Install
Dune Dependency
Authors
Maintainers
Sources
md5=ea26b5c72e6731e59d856626049cca4d
sha512=55975b62c26f6db77433a3ac31f97af609fc6789bb62ac38b267249c78fd44ff37fe81901f1cf560857b9493a6046dd37b0d1c0234c66bd59e52843aac3ce6cb
doc/batteries.unthreaded/BatFloat/index.html
Module BatFloat
Source
module Pervasives := Stdlib
Operations on floating-point numbers.
OCaml's floating-point numbers follow the IEEE 754 standard, using double precision (64 bits) numbers. Floating-point operations never raise an exception on overflow, underflow, division by zero, etc. Instead, special IEEE numbers are returned as appropriate, such as infinity
for 1.0 /. 0.0
, neg_infinity
for -1.0 /. 0.0
, and nan
(``not a number'') for 0.0 /. 0.0
. These special numbers then propagate through floating-point computations as expected: for instance, 1.0 /. infinity
is 0.0
, and any operation with nan
as argument returns nan
as result.
For more precision, see The Wikipedia entry on standard IEEE 754.
@documents Float
The type of floating-point numbers.
Floating-point numbers are the default representation of real numbers by OCaml.
Usual operations
Floating number zero. This is the same thing as 0.
Floating number one. This is the same thing as 1.
Returns the negation of the input, i.e. (fun x -> ~-. x)
Add 1.
to a floating number. Note that, as per IEEE 754, if x
is a large enough float number, succ x
might be equal to x
, due to rounding.
Subtract 1.
from a floating number. Note that, as per IEEE 754, if x
is a large enough float number, pred x
might be equal to x
, due to rounding.
The absolute value of a floating point number.
Operations specific to floating-point numbers
Square root.
Exponential.
Natural logarithm.
Base 10 logarithm.
The usual trigonometric functions.
The usual hyperbolic trigonometric functions.
Round the given float to an integer value. floor f
returns the greatest integer value less than or equal to f
. ceil f
returns the least integer value greater than or equal to f
.
round x
rounds x
to the nearest integral floating-point (the nearest of floor x
and ceil x
). In case the fraction of x is exactly 0.5, we round away from 0. : round 1.5
is 2.
but round (-3.5)
is -4.
.
round_to_int x
is int_of_float (round x)
.
round_to_string ~digits:d x
will return a string representation of x
-- in base 10 -- rounded to d
digits after the decimal point. By default, digits
is 0
, we round to the nearest integer.
This is strictly a convenience function for simple end-user printing and you should not rely on its behavior. One possible implementation is to rely on C `sprintf` internally, which means:
- no guarantee is given on the round-at-half behavior; it may not be consistent with
round
orround_to_int
round_to_string ~digits:0 3.
may return "3" instead of "3." asstring_of_float
would
- no guarantee is given on the behavior for abusively high number of digits precision; for example
round_to_string ~digits:max_int x
may return the empty string.
root x n
calculates the nth root of x.
copysign x y
returns a copy of x
with the same sign as y
.
is_nan f
returns true
if f
is nan
, false
otherwise.
is_special f
returns true
if f
is nan
or +/- infinity
, false
otherwise.
is_finite f
returns true
if f
is not nan
or +/- infinity
, false
otherwise.
Constants
Special float constants. It may not be safe to compare directly with these, as they have multiple internal representations. Instead use the is_special
, is_nan
, etc. tests
Positive infinity.
Negative infinity.
A special floating-point value denoting the result of an undefined operation such as 0.0 /. 0.0
. Stands for ``not a number''. Any floating-point operation with nan
as argument returns nan
as result. As for floating-point comparisons, =
, <
, <=
, >
and >=
return false
and <>
returns true
if one or both of their arguments is nan
.
Numeric constants
The smallest positive float x
such that 1.0 +. x <> 1.0
.
Euler? ... Euler? ... Euler?
Math.log2 e
log10 e
log 2
log 10
The constant pi (3.14159...)
pi /. 2.
pi /. 4.
1. /. pi
2. /. pi
2. *. sqrt pi
sqrt 2.
1. /. sqrt 2.
Operations on the internal representation of floating-point numbers
frexp f
returns the pair of the significant and the exponent of f
. When f
is zero, the significant x
and the exponent n
of f
are equal to zero. When f
is non-zero, they are defined by f = x *. 2 ** n
and 0.5 <= x < 1.0
.
ldexp x n
returns x *. 2 ** n
.
modf f
returns the pair of the fractional and integral part of f
.
Classes of floating point numbers
The five classes of floating-point numbers, as determined by the classify
function.
Return the class of the given floating-point number: normal, subnormal, zero, infinite, or not a number.
Test whether two floats are approximately equal (i.e. within epsilon of each other). epsilon
defaults to 1e-5.
Submodules grouping all infix operators
Boilerplate code
Printing
Operations on floating-point numbers, with exceptions raised in case of error.