in reply to Using the Math::BigFloat->digit() method

G'day Athanasius,

"What am I doing wrong?"

I don't think you're doing anything wrong: this looks like a bug.

I have a different Perl version to you: "(v5.18.1) built for darwin-thread-multi-2level". I have the same (builtin) version of Math::BigFloat as you; you might like to check the versions of the other modules involved. Here's what I've got:

Math::BigFloat 1.998 Math::BigInt 1.9991 Math::BigInt::Calc 1.997

[In the following code from various modules, I've removed a fair amount of comments and whitespace (and, in some cases, code); just leaving what seems relevant to this discussion. Also, when comparing your code with module code, I've replaced your $n with $YOUR_n to avoid ambiguity.]

The code where the error occurs is in Math::BigInt::Calc:

sub _len { my $cx = $_[1]; (@$cx-1)*$BASE_LEN+length(int($cx->[-1])); # <--- line 1209 }

So, $cx is NOT an arrayref; it's undef. _len() is called by _digit() (which is defined just below _len()):

sub _digit { my ($c,$x,$n) = @_; my $len = _len('',$x); ... }

So, $x here is assigned to $cx in _len(); therefore, $x is undef. _digit() is called by Math::BigInt::digit() — see below for how we get to this method.

Math::BigFloat doesn't have a digit() method; it does have:

@ISA = qw/Math::BigInt/;

Math::BigInt has:

... my $CALC = 'Math::BigInt::Calc'; ... sub digit { my ($self,$x,$n) = ref($_[0]) ? (undef,@_) : objectify(1,@_); $n = $n->numify() if ref($n); $CALC->_digit($x->{value},$n||0); }

When you call "$YOUR_n->digit(-1)", $_[0] (i.e. $YOUR_n) is a reference and the first line evaluates as:

my ($self,$x,$n) = (undef, $YOUR_n, -1);

$n, assigned from $_[1] (i.e. -1), is not a reference so "$n = $n->numify()" isn't executed (i.e. $n remains -1).

$x, assigned from $YOUR_n, has no 'value' hash key. This means the third line is evaluated as:

Math::BigInt::Calc->_digit(undef, -1);

I suspect either the 'value' hash key should have been assigned earlier or a different hash key should have been used instead of 'value'. I haven't investigated this further.

Here's a short test demonstrating the issue:

#!/usr/bin/env perl use 5.010; use strict; use warnings; use Math::BigFloat; my $n = Math::BigFloat->new(123); say $n; say 'REF $n: ', ref $n; eval { say $n->digit(-1); }; warn $@ if $@; say '$n->{value} EXISTS: ', exists $n->{value} ? 'Yes' : 'No'; say '$n->{value} DEFINED: ', defined $n->{value} ? 'Yes' : 'No'; eval { $n->{value} = [123]; say '$n->digit(-1): ', $n->digit(-1); say '$n->digit(1): ', $n->digit(1); say '$n->digit(): ', $n->digit(); }; warn $@ if $@;

Output:

123 REF $n: Math::BigFloat Can't use an undefined value as an ARRAY reference at /Users/ken/perl5 +/perlbrew/perls/perl-5.18.1t/lib/5.18.1/Math/BigInt/Calc.pm line 1209 +. $n->{value} EXISTS: No $n->{value} DEFINED: No $n->digit(-1): 1 $n->digit(1): 2 $n->digit(): 3

I'll leave you to verify that and, if correct, raise a bug report.

-- Ken