Math::BigInt::Scalar is only for tests.
[p5sagit/p5-mst-13.2.git] / t / lib / Math / BigRat / Test.pm
1 #!/usr/bin/perl -w
2
3 package Math::BigRat::Test;
4
5 require 5.005_02;
6 use strict;
7
8 use Exporter;
9 use Math::BigRat;
10 use Math::BigFloat;
11 use vars qw($VERSION @ISA $PACKAGE
12             $accuracy $precision $round_mode $div_scale);
13
14 @ISA = qw(Exporter Math::BigRat);
15 $VERSION = 0.03;
16
17 use overload;           # inherit overload from BigRat
18
19 # Globals
20 $accuracy = $precision = undef;
21 $round_mode = 'even';
22 $div_scale = 40;
23
24 my $class = 'Math::BigRat::Test';
25
26 #ub new
27 #{
28 #        my $proto  = shift;
29 #        my $class  = ref($proto) || $proto;
30 #
31 #        my $value       = shift;
32 #       my $a = $accuracy; $a = $_[0] if defined $_[0];
33 #       my $p = $precision; $p = $_[1] if defined $_[1];
34 #        # Store the floating point value
35 #        my $self = Math::BigFloat->new($value,$a,$p,$round_mode);
36 #        bless $self, $class;
37 #        $self->{'_custom'} = 1; # make sure this never goes away
38 #        return $self;
39 #}
40
41 sub bstr
42   {
43   # calculate a BigFloat compatible string output
44   my ($x) = @_;
45
46   $x = $class->new($x) unless ref $x;
47
48   if ($x->{sign} !~ /^[+-]$/)           # inf, NaN etc
49     {
50     my $s = $x->{sign}; $s =~ s/^\+//;  # +inf => inf
51     return $s;
52     }
53
54   my $s = ''; $s = $x->{sign} if $x->{sign} ne '+';     # +3 vs 3
55
56   return $s.$x->{_n} if $x->{_d}->is_one(); 
57   my $output = Math::BigFloat->new($x->{_n})->bdiv($x->{_d});
58   return $s.$output->bstr();
59   }
60
61 sub bsstr
62   {
63   # calculate a BigFloat compatible string output
64   my ($x) = @_;
65
66   $x = $class->new($x) unless ref $x;
67
68   if ($x->{sign} !~ /^[+-]$/)           # inf, NaN etc
69     {
70     my $s = $x->{sign}; $s =~ s/^\+//;  # +inf => inf
71     return $s;
72     }
73
74   my $s = ''; $s = $x->{sign} if $x->{sign} ne '+';     # +3 vs 3
75
76   return $s.$x->{_n}->bsstr() if $x->{_d}->is_one(); 
77   my $output = Math::BigFloat->new($x->{_n})->bdiv($x->{_d});
78   return $s.$output->bsstr();
79   }
80
81 1;