in reply to List::Utils can't find max in array of BigInts

That's actually what I'd expect. List::Utils is full of XS code. XS code almost always gets simple things wrong. It almost never handles mundane Perl magic like overloading, which is what bigint.pm is a simple demonstration of.

List::Util comes with non-XS implementations of at least most of its routines and those should have no problems with bigint values. Or you could just write your own 'max' in about 1 or 2 lines of Perl code.

- tye        

  • Comment on Re: List::Utils can't find max in array of BigInts (XS--)

Replies are listed 'Best First'.
Re^2: List::Utils can't find max in array of BigInts (XS--)
by salva (Canon) on Apr 24, 2013 at 07:32 UTC
    But List::Utils does handle magic in the XS version too. The code is there to see... though it may be buggy.

      Having XS code that attempts to deal with even mundane Perl magic is still never something I expect. (Dealing with overloading wasn't something List::Util's XS code even attempted for the first decade of its existence). But, yes, XS code is indeed much, much more likely to be buggy than Perl code. Worse, XS code is much, much more likely to be buggy in ways that you don't notice at first and then cause your code to fail in ways that don't even hint that the XS code is to blame.

      But, you know: speed! It is vitally important that your max() function run a fraction of 1ms faster and increasing the code size by an order of magnitude and the code complexity by two orders of magnitude is surely worth all of the time wasted by everybody when it means that a few scripts are running a few ms faster and reducing their total run time by 0.2%.

      Every professional software developer knows that "trivially faster code" trumps "correct code" every time.

      Me, I tend to care a lot about hidden complexity of code. And so I'm pretty unlikely to use List::Util (especially since v1.23_03: "Dropped the pure-Perl implementation of both Scalar::- and List::Util."). I'll use Perl code because mundane Perl magic gets heavily tested and heavily exercised there and so the odds of me having to dive into the Perl source code because of a bug are much lower.

      - tye        

      “It may be buggy” of course being the operative phrase here ...

      Sometimes pure-Perl brute force is just as good as anything else, because there’s only one way to find the maximum value in an unsorted list, and the overhead of the Perl interpreter running the loop to do that is probably worth ignoring.

Re^2: List::Utils can't find max in array of BigInts (XS--)
by mgatto (Novice) on Apr 25, 2013 at 20:09 UTC

    Dang, even a simple foreach produces the same unblessed reference error:

    my $max_bigint = $result[1]; foreach (@result) { if ($_ > $max_bigint) { $max_bigint = $_; } } say $max_bigint;
    -------
    Can't call method "can" on unblessed reference at C:/Languages/Perl/perl/lib/Math/BigInt.pm line 2693. at trial_division.pl line 12. main::__ANON__('Can\'t call method "can" on unblessed reference at C:/Languag...') called at C:/Languages /Perl/perl/lib/Math/BigInt.pm line 2693 Math::BigInt::objectify(2, 'Math::BigInt', 'ARRAY(0x3faefc)', 'Math::BigInt=HASH(0x3fb1bc)') called at C:/Languages/Perl/perl/lib/Math/BigInt.pm line 1050 Math::BigInt::bcmp('Math::BigInt', 'ARRAY(0x3faefc)', 'Math::BigInt=HASH(0x3fb1bc)') called at C:/Languages /Perl/perl/lib/Math/BigInt.pm line 65 Math::BigInt::__ANON__('Math::BigInt=HASH(0x3fb1bc)', 'ARRAY(0x3faefc)', 1) called at trial_division.pl line 55

    It the exact same stack trace as when I tried to use List::Util::max().