Data::Float(3pm) | User Contributed Perl Documentation | Data::Float(3pm) |

use Data::Float qw(have_signed_zero); if(have_signed_zero) { ... # and many other constants; see text use Data::Float qw( float_class float_is_normal float_is_subnormal float_is_nzfinite float_is_zero float_is_finite float_is_infinite float_is_nan); $class = float_class($value); if(float_is_normal($value)) { ... if(float_is_subnormal($value)) { ... if(float_is_nzfinite($value)) { ... if(float_is_zero($value)) { ... if(float_is_finite($value)) { ... if(float_is_infinite($value)) { ... if(float_is_nan($value)) { ... use Data::Float qw(float_sign signbit float_parts); $sign = float_sign($value); $sign_bit = signbit($value); ($sign, $exponent, $significand) = float_parts($value); use Data::Float qw(float_hex hex_float); print float_hex($value); $value = hex_float($string); use Data::Float qw(float_id_cmp totalorder); @sorted_floats = sort { float_id_cmp($a, $b) } @floats; if(totalorder($a, $b)) { ... use Data::Float qw( pow2 mult_pow2 copysign nextup nextdown nextafter); $x = pow2($exp); $x = mult_pow2($value, $exp); $x = copysign($magnitude, $sign_from); $x = nextup($x); $x = nextdown($x); $x = nextafter($x, $direction);

- normalised
- The value is made up of a sign bit (making the value positive or negative), a significand, and exponent. The significand is a number in the range [1, 2), expressed as a binary fraction of a certain fixed length. (Significands requiring a longer binary fraction, or lacking a terminating binary representation, cannot be obtained.) The exponent is an integer in a certain fixed range. The magnitude of the value represented is the product of the significand and two to the power of the exponent.

- subnormal
- The value is made up of a sign bit, significand, and exponent, as for normalised values. However, the exponent is fixed at the minimum possible for a normalised value, and the significand is in the range (0, 1). The length of the significand is the same as for normalised values. This is essentially a fixed-point format, used to provide gradual underflow. Not all floating point formats support this subtype. Where it is not supported, underflow is sudden, and the difference between two minimum-exponent normalised values cannot be exactly represented.

- zero
- Depending on the floating point type, there may be either one or two zero values: zeroes may carry a sign bit. Where zeroes are signed, it is primarily in order to indicate the direction from which a value underflowed (was rounded) to zero. Positive and negative zero compare as numerically equal, and they give identical results in most arithmetic operations. They are on opposite sides of some branch cuts in complex arithmetic.

- infinite
- Some floating point formats include special infinite
values. These are generated by overflow, and by some arithmetic cases that
mathematically generate infinities. There are two infinite values:
positive infinity and negative infinity.

- not-a-number (NaN)
- This type of value exists in some floating point formats to
indicate error conditions. Mathematically undefined operations may
generate NaNs, and NaNs propagate through all arithmetic operations. A NaN
has the distinctive property of comparing numerically unequal to all
floating point values, including itself.

- have_signed_zero
- Truth value indicating whether floating point zeroes carry a sign. If yes, then there are two floating point zero values: +0.0 and -0.0. (Perl scalars can nevertheless also hold an integer zero, which is unsigned.) If no, then there is only one zero value, which is unsigned.

- have_subnormal
- Truth value indicating whether there are subnormal floating point values.

- have_infinite
- Truth value indicating whether there are infinite floating point values.

- have_nan
- Truth value indicating whether there are NaN floating point
values.

- significand_bits
- The number of fractional bits in the significand of finite floating point values. The significand also has an implicit integer bit, not counted in this constant; the integer bit is always 1 for normalised values and always 0 for subnormal values.

- significand_step
- The difference between adjacent representable values in the range [1, 2] (where the exponent is zero). This is equal to 2^-significand_bits.

- max_finite_exp
- The maximum exponent permitted for finite floating point values.

- max_finite_pow2
- The maximum representable power of two. This is 2^max_finite_exp.

- max_finite
- The maximum representable finite value. This is 2^(max_finite_exp+1) - 2^(max_finite_exp-significand_bits).

- max_number
- The maximum representable number. This is positive infinity if there are infinite values, or max_finite if there are not.

- max_integer
- The maximum integral value for which all integers from zero to that value inclusive are representable. Equivalently: the minimum positive integral value N for which the value N+1 is not representable. This is 2^(significand_bits+1). The name is somewhat misleading.

- min_normal_exp
- The minimum exponent permitted for normalised floating point values.

- min_normal
- The minimum positive value representable as a normalised floating point value. This is 2^min_normal_exp.

- min_finite_exp
- The base two logarithm of the minimum representable positive finite value. If there are subnormals then this is min_normal_exp - significand_bits. If there are no subnormals then this is min_normal_exp.

- min_finite
- The minimum representable positive finite value. This is 2^min_finite_exp.

- pos_zero
- The positive zero value. (Exists only if zeroes are signed,
as indicated by the "have_signed_zero" constant.)

- neg_zero
- The negative zero value. (Exists only if zeroes are signed,
as indicated by the "have_signed_zero" constant.)

- pos_infinity
- The positive infinite value. (Exists only if there are infinite values, as indicated by the "have_infinite" constant.)

- neg_infinity
- The negative infinite value. (Exists only if there are infinite values, as indicated by the "have_infinite" constant.)

- nan
- Not-a-number. (Exists only if NaN values were detected, as indicated by the "have_nan" constant.)

- float_class(VALUE)
- Determines which of the five classes described above VALUE falls into. Returns "NORMAL", "SUBNORMAL", "ZERO", "INFINITE", or "NAN" accordingly.

- float_is_normal(VALUE)
- Returns true iff VALUE is a normalised floating point value.

- float_is_subnormal(VALUE)
- Returns true iff VALUE is a subnormal floating point value.

- float_is_nzfinite(VALUE)
- Returns true iff VALUE is a non-zero finite value (either normal or subnormal; not zero, infinite, or NaN).

- float_is_zero(VALUE)
- Returns true iff VALUE is a zero. If zeroes are signed then the sign is irrelevant.

- float_is_finite(VALUE)
- Returns true iff VALUE is a finite value (either normal, subnormal, or zero; not infinite or NaN).

- float_is_infinite(VALUE)
- Returns true iff VALUE is an infinity (either positive infinity or negative infinity).

- float_is_nan(VALUE)
- Returns true iff VALUE is a NaN.

- float_sign(VALUE)
- Returns "
**+**" or "**-**" to indicate the sign of VALUE. An unsigned zero returns the sign "**+**". "die"s if VALUE is a NaN.

- signbit(VALUE)
- VALUE must be a floating point value. Returns the sign bit
of VALUE: 0 if VALUE is positive or a positive or unsigned zero, or 1 if
VALUE is negative or a negative zero. Returns an unpredictable value if
VALUE is a NaN.

- float_parts(VALUE)
- Divides up a non-zero finite floating point value into sign, exponent, and significand, returning these as a three-element list in that order. The significand is returned as a floating point value, in the range [1, 2) for normalised values, and in the range (0, 1) for subnormals. "die"s if VALUE is not finite and non-zero.

- float_hex(VALUE[, OPTIONS])
- Encodes the exact value of VALUE as a hexadecimal fraction,
returning the fraction as a string. Specifically, for finite values the
output is of the form "
*s***0x***m***.***mmmmm***p***eee*", where "*s*" is the sign, "*m***.***mmmm*" is the significand in hexadecimal, and "*eee*" is the exponent in decimal with a sign.

**exp_digits**- The number of digits of exponent to show, unless this is
modified by
**exp_digits_range_mod**or more are required to show the exponent exactly. (The exponent is always shown in full.) Default 0, so the minimum possible number of digits is used.

**exp_digits_range_mod**- Modifies the number of exponent digits to show, based on
the number of digits required to show the full range of exponents for
normalised and subnormal values. If "
**IGNORE**" then nothing is done. If "**ATLEAST**" then at least this many digits are shown. Default "**IGNORE**".

**exp_neg_sign**- The string that is prepended to a negative exponent.
Default "
**-**".

**exp_pos_sign**- The string that is prepended to a non-negative exponent.
Default "
**+**". Make it the empty string to suppress the positive sign.

**frac_digits**- The number of fractional digits to show, unless this is
modified by
**frac_digits_bits_mod**or**frac_digits_value_mod**. Default 0, but by default this gets modified.

**frac_digits_bits_mod**- Modifies the number of fractional digits to show, based on
the length of the significand. There is a certain number of digits that is
the minimum required to explicitly state every bit that is stored, and the
number of digits to show might get set to that number depending on this
option. If "
**IGNORE**" then nothing is done. If "**ATLEAST**" then at least this many digits are shown. If "**ATMOST**" then at most this many digits are shown. If "**EXACTLY**" then exactly this many digits are shown. Default "**ATLEAST**".

**frac_digits_value_mod**- Modifies the number of fractional digits to show, based on
the number of digits required to show the actual value exactly. Works the
same way as
**frac_digits_bits_mod**. Default "**ATLEAST**".

**hex_prefix_string**- The string that is prefixed to hexadecimal digits. Default
"
**0x**". Make it the empty string to suppress the prefix.

**infinite_string**- The string that is returned for an infinite magnitude.
Default "
**inf**".

**nan_string**- The string that is returned for a NaN value. Default
"
**nan**".

**neg_sign**- The string that is prepended to a negative value (including
negative zero). Default "
**-**".

**pos_sign**- The string that is prepended to a positive value (including
positive or unsigned zero). Default "
**+**". Make it the empty string to suppress the positive sign.

**subnormal_strategy**- The manner in which subnormal values are displayed. If
"
**SUBNORMAL**", they are shown with the minimum exponent for normalised values and a significand in the range (0, 1). This matches how they are stored internally. If "**NORMAL**", they are shown with a significand in the range [1, 2) and a lower exponent, as if they were normalised. This gives a consistent appearance for magnitudes regardless of normalisation. Default "**SUBNORMAL**".

**zero_strategy**- The manner in which zero values are displayed. If
"
**STRING=***str*", the string*str*is used, preceded by a sign. If "**SUBNORMAL**", it is shown with significand zero and the minimum normalised exponent. If "**EXPONENT=***exp*", it is shown with significand zero and exponent*exp*. Default "**STRING=0.0**". An unsigned zero is treated as having a positive sign.

- hex_float(STRING)
- Generates and returns a floating point value from a string
encoding it in hexadecimal. The standard input form is "[
*s*][**0x**]*m*[**.***mmmmm*][**p***eee*]", where "*s*" is the sign, "*m*[**.***mmmm*]" is a (fractional) hexadecimal number, and "*eee*" an optionally-signed exponent in decimal. If present, the exponent identifies a power of two (not sixteen) by which the given fraction will be multiplied.*s*]**inf**[**inity**]" returns an infinity, or "die"s if there are no infinities. "[*s*][**s**]**nan**" returns a NaN, or "die"s if there are no NaNs available.

- float_id_cmp(A, B)
- This is a comparison function supplying a total ordering of
floating point values. A and B must both be floating point values. Returns
-1, 0, or +1, indicating whether A is to be sorted before, the same as, or
after B.

- totalorder(A, B)
- This is a comparison function supplying a total ordering of
floating point values. A and B must both be floating point values. Returns
a truth value indicating whether A is to be sorted before-or-the-same-as
B. That is, it is a <= predicate on the total ordering. The ordering is
the same as that provided by "float_id_cmp": NaNs come first,
followed by negative infinity, then negative finite values, then negative
zero, then positive (or unsigned) zero, then positive finite values, then
positive infinity.

- pow2(EXP)
- EXP must be an integer. Returns the value two the the power EXP. "die"s if that value cannot be represented exactly as a floating point value. The return value may be either normalised or subnormal.

- mult_pow2(VALUE, EXP)
- EXP must be an integer, and VALUE a floating point value. Multiplies VALUE by two to the power EXP. This gives exact results, except in cases of underflow and overflow. The range of EXP is not constrained. All normal floating point multiplication behaviour applies.

- copysign(VALUE, SIGN_FROM)
- VALUE and SIGN_FROM must both be floating point values.
Returns a floating point value with the magnitude of VALUE and the sign of
SIGN_FROM. If SIGN_FROM is an unsigned zero then it is treated as
positive. If VALUE is an unsigned zero then it is returned unchanged. If
VALUE is a NaN then it is returned unchanged. If SIGN_FROM is a NaN then
the sign copied to VALUE is unpredictable.

- nextup(VALUE)
- VALUE must be a floating point value. Returns the next
representable floating point value adjacent to VALUE with a numerical
value that is strictly greater than VALUE, or returns VALUE unchanged if
there is no such value. Infinite values are regarded as being adjacent to
the largest representable finite values. Zero counts as one value, even if
it is signed, and it is adjacent to the smallest representable positive
and negative finite values. If a zero is returned, because VALUE is the
smallest representable negative value, and zeroes are signed, it is a
negative zero that is returned. Returns NaN if VALUE is a NaN.

- nextdown(VALUE)
- VALUE must be a floating point value. Returns the next
representable floating point value adjacent to VALUE with a numerical
value that is strictly less than VALUE, or returns VALUE unchanged if
there is no such value. Infinite values are regarded as being adjacent to
the largest representable finite values. Zero counts as one value, even if
it is signed, and it is adjacent to the smallest representable positive
and negative finite values. If a zero is returned, because VALUE is the
smallest representable positive value, and zeroes are signed, it is a
positive zero that is returned. Returns NaN if VALUE is a NaN.

- nextafter(VALUE, DIRECTION)
- VALUE and DIRECTION must both be floating point values.
Returns the next representable floating point value adjacent to VALUE in
the direction of DIRECTION, or returns DIRECTION if it is numerically
equal to VALUE. Infinite values are regarded as being adjacent to the
largest representable finite values. Zero counts as one value, even if it
is signed, and it is adjacent to the positive and negative smallest
representable finite values. If a zero is returned and zeroes are signed
then it has the same sign as VALUE. Returns NaN if either argument is a
NaN.

2017-08-28 | perl v5.26.0 |