Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^4: Can I access and use UV types from perl?

by Don Coyote (Hermit)
on Nov 17, 2019 at 20:19 UTC ( [id://11108828]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Can I access and use UV types from perl?
in thread Can I access and use UV types from perl?

The main sense I am getting from this, is that Perl is all about the storage.

bignum still appears to store the value as an IV though. and converting a number into a rat is the same problem only stepped up to a relationship between Rationals and Naturals, the Natural numbers now being a subset of Rationals rather than a subset of Integers.

#use bignum; =head1 output SV = PVAV(0x6fb404) at 0x6fa59c REFCNT = 1 FLAGS = () ARRAY = 0x2ce1c64 FILL = 1 MAX = 3 ARYLEN = 0x0 FLAGS = (REAL) Elt No. 0 SV = IV(0x25bfd30) at 0x25bfd34 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 4967296 Elt No. 1 SV = IV(0x6fa578) at 0x6fa57c REFCNT = 1 FLAGS = (IOK,pIOK) IV = 429 =cut

Interestingly when I was hacking MvT attempting to use hashkeys to represent natural numbers i kept getting (FAKE) flags, maybe there's something in that. Also storing bitfields as hashkeys has unexpected results.

It would seem that some kind of overloading solution may be the way to go, let Perl handle the storage, but overload the operations with XSUBS that do type checking/coercion. Or even straight up Inlining C.

Replies are listed 'Best First'.
Re^5: Can I access and use UV types from perl? (updated)
by haukex (Archbishop) on Nov 17, 2019 at 20:36 UTC
    bignum still appears to store the value as an IV though.

    No, they're objects*. See the output of perl -Mbignum -MDevel::Peek -e 'my $x=1; Dump($x)' or perl -Mbignum -MData::Dump -e 'my $x=1; dd($x)'.

    It would seem that some kind of overloading solution may be the way to go, let Perl handle the storage, but overload the operations with XSUBS that do type checking/coercion. Or even straight up Inlining C.

    Well, again, if you don't tell us what for, we can't really comment.

    * Update: I guess you maybe mean internally? The point is, how they're stored internally is abstracted and encapsulated in an object, and you shouldn't worry about it. bignum will allow you to work with numbers of any size and any precision without any loss due to the usual floating-point imprecisions, overflows, etc.

      for the greater understanding and enoblement of homus erectus.

      That and, well one reason may be to help with the documentation on the Rat() function in Perl 6. By understanding what Perl is doing with numbers at the level of the Naturals we can possibly figure out how to convert the Rationals into Reals and update the Docs accordingly.

      Well that and just sanity in general.

        That and, well one reason may be to help with the documentation on the Rat() function in Perl 6.

        My understanding from a quick look at the Raku (formerly known as Perl 6) documentation is:

        • int is a native integer, so like Perl's IV
        • Int is like Perl's Math::BigInt (bignum; note that enables Math::BigFloats too)
        • Num is a native float, so like Perl's NV
        • Rat is basically like two integers for numerator and denominator, overflowing causes it to be cast to Num (which seems like a downgrade to me)
        • FatRat is like Perl's Math::BigRat (bigrat)

        I don't see an equivalent of Math::BigFloat in Raku at the moment.

        By understanding what Perl is doing with numbers at the level of the Naturals

        I think Perl's data types are much more closely aligned with the underlying native types than with mathematical concepts like natrual numbers, whole numbers, etc.

        we can possibly figure out how to convert the Rationals into Reals

        According to the docs, role Rational[::NuT, ::DeT] does Real { ... } already...?

Re^5: Can I access and use UV types from perl?
by hippo (Bishop) on Nov 17, 2019 at 23:48 UTC
    The main sense I am getting from this, is that Perl is all about the storage.

    My sense is that Perl is all about the DWIM. What I haven't seen so far (and apologies if I've missed it) is where your problem lies. Here's a simple example:

    #!/usr/bin/env perl use strict; use warnings; use Test::More tests => 1; my $x = 3; my $y = 2; is $x + $y, 5, 'Yes, your integers behave exactly as expected';

    What else do you expect to happen?

      well, and this may a little bit controversial, but...

      #!perl use strict; use warnings; use Test::More tests => 1; my $x = 5; my $y = 3; is $x / $y, undef, 'Yes, your integers behave exactly as expected'; =head1 output 1..1 not ok 1 - yes your integers behave as expected # Failed test 'yes your integers behave as expected' # at -e line 1. # got: '1.66666666666667' # expected: undef # Looks like you failed 1 test of 1. =cut

        This is what I would tend to expect (using integer):

        c:\@Work\Perl\monks>perl -le "use strict; use warnings; ;; use integer; ;; use Test::More tests => 2; ;; my $x = 5; my $y = 3; ;; is $x / $y, 1, qq{$x / $y == 1 is expected}; is $y / $x, 0, qq{$y / $x == 0 is expected}; " 1..2 ok 1 - 5 / 3 == 1 is expected ok 2 - 3 / 5 == 0 is expected


        Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11108828]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-03-28 10:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found