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


In reply to Re: Using the Math::BigFloat->digit() method by kcott
in thread Using the Math::BigFloat->digit() method by Athanasius

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.