Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

integer container for float

by basiliscos (Pilgrim)
on Mar 28, 2016 at 11:21 UTC ( [id://1158943]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,

The business values I operated are floats; meanwhile the module I use to them in shared memory uses integers, that also guarantees the atomicity of writing int-values in shared memory.

Is there any way to pack (not convert) float to integer, without loosing precision? Way# 1

my $packed_integer = int($float * 1_000_000); my $unpacked_float = $packed_integer / 1_000_000;

Obviously, I loose precision here. In C I can do to manipulation with pointers, to get the result I like:

assert(sizeof(int) >= sizeof(float)); float f = ...; int *i_ptr = (int*)&f; int i = *int;

Then I've found can do something like that via pack/upack build-ins in Perl:

my $v = 1/3; my $v2 = unpack("F", pack("j", unpack("j", pack("F", $v)))); print ("v = $v\nv = $v2\n"); # v = 0.333333333333333 # v = 0.333333333333333

Seems fine.

  • Is there any hidden problem with pack/unpack here?
  • How to emulate C's assert in perl, i.e. how to get sizeof:
    die("float cannot be packed into integer") unless ($int_size >= $float_size);
    ?
  • May be there is already some module that does that? If there is no, does it worth to make a small module with pack + assert as above?

Thanks!

WBR, basiliscos.

Replies are listed 'Best First'.
Re: integer container for float
by BrowserUk (Patriarch) on Mar 28, 2016 at 11:38 UTC

    You can check that a native int will hold a native double:

    use Config; die "Int/double size mismatch" unless $Config{ ivsize } == $Config{ nv +size };

    And if you get passed that test, then the following should always work:

    $packedDoubleAsInt = unpack 'Q', pack 'd', 3.14159265358979;; print $packedDoubleAsInt;; 4614256656552045841 $unpacked = unpack 'd', pack 'Q', $packedDoubleAsInt;; print $unpacked;; 3.14159265358979 $unpacked = unpack 'd', pack 'Q', 4614256656552045841;; print $unpacked;; 3.14159265358979

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: integer container for float
by syphilis (Archbishop) on Mar 28, 2016 at 12:44 UTC
    BrowserUk has nicely covered the case where $Config{ivsize} == 8.

    Similarly it's possible to pack/unpack single precision floats when $Config{ivsize} == 4.
    Obviously, you can't use the "Q" template because, according to the docs, that applies only to ivsize of 8. But you can use the "j" (or "J") templates with the "f" template:
    #!perl -l use strict; use warnings; use Config; my $val = 3.625; if($Config{ivsize} == 4) { my $to_int = unpack 'j', pack 'f', $val;; print $to_int; # 1080557568 my $to_float = unpack 'f', pack 'j', $to_int;; print $to_float; # 3.625 } else { print "Can't pack C floats to IV/UV"; }
    Cheers,
    Rob

      Indeed.

      He could even use 2 x 32-bit ints as an intermediary for a double:

      ($lo, $hi) = unpack 'NN', pack 'd', 3.14159265358979;; print $lo, $hi;; 288179284 4213246272 print unpack 'd', pack 'NN', 288179284, 4213246272;; 3.14159265358979

      Or even 4 x 16-bit ints:

      ( $_0, $_1, $_2, $_3 ) = unpack 'n*', pack 'd', exp(1);; print unpack 'd', pack 'n*', $_0, $_1, $_2, $_3;; 2.71828182845905

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re: integer container for float
by Anonymous Monk on Mar 28, 2016 at 16:22 UTC

    Just a side note which probably won't help you any: there's a problem with your C code, too.

    In modern C, one cannot access an object via pointer of incompatible type#1. This is called type punning and it violates aliasing constraints. You can still compile such code with -fno-strict-aliasing, but this is a poor practice. Going via union is to be preferred.

    #1 excepting character type—one may inspect the stored representation of any object through (unsigned char *).

Log In?
Username:
Password:

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

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

    No recent polls found