Float(3)
๐ Float(3) โ OCaml library ๐
๐ธ NAME ๐ธ
Float โ floating-point arithmetic, bestie. Thatโs the whole assignment. ๐
๐ง Module ๐ง
Module Float
: sig end
โจ Documentation โจ
Floating-point arithmetic, and ngl this module is lowkey iconic. 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, basic arithmetic operations (+., -., *., /.) with nan as an argument return nan, โฆ ๐ซถ
Since 4.07 โญ
val zero : floatโ the floating-point number0. ๐ Since 4.08val one : floatโ the floating-point1. ๐ธ Since 4.08val minus_one : floatโ the floating-point-1. ๐ Since 4.08val neg : float -> floatโ unary negation. Flip the vibe, flip the sign. ๐val add : float -> float -> floatโ floating-point addition. Math but make it sparkle. โจval sub : float -> float -> floatโ floating-point subtraction. Bye-bye, amount. ๐ฆval mul : float -> float -> floatโ floating-point multiplication. Double it and give it to the next one. ๐val div : float -> float -> floatโ floating-point division. Sharing is caring, allegedly. ๐val fma : float -> float -> float -> floatโfma x y zreturnsx * y + z, with a best effort for computing this expression with a single rounding, using either hardware instructions (providing full IEEE compliance) or a software emulation. ๐
โ ๏ธ On 64-bit Cygwin, 64-bit mingw-w64 and MSVC 2017 and earlier, this function may be emulated owing to known bugs or limitations on these platforms. Note: since software emulation of the fma is costly, make sure that you are using hardware fma support if performance matters. ๐
Since 4.08
val rem : float -> float -> floatโrem a breturns the remainder ofawith respect tob. The returned value isa -. n *. b, wherenis the quotienta /. brounded towards zero to an integer. Remainder, but make it precise. ๐งval succ : float -> floatโsucc xreturns the floating point number right afterxi.e., the smallest floating-point number greater thanx. See alsoFloat.next_after. Next up, bestie. โญ๏ธ Since 4.08val pred : float -> floatโpred xreturns the floating-point number right beforexi.e., the greatest floating-point number smaller thanx. See alsoFloat.next_after. Back it up, back it up. โฎ๏ธ Since 4.08val abs : float -> floatโabs freturns the absolute value off. No negatives in this house. ๐ซval infinity : floatโ positive infinity. Going up forever, fr fr. โพ๏ธval neg_infinity : floatโ negative infinity. Going down forever, no cap. ๐ณ๏ธval nan : floatโ a special floating-point value denoting the result of an undefined operation such as0.0 /. 0.0. Stands for 'not a number'. Any floating-point operation withnanas argument returnsnanas result, unless otherwise specified in IEEE 754 standard. As for floating-point comparisons,=,<,<=,>, and>=return false and<>returns true if one or both of their arguments isnan. ๐
nan is quiet_nan since 5.1; it was a signaling NaN before. ๐ซถ
val signaling_nan : floatโ Signaling NaN. The corresponding signals do not raise OCaml exception, but the value can be useful for interoperability with C libraries. โก Since 5.1val quiet_nan : floatโ Quiet NaN. Shhh, itโs giving calm but still not a number. ๐คซ Since 5.1val pi : floatโ the constantpi. Circumference queen. ๐ฅงval max_float : floatโ the largest positive finite value of typefloat. Big energy, but finite. ๐val min_float : floatโ the smallest positive, non-zero, non-denormalized value of typefloat. Tiny but mighty. ๐val epsilon : floatโ the difference between1.0and the smallest exactly representable floating-point number greater than1.0. The tiny gap between besties. ๐val is_finite : float -> boolโis_finite xis true if and only ifxis finite i.e., not infinite and notFloat.nan. ๐ธ Since 4.08val is_infinite : float -> boolโis_infinite xis true if and only ifxisFloat.infinityorFloat.neg_infinity. โพ๏ธ Since 4.08val is_nan : float -> boolโis_nan xis true if and only ifxis not a number (seeFloat.nan). ๐ Since 4.08val is_integer : float -> boolโis_integer xis true if and only ifxis an integer. Whole number behavior. ๐ข Since 4.08val of_int : int -> floatโ convert an integer to floating-point. Glow-up frominttofloat. โจval to_int : float -> intโ truncate the given floating-point number to an integer. The result is unspecified if the argument isnanor falls outside the range of representable integers. ๐จ
โ ๏ธ That โunspecifiedโ is not a vibe check, bestie โ donโt rely on it. ๐
val of_string : string -> floatโ convert the given string to a float. The string is read in decimal (by default) or in hexadecimal (marked by0xor0X). The format of decimal floating-point numbers is[-] dd.ddd (e|E) [+|-] dd, where d stands for a decimal digit. The format of hexadecimal floating-point numbers is[-] 0(x|X) hh.hhh (p|P) [+|-] dd, where h stands for an hexadecimal digit and d for a decimal digit. In both cases, at least one of the integer and fractional parts must be given; the exponent part is optional. The_(underscore) character can appear anywhere in the string and is ignored. Depending on the execution platforms, other representations of floating-point numbers can be accepted, but should not be relied upon. ๐ง
Raises Failure if the given string is not a valid representation of a float. ๐ญ
val of_string_opt : string -> float optionโ same asof_string, but returnsNoneinstead of raising. Gentle parenting the parser. ๐ซถval to_string : float -> stringโ return a string representation of a floating-point number. ๐
This conversion can involve a loss of precision. For greater control over the manner in which the number is printed, see Printf. โจ
This function is an alias for string_of_float. ๐
fpclasstype โ the five classes of floating-point numbers, as determined by theFloat.classify_floatfunction. ๐ง
type fpclass = fpclass =
| FP_normal (* Normal number, none of the below
*)
| FP_subnormal (* Number very close to 0.0, has reduced precision
*)
| FP_zero (* Number is 0.0 or -0.0
*)
| FP_infinite (* Number is positive or negative infinity
*)
| FP_nan (* Not a number: result of an undefined operation
*)
val classify_float : float -> fpclassโ return the class of the given floating-point number: normal, subnormal, zero, infinite, or not a number. Sorting hat, but for floats. ๐ฉval pow : float -> float -> floatโ exponentiation. Raise it up. ๐ฅval sqrt : float -> floatโ square root. Rooting for you, bestie. ๐ฑval cbrt : float -> floatโ cube root. Three-dimensional root energy. ๐ง Since 4.13val exp : float -> floatโ exponential. Eeeeee. ๐val exp2 : float -> floatโ base 2 exponential function. Binary glow-up. ๐ Since 4.13val log : float -> floatโ natural logarithm. Logging it for the group chat. ๐val log10 : float -> floatโ base 10 logarithm. Ten out of ten. ๐val log2 : float -> floatโ base 2 logarithm. Two cool. ๐ Since 4.13val expm1 : float -> floatโexpm1 xcomputesexp x -. 1.0, giving numerically-accurate results even ifxis close to0.0. Tiny-number protection, slay. โจval log1p : float -> floatโlog1p xcomputeslog(1.0 +. x)(natural logarithm), giving numerically-accurate results even ifxis close to0.0. Same tiny-number care, different function. ๐ซถval cos : float -> floatโ cosine. Argument is in radians. ๐val sin : float -> floatโ sine. Argument is in radians. ๐val tan : float -> floatโ tangent. Argument is in radians. ๐val acos : float -> floatโ arc cosine. The argument must fall within the range[-1.0, 1.0]. Result is in radians and is between0.0andpi. ๐ฏval asin : float -> floatโ arc sine. The argument must fall within the range[-1.0, 1.0]. Result is in radians and is between-pi/2andpi/2. ๐ฆval atan : float -> floatโ arc tangent. Result is in radians and is between-pi/2andpi/2. ๐ธval atan2 : float -> float -> floatโatan2 y xreturns the arc tangent ofy /. x. The signs ofxandyare used to determine the quadrant of the result. Result is in radians and is between-piandpi. Quadrant-aware, main character energy. ๐val hypot : float -> float -> floatโhypot x yreturnssqrt(x *. x +. y *. y), that is, the length of the hypotenuse of a right-angled triangle with sides of lengthxandy, or, equivalently, the distance of the point(x,y)to origin. If one ofxoryis infinite, returnsinfinityeven if the other isnan. Triangle math, but make it fashion. ๐๐val cosh : float -> floatโ hyperbolic cosine. Hyperbolic, not dramatic. ๐ญval sinh : float -> floatโ hyperbolic sine. ๐val tanh : float -> floatโ hyperbolic tangent. ๐val acosh : float -> floatโ hyperbolic arc cosine. The argument must fall within the range[1.0, inf]. Result is in radians and is between0.0andinf. โพ๏ธ Since 4.13val asinh : float -> floatโ hyperbolic arc sine. The argument and result range over the entire real line. Result is in radians. ๐ Since 4.13val atanh : float -> floatโ hyperbolic arc tangent. The argument must fall within the range[-1.0, 1.0]. Result is in radians and ranges over the entire real line. ๐ฆ Since 4.13val erf : float -> floatโ error function. The argument ranges over the entire real line. The result is always within[-1.0, 1.0]. ๐ Since 4.13val erfc : float -> floatโ complementary error function (erfc x = 1 - erf x). The argument ranges over the entire real line. The result is always within[0.0, 2.0]. ๐ Since 4.13val trunc : float -> floatโtrunc xroundsxto the nearest integer whose absolute value is less than or equal tox. Chopping off the fractional bit, respectfully. โ๏ธ Since 4.08val round : float -> floatโround xroundsxto the nearest integer with ties (fractional values of0.5) rounded away from zero, regardless of the current rounding direction. Ifxis an integer,+0.,-0.,nan, or infinite,xitself is returned. ๐
โ ๏ธ On 64-bit mingw-w64, this function may be emulated owing to a bug in the C runtime library (CRT) on this platform. ๐
Since 4.08
val ceil : float -> floatโ round above to an integer value.ceil freturns the least integer value greater than or equal tof. The result is returned as a float. Up, up, up. โฌ๏ธval floor : float -> floatโ round below to an integer value.floor freturns the greatest integer value less than or equal tof. The result is returned as a float. Down, down, down. โฌ๏ธval next_after : float -> float -> floatโnext_after x yreturns the next representable floating-point value followingxin the direction ofy. More precisely, ifyis greater (resp. less) thanx, it returns the smallest (resp. largest) representable number greater (resp. less) thanx. Ifxequalsy, the function returnsy. Ifxoryisnan, ananis returned. Note thatnext_after max_float infinity = infinityand thatnext_after 0. infinityis the smallest denormalized positive number. Ifxis the smallest denormalized positive number,next_after x 0. = 0. ๐ง Since 4.08val copy_sign : float -> float -> floatโ signature only in this part, bestie. ๐ซถcopy_sign x yโ returns a float whose absolute value is that ofxand whose sign is that ofy. Ifxisnan, returnsnan. Ifyisnan, returns eitherxor-. x, but it is not specified which โ the chaos is the point, iykyk ๐val sign_bit : float -> boolโsign_bit xistrueif and only if the sign bit ofxis set. For examplesign_bit 1.andsign_bit 0.arefalse, whilesign_bit (-1.)andsign_bit (-0.)aretrue. Since 4.08 โจval frexp : float -> float * intโfrexp freturns the pair of the significant and the exponent off. Whenfis zero, the significantxand the exponentnoffare equal to zero. Whenfis non-zero, they are defined byf = x *. 2 ** nand0.5 <= abs x < 1.0. math but make it cute ๐งval ldexp : float -> int -> floatโldexp x nreturnsx *. 2 ** n. simple, iconic, no notes ๐val modf : float -> float * floatโmodf freturns the pair of the fractional and integral part off. ๐type t = floatโ an alias for the type of floating-point numbers. same float energy, different name tag ๐val compare : t -> t -> intโcompare x yreturns0ifxis equal toy, a negative integer ifxis less thany, and a positive integer ifxis greater thany.comparetreatsnanas equal to itself and less than any other float value. This treatment ofnanensures thatcomparedefines a total ordering relation. nan said โI contain multitudes but Iโm still orderableโ ๐val equal : t -> t -> boolโ the equal function for floating-point numbers, compared usingFloat.compare. ๐val min : t -> t -> tโmin x yreturns the minimum ofxandy. It returnsnanwhenxoryisnan. Moreovermin (-0.) (+0.) = -0.Since 4.08 ๐ธval max : float -> float -> floatโmax x yreturns the maximum ofxandy. It returnsnanwhenxoryisnan. Moreovermax (-0.) (+0.) = +0.Since 4.08 ๐ฅval min_max : float -> float -> float * floatโmin_max x yis(min x y, max x y), just more efficient. two-for-one behavior, we love efficiency Since 4.08 โจval min_num : t -> t -> tโmin_num x yreturns the minimum ofxandytreatingnanas missing values. If bothxandyarenan,nanis returned. Moreovermin_num (-0.) (+0.) = -0.Since 4.08 ๐ฆval max_num : t -> t -> tโmax_num x yreturns the maximum ofxandytreatingnanas missing values. If bothxandyarenannanis returned. Moreovermax_num (-0.) (+0.) = +0.Since 4.08 โญval min_max_num : float -> float -> float * floatโmin_max_num x yis(min_num x y, max_num x y), just more efficient. Note that in particularmin_max_num x nan = (x, x)andmin_max_num nan y = (y, y). Since 4.08 ๐ซถval seeded_hash : int -> t -> intโ a seeded hash function for floats, with the same output value asHashtbl.seeded_hash. This function allows this module to be passed as argument to the functorHashtbl.MakeSeeded. Since 5.1 ๐val hash : t -> intโ an unseeded hash function for floats, with the same output value asHashtbl.hash. This function allows this module to be passed as argument to the functorHashtbl.Make. hash girl summer ๐
module Array : sig endโ Float arrays with packed representation. ๐module ArrayLabels : sig endโ Float arrays with packed representation (labeled functions). ๐ฆ
Shoutout to OCamldoc โ thatโs the source/version tag on this page โ dated 2026-06-22, and we are officially wrapping Float(3). Bye bestie, go treat your floats right! ๐โจ
๐ฆ where this page comes from
- arch rolling: ocaml 5.5.0-1 ยท LGPL2.1 AND custom: QPL-1.0
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms