Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Using the Math::BigFloat->digit() method

by Athanasius (Archbishop)
on Apr 23, 2014 at 03:51 UTC ( [id://1083267]=perlquestion: print w/replies, xml ) Need Help??

Athanasius has asked for the wisdom of the Perl Monks concerning the following question:

I’m using the Math::BigFloat module, and noticed in the documentation’s Synopsis that it has a digit() method:

$x->digit($n); # return the nth digit, counting from right $x->digit(-$n); # return the nth digit, counting from left

But I can’t seem to get it to work:

13:45 >perl -MMath::BigFloat -wE "my $n = Math::BigFloat->new(123); sa +y $n; say $n->digit(-1);" 123 Can't use an undefined value as an ARRAY reference at C:/Perl/Strawber +ry/strawberry-perl-5.18.2.1-64bit-portable/perl/lib/Math/BigInt/Calc. +pm line 1209. 13:45 >p5u v Math::BigFloat Math::BigFloat C:\Perl\Strawberry\strawberry-perl-5.18.2.1-64bit-portable\per +l\lib\Math\BigFloat.pm: 1.998 13:46 >

What am I doing wrong?

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re: Using the Math::BigFloat->digit() method
by kcott (Archbishop) on Apr 23, 2014 at 08:17 UTC

    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

Re: Using the Math::BigFloat->digit() method
by Anonymous Monk on Apr 23, 2014 at 07:08 UTC

    What am I doing wrong?

    I think you found a bug

    $ perl -e " use bignum; my $q = 123.0; print join q{ }, $_, $q->digit( +-$_),$/ for 1 .. 5 ; warn ref $q " 1 1 2 2 3 3 4 0 5 0 Math::BigInt at -e line 1.

    Right, bug is bug

    $ perl -e " use bignum; my $q = 123 + 1/3; print join q{ }, $_, $q->di +git(-$_),$/ for 1 .. 5 ; warn ref $q " Can't use an undefined value as an ARRAY reference at ...lib/Math/BigI +nt/Calc.pm line 1209.

    Not surprising since its not clearly tested in the test suite http://search.cpan.org/grep?cpanid=PJACKLAM&release=Math-BigInt-1.9993&string=%3Edigit&i=1&n=1&C=9

Re: Using the Math::BigFloat->digit() method
by Khen1950fx (Canon) on Apr 23, 2014 at 07:58 UTC
    Just use Math::BigInt:
    #!/usr/bin/perl use v5.10; use strict; use warnings; use Math::BigInt lib => 'FastCalc'; my $n = 123; my $x = Math::BigInt->new($n); say $x; say $x->digit(-1);

      Q: How do I Math::BigFloat->digit() ??

      A: Khen1950fx says Just use Math::BigInt:

      That is pure genius Khen1950fx and completely answer the question, I love you
Re: Using the Math::BigFloat->digit() method
by trizen (Hermit) on Apr 23, 2014 at 13:31 UTC
    In addition to the above solutions, we can also use the as_int method:
    my $n = Math::BigFloat->new(123); say $n->as_int->digit(-1); # prints: 1

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1083267]
Approved by 2teez
Front-paged by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-26 03:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found