in reply to Re^2: Trying to translate overflowing JS code to Perl
in thread Trying to translate overflowing JS code to Perl

Instead of "long", you should specify a return type of "int32_t" - tho +ugh I think you'll get away with specifying "int" (thereby avoiding t +he typemap issue that NERDVANA mentioned), which is the same as "int3 +2_t" everywhere.

Thanks for the suggestion (and in the other threads too!). But when I specify a int32_t as return type to NERDVANA's function via Inline::C it compiles fine but Perl complains that it can not find that function. I think it checks the signatures and does not consider a function with return as int32_t as a proper candidate. It works fine for int (and long) return type.

Replies are listed 'Best First'.
Re^4: Trying to translate overflowing JS code to Perl
by syphilis (Archbishop) on Dec 01, 2023 at 11:31 UTC
    It works fine for int (and long) return type

    For me, on Windows 11 and Ubuntu-22.04, both of which are running perl-5.38.0, the last 2 tests of the Math::AnyInt test suite are failing because:
    not ok 9 - 1073741824 + 1073741824 : got -2147483648, expected 2147483 +648. # Failed test '1073741824 + 1073741824 : got -2147483648, expected 2 +147483648.' # at ../math-anyint/test.pl line 28. # got: '-2147483648' # expected: '2147483648' not ok 10 - Testing NERDVANA's C additon calc() # Failed test 'Testing NERDVANA's C additon calc()' # at ../math-anyint/test.pl line 34. # got: '-2147483648' # expected: '2147483648' 1..10 # Looks like you failed 2 tests of 10.
    I'm wondering why that is.
    Looks like you're right - seems it not the reason I tendered. I was too lazy to start up the Ubuntu box, and on Windows both "int" and "long" are always 32 bits so the result will always be the same no matter which type is picked. So I took a (bad) guess.

    Do you know why you and I are getting different results ?
    Are we hitting undefined (or implementation defined) behaviour ?

    Here's a simple Inline::C script that tests for both "int" and "long" inputs and outputs:
    use strict; use warnings; use Inline C => <<'EOC'; /* As the function appears in AnyInt.pm */ long calc_32bit_signed_in_C(long input) { return (int32_t)((int32_t)input); } /* The same function, renamed to begin with an underscore, and return type altered to int */ int _calc_32bit_signed_in_C(int input) { return (int32_t)((int32_t)input); } EOC print calc_32bit_signed_in_C(1073741824 + 1073741824), "\n"; print _calc_32bit_signed_in_C(1073741824 + 1073741824), "\n"; __END__ Outputs: -2147483648 -2147483648
    But you're Math::IntAny test script implies that you are seeing:
    2147483648 2147483648
    Cheers,
    Rob
        The test's expected value is what JS outputs (therefore what I expect if I want to simulate JS behaviour)

        I missed that - more likely through my inattention to detail, than anything else.
        I had also missed that the unpack/pack approach also fails with the same values.

        The integer pragma on a 32-bit perl seemed pretty good, but it fails to emulate on some expressions.
        For example, the expression 1073741824+1073741824 needs to be evaluated outside the integer pragma, whereas the expression 1169367104 << 5 needs to be evaluated inside the integer pragma.

        Cheers,
        Rob