Data-Float-DoubleDouble

 view release on metacpan or  search on metacpan

DoubleDouble.pm  view on Meta::CPAN

# 3) exponent
# For nan/inf, the mantissa is 'nan' or 'inf' respectively unless
# 2nd arg is literally 'raw'.

sub float_B {
  my $hex = NV2H($_[0]);

  my $raw = @_ == 2 && $_[1] eq 'raw' ? 1 : 0;

  # If the 2nd arg is 'raw' we do the calculations for the arg
  # even if it is an Inf/NaN.
  unless($raw) {
    if($hex eq '7ff00000000000000000000000000000') { # +inf
      return ('+', 'inf', 1024);
    }
    if($hex eq 'fff00000000000000000000000000000') { # -inf
      return ('-', 'inf', 1024);
    }
    if($hex eq '7ff80000000000000000000000000000') { # + nan
      return ('+', 'nan', 1024);
    }

DoubleDouble.pm  view on Meta::CPAN

  die "Wrong number of args to B_float (", scalar @_, ")"
    unless @_ == 3;

  my $hex = B2float_H(@_);
  return H_float($hex);
}

##############################
##############################
# Return a hex string representation as per perl Data::Float
# For NaN and Inf returns 'nan' or 'inf' (prefixed with either
# '+' or '-' as appropriate) unless an additional arg of 'raw'
# has been provided - in which case it does the calculations
# and returns the hex string it has calculated.
# This function returns a hex representation of the *actual*
# value - even if that value requires more than 106 bits.

sub float_H {
  my ($sign, $mant, $exp);

  if(@_ == 1)            {($sign, $mant, $exp) = float_B($_[0])}

DoubleDouble.pm  view on Meta::CPAN

      return 0;
    }
  }

  return 1;

}

##############################
##############################
# Return true iff at least one argument is a NaN.

sub are_nan {

  for(@_) {
    return 0 if $_ == $_;
  }

  return 1;

}

DoubleDouble.pm  view on Meta::CPAN

   If a second arg is provided, it must be the string 'raw' - in
   which case inf/nan mantissas will be returned in hex format
   instead of as "inf"/"nan" strings.

  #############################################

  @bin = NV2binary($nv);

   Another way of arriving at (almost) the same binary representation
   of the NV -ie as an array consisting of (sign, mantissa, exponent).
   The mantissa if Infs and NaNs will be returned as 'inf' or 'nan'
   respectively and the sign associated with the nan will always
   be '+'.
   With this function, trailing zeroes are stripped from the mantissa
   and exponents for 0, inf and nan might not match the other binary
   representations.
   This function is based on code from the mpfr library's
   tests/tset_ld.c file.

  #############################################

DoubleDouble.pm  view on Meta::CPAN


   Returns true if and only if all of the (NV) arguments are
   infinities.
   Else returns false.

  #############################################

  $bool = are_nan(@nv); # Aliased to float_is_nan.

   Returns true if and only if all of the (NV) arguments are
   NaNs. Else returns false.

  #############################################

  $hex = dd_bytes($nv);

   Returns same as NV2H($nv).

  #############################################

  For Compatibility with Data::Float:

DoubleDouble.pm  view on Meta::CPAN

  $class = float_class($nv);

   Returns one of either "NAN", "INFINITE", "ZERO", "NORMAL"
   or "SUBNORMAL" - whichever is appropriate. (The NV must
   belong to one (and only one) class.

  #############################################

  $bool = float_is_nan($nv); # Alias for are_nan()

   Returns true if $nv is a NaN.
   Else returns false.

  #############################################

  $bool = float_is_infinite($nv); # Alias for are_inf()

   Returns true if $nv is infinite.
   Else returns false.

  #############################################

  $bool = float_is_finite($nv);

   Returns true if NV is neither infinite nor a NaN.
   Else returns false.

  #############################################

  $bool = float_is_nzfinite($nv);

   Returns true if NV is neither infinite, nor a NaN, nor zero.
   Else returns false.

  #############################################

  $bool = float_is_zero($nv);

   Returns true if NV is zero.
   Else returns false.

  #############################################

DoubleDouble.pm  view on Meta::CPAN

  $nv = nextafter($nv1, $nv2);

   $nv1 and $nv2 must both be floating point values. Returns the
   next representable floating point value adjacent to $nv1 in the
   direction of $nv2, or returns $nv2 if it is numerically
   equal to $nv1. 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
   then it has the same sign as $nv1. Returns
   NaN if either argument is a NaN.

  #############################################

  $nv = nextup($nv1);

   $nv1 must be a floating point value. Returns the next representable
   floating point value adjacent to $nv1 with a numerical value that
   is strictly greater than $nv1, or returns $nv1 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 $nv1 is the smallest representable negative
   value, and zeroes are signed, it is a negative zero that is
   returned. Returns NaN if $nv1 is a NaN.

  #############################################

  $nv = nextdown($nv1);

   $nv1 must be a floating point value. Returns the next representable
   floating point value adjacent to $nv1 with a numerical value that
   is strictly less than $nv1, or returns $nv1 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
   $nv 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.

  #############################################
  #############################################

=head1 TODO

   Over time, introduce the features of (and functions provided by)
   Data::Float

=head1 LICENSE

t/16_inf_nan_zero.t  view on Meta::CPAN


if(NV2H($n_inf) eq 'fff00000000000000000000000000000') {print "ok $t\n"}
else {print "not ok $t\n"}
$t++;

my $p_nan = 'inf' / 'inf';
my $n_nan = -('inf' / 'inf');

if(are_nan($p_nan, $n_nan)) {print "ok $t\n"}
else {
  warn "\nNot a NaN: $p_nan $n_nan\n";
  print "not ok $t\n";
}
$t++;

if($p_nan != $p_nan) {print "ok $t\n"}
else {print "not ok $t\n"}
$t++;

if($p_nan != $n_nan) {print "ok $t\n"}
else {print "not ok $t\n"}



( run in 0.239 second using v1.01-cache-2.11-cpan-4d50c553e7e )