How to convert a floating point number to a 64 bit integer?

Discussion in 'Perl Misc' started by SamL, Mar 9, 2009.

  1. SamL

    SamL Guest

    I need to convert a floating point number to a 64 bit integer (long
    long). How to do that in perl? Thanks.
     
    SamL, Mar 9, 2009
    #1
    1. Advertising

  2. SamL

    SamL Guest

    On Mar 9, 3:04 pm, Ben Morrow <> wrote:
    > Quoth SamL <>:
    >
    > > I need to convert a floating point number to a 64 bit integer (long
    > > long). How to do that in perl? Thanks.

    >
    > You will need to specify a little more about where these numbers are
    > coming from. Probably you want perldoc -f pack.
    >
    > Ben


    The number comes from calculation. For example:

    my $a = 2.0**37;

    Here I cannot use 2**37 directly since the result is -1.

    I have found a way to "convert" it although I think there should be a
    better way:

    my $b = sprintf ("%.0f", $a);
     
    SamL, Mar 9, 2009
    #2
    1. Advertising

  3. SamL

    smallpond Guest

    On Mar 9, 5:05 pm, SamL <> wrote:
    > On Mar 9, 3:04 pm, Ben Morrow <> wrote:
    >
    > > Quoth SamL <>:

    >
    > > > I need to convert a floating point number to a 64 bit integer (long
    > > > long). How to do that in perl? Thanks.

    >
    > > You will need to specify a little more about where these numbers are
    > > coming from. Probably you want perldoc -f pack.

    >
    > > Ben

    >
    > The number comes from calculation. For example:
    >
    > my $a = 2.0**37;
    >
    > Here I cannot use 2**37 directly since the result is -1.
    >
    > I have found a way to "convert" it although I think there should be a
    > better way:
    >
    > my $b = sprintf ("%.0f", $a);


    use bigint;
    $a = 2.0**37;
    print int($a),"\n";

    137438953472
     
    smallpond, Mar 9, 2009
    #3
  4. On 2009-03-09 21:26, smallpond <> wrote:
    > On Mar 9, 5:05 pm, SamL <> wrote:
    >> On Mar 9, 3:04 pm, Ben Morrow <> wrote:
    >>
    >> > Quoth SamL <>:

    >>
    >> > > I need to convert a floating point number to a 64 bit integer (long
    >> > > long). How to do that in perl? Thanks.

    >>
    >> > You will need to specify a little more about where these numbers are
    >> > coming from. Probably you want perldoc -f pack.

    >>

    That was my interpretation of the question, too. But it seems I was
    mistaken.


    >> The number comes from calculation. For example:
    >>
    >> my $a = 2.0**37;
    >>
    >> Here I cannot use 2**37 directly since the result is -1.
    >>
    >> I have found a way to "convert" it although I think there should be a
    >> better way:
    >>
    >> my $b = sprintf ("%.0f", $a);


    The result of this is a string, not a 64 bit integer.

    > use bigint;
    > $a = 2.0**37;
    > print int($a),"\n";
    >
    > 137438953472


    And the result of this is a bigint, which is also not the same as a 64
    bit integer.

    Perl can also be compiled to use 64 bit integers on most platforms.

    But the real question is: What is the 64 bit integer for? Until
    this is known, there is no way to tell whether a real 64 bit int is
    needed or string or a bigint (or
    maybe even a float) can be used instead.

    hp
     
    Peter J. Holzer, Mar 9, 2009
    #4
  5. SamL

    SamL Guest

    On Mar 9, 6:29 pm, "Peter J. Holzer" <> wrote:
    > On 2009-03-09 21:26, smallpond <> wrote:> On Mar 9, 5:05 pm, SamL <> wrote:
    > >> On Mar 9, 3:04 pm, Ben Morrow <> wrote:

    >
    > >> > Quoth SamL <>:

    >
    > >> > > I need to convert a floating point number to a 64 bit integer (long
    > >> > > long). How to do that in perl? Thanks.

    >
    > >> > You will need to specify a little more about where these numbers are
    > >> > coming from. Probably you want perldoc -f pack.

    >
    > That was my interpretation of the question, too. But it seems I was
    > mistaken.
    >
    > >> The number comes from calculation. For example:

    >
    > >> my $a = 2.0**37;

    >
    > >> Here I cannot use 2**37 directly since the result is -1.

    >
    > >> I have found a way to "convert" it although I think there should be a
    > >> better way:

    >
    > >> my $b = sprintf ("%.0f", $a);

    >
    > The result of this is a string, not a 64 bit integer.
    >
    > > use bigint;
    > > $a = 2.0**37;
    > > print int($a),"\n";

    >
    > > 137438953472

    >
    > And the result of this is a bigint, which is also not the same as a 64
    > bit integer.
    >
    > Perl can also be compiled to use 64 bit integers on most platforms.
    >
    > But the real question is: What is the 64 bit integer for? Until
    > this is known, there is no way to tell whether a real 64 bit int is
    > needed or  string or a bigint (or
    > maybe even a float) can be used instead.
    >
    >         hp


    Sorry, I did not make it clear. Actually I just need a string which is
    the result of 2.0**37. So the simplest solution, as Ben said, is

    perl -le'print 2.0**37'

    That solved my problem. Thanks.
     
    SamL, Mar 9, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mantorok Redgormor

    integer and floating-point bit pattern

    Mantorok Redgormor, Oct 1, 2003, in forum: C Programming
    Replies:
    5
    Views:
    807
    Mike Wahler
    Oct 3, 2003
  2. ITrishGuru
    Replies:
    15
    Views:
    633
    Andrew Thompson
    Oct 3, 2007
  3. FPGA
    Replies:
    5
    Views:
    1,291
    Thomas Stanka
    Jan 8, 2008
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,339
    Saraswati lakki
    Jan 6, 2012
  5. Jeff.M
    Replies:
    6
    Views:
    178
    Lasse Reichstein Nielsen
    May 4, 2009
Loading...

Share This Page