More PerlIO documentation.
[p5sagit/p5-mst-13.2.git] / lib / Math / BigRat.pm
index b00aed4..2b5796f 100644 (file)
@@ -9,7 +9,7 @@
 #   _n   : numeraotr (value = _n/_d)
 #   _a   : accuracy
 #   _p   : precision
-#   _f   : flags, used by MBR to flag parts of a rationale as untouchable
+#   _f   : flags, used by MBR to flag parts of a rational as untouchable
 
 package Math::BigRat;
 
@@ -56,7 +56,7 @@ sub isa
 
 sub _new_from_float
   {
-  # turn a single float input into a rationale (like '0.1')
+  # turn a single float input into a rational (like '0.1')
   my ($self,$f) = @_;
 
   return $self->bnan() if $f->is_nan();
@@ -168,7 +168,7 @@ sub new
       $self->{sign} = '+';
       return $self->bnan() if $self->{_n}->{sign} eq $nan ||
                               $self->{_d}->{sign} eq $nan;
-      # inf handling is missing here
+      # handle inf and NAN cases:
       if ($self->{_n}->is_inf() || $self->{_d}->is_inf())
         {
         # inf/inf => NaN
@@ -365,7 +365,7 @@ sub _bzero
 
 sub badd
   {
-  # add two rationales
+  # add two rationals
 
   # set up parameters
   my ($self,$x,$y,@r) = (ref($_[0]),@_);
@@ -410,7 +410,7 @@ sub badd
 
 sub bsub
   {
-  # subtract two rationales
+  # subtract two rationals
 
   # set up parameters
   my ($self,$x,$y,@r) = (ref($_[0]),@_);
@@ -454,7 +454,7 @@ sub bsub
 
 sub bmul
   {
-  # multiply two rationales
+  # multiply two rationals
   
   # set up parameters
   my ($self,$x,$y,@r) = (ref($_[0]),@_);
@@ -570,12 +570,13 @@ sub bmod
 
   # compute $x - $y * floor($x/$y), keeping the sign of $x
 
+  # locally disable these, since they would interfere
   local $Math::BigInt::upgrade = undef;
   local $Math::BigInt::accuracy = undef;
   local $Math::BigInt::precision = undef;
 
   my $u = $x->copy()->babs();
-  # do a "normal" division ($x/$y)
+  # first, do a "normal" division ($x/$y)
   $u->{_d}->bmul($y->{_n});
   $u->{_n}->bmul($y->{_d});
 
@@ -597,7 +598,6 @@ sub bmod
   $x->{sign} = $xsign;                         # put sign back
 
   $x->bnorm()->round(@r);
-  $x;
   }
 
 ##############################################################################
@@ -908,8 +908,8 @@ sub bsqrt
   local $Math::BigInt::upgrade = undef;
   local $Math::BigInt::precision = undef;
   local $Math::BigInt::accuracy = undef;
-  $x->{_d} = Math::BigFloat->new($x->{_d})->bsqrt(@r);
-  $x->{_n} = Math::BigFloat->new($x->{_n})->bsqrt(@r);
+  $x->{_d} = Math::BigFloat->new($x->{_d})->bsqrt();
+  $x->{_n} = Math::BigFloat->new($x->{_n})->bsqrt();
 
   # if sqrt(D) was not integer
   if ($x->{_d}->{_e}->{sign} ne '+')
@@ -921,7 +921,7 @@ sub bsqrt
   if ($x->{_n}->{_e}->{sign} ne '+')
     {
     $x->{_d}->blsft($x->{_n}->{_e}->babs(),10);                # 71/45.1 => 710/45.1
-    $x->{_n} = $x->{_n}->{_n};                         # 710/45.1 => 710/451
+    $x->{_n} = $x->{_n}->{_m};                         # 710/45.1 => 710/451
     }
  
   # convert parts to $MBI again 
@@ -1024,10 +1024,12 @@ sub numify
  
   return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, NaN, etc
 
-  my $t = Math::BigFloat->new($x->{_n});
-  $t->bneg() if $x->is_negative();
-  $t->bdiv($x->{_d});
-  $t->numify();  
+  # N/1 => N
+  return $x->{_n}->numify() if $x->{_d}->is_one();
+
+  # N/D
+  my $neg = 1; $neg = -1 if $x->{sign} ne '+';
+  $neg * $x->{_n}->numify() / $x->{_d}->numify();      # return sign * N/D
   }
 
 sub as_number
@@ -1113,7 +1115,10 @@ sub import
       eval $rc;
       }
     }
-  die ("Couldn't load $MBI: $! $@") if $@;
+  if ($@)
+    {
+    require Carp; Carp::croak ("Couldn't load $MBI: $! $@");
+    }
 
   # any non :constant stuff is handled by our parent, Exporter
   # even if @_ is empty, to give it a chance
@@ -1127,7 +1132,7 @@ __END__
 
 =head1 NAME
 
-Math::BigRat - arbitrarily big rationales
+Math::BigRat - arbitrarily big rationals
 
 =head1 SYNOPSIS
 
@@ -1141,7 +1146,7 @@ Math::BigRat - arbitrarily big rationales
 =head1 DESCRIPTION
 
 Math::BigRat complements Math::BigInt and Math::BigFloat by providing support
-for arbitrarily big rationales.
+for arbitrarily big rationals.
 
 =head2 MATH LIBRARY