in reply to Re^6: Perl can't make some easy arithmetics :(
in thread Perl can't make some easy arithmetics :(

Maybe it would be better if Perl has such magic precedence: 1) look at the operator, 2) if operator is for numerals (+,++) -> change operands to numerals ("4" -> 4 (change to bigint if used, otherwise to int), "40..(many)..0" -> 40..(many)..0 (change to bigint, if used, otherwise to int)). Now it seems that after looking to operator, if numeric, Perl change operands to int, then make operation, and later convert result to bigint (or not convert?).
  • Comment on Re^7: Perl can't make some easy arithmetics :(

Replies are listed 'Best First'.
Re^8: Perl can't make some easy arithmetics :(
by AnomalousMonk (Archbishop) on Oct 25, 2014 at 00:37 UTC

    Perl already interconverts strings and numbers according to operator context in a manner similar to what you suggest. The bigint module introduces operator overloadings on top of existing Perl operators. These overloadings may or may not be optimal, but that's a discussion you must have with bigint and not with Perl.

      Thanks. Now it is clearer for me than at the beginning of evening.
      If bigint is a standard Perl module (called by "use"), mustn't it fit to Perl DWIM ideology? Isn't DWIM ideology higher than anarchy of modules?


        Consider this script:
        use strict; use warnings; use Devel::Peek; use bigint; my $x = "900000000000000000000000000000000000009"; my $y = 900000000000000000000000000000000000009; #Dump($x); # Shows that $x is a simple perl scalar #Dump($y); # Shows that $y is already a Math::BigInt object. my $m = $x - 2; my $n = $x - "2"; if($m == $n) {print "ok\n"} else {print "$m\n$n\n"} # Outputs: # 900000000000000000000000000000000000007 # 9e+038
        Question 1:
        If $y is automatically a Math::BigInt object, then why not the same for $x ?
        Answer:
        Because then there would be no way to assign a string of numbers as simply "a string of numbers" - such a string would automatically be converted into a Math::BigInt object (and there's nothing to suggest you want that to happen).

        That has relevance to the code you posted in your original script because when you read from STDIN, you're reading a *string*.

        Question 2:
        Why should $x - 2 and $x - "2" return different values ?
        Answer:
        ... I don't have a satisfactory explanation of that, and you might like to query it (via a perlbug report) with p5p.

        UPDATE: OP did file a bug report with p5p, and Lucas Mai pointed out that with $x - 2 the bareword 2 gets automatically turned into a Math::BigInt object and then gets subtracted from the string $x (thanks to Math::BigInt overloading), returning a Math::BigInt object.
        But with $x - "2" there's nothing happening that will create any Math::BigInt object, so it's just "string - string" which, of course, produces different results. It's obvious when you look at it the right way ... but the effect still seems odd to me.
        /UPDATE


        That's also of relevance to the code you posted because your $a - $b is "string - string", just the same as my $x - "2" (which also fails to upgrade either argument to a Math::BigInt object).

        It's issues like these that strongly deter me from using bigint.
        IMO, if you're wanting a pure-perl solution for your bigint handling then you're better off using Math::BigInt explicitly.

        And if you want efficient bignum handling you should just forget about *pure-perl* solutions altogether and look at using modules such as Math::GMP, Math::GMPz (plug), Math::GMPq (plug), Math::MPFR (plug) and Math::Pari.

        Cheers,
        Rob
        Being core mostly means being old and old sometimes means immature because of the limitations of backwards compatibility.

        In theory modules should evolve for years in CPAN before being accepted to core.

        This would have meant Perl being distributed without standard modules for years, hence not gaining the acceptance which brought us together to discuss its limitations .

        Cheers Rolf

        (addicted to the Perl Programming Language and ☆☆☆☆ :)