Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to convert a floating point number to a 64 bit integer?

Reply
Thread Tools

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

 
 
SamL
Guest
Posts: n/a
 
      03-09-2009
I need to convert a floating point number to a 64 bit integer (long
long). How to do that in perl? Thanks.
 
Reply With Quote
 
 
 
 
SamL
Guest
Posts: n/a
 
      03-09-2009
On Mar 9, 3:04*pm, Ben Morrow <(E-Mail Removed)> wrote:
> Quoth SamL <(E-Mail Removed)>:
>
> > 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);

 
Reply With Quote
 
 
 
 
smallpond
Guest
Posts: n/a
 
      03-09-2009
On Mar 9, 5:05 pm, SamL <(E-Mail Removed)> wrote:
> On Mar 9, 3:04 pm, Ben Morrow <(E-Mail Removed)> wrote:
>
> > Quoth SamL <(E-Mail Removed)>:

>
> > > 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
 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      03-09-2009
On 2009-03-09 21:26, smallpond <(E-Mail Removed)> wrote:
> On Mar 9, 5:05 pm, SamL <(E-Mail Removed)> wrote:
>> On Mar 9, 3:04 pm, Ben Morrow <(E-Mail Removed)> wrote:
>>
>> > Quoth SamL <(E-Mail Removed)>:

>>
>> > > 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
 
Reply With Quote
 
SamL
Guest
Posts: n/a
 
      03-09-2009
On Mar 9, 6:29*pm, "Peter J. Holzer" <(E-Mail Removed)> wrote:
> On 2009-03-09 21:26, smallpond <(E-Mail Removed)> wrote:> On Mar 9, 5:05 pm, SamL <(E-Mail Removed)> wrote:
> >> On Mar 9, 3:04 pm, Ben Morrow <(E-Mail Removed)> wrote:

>
> >> > Quoth SamL <(E-Mail Removed)>:

>
> >> > > 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
What is the point of having 16 bit colour if a computer monitor can only display 8 bit colour? How do you edit 16 bit colour when you can only see 8 bit? Scotius Digital Photography 6 07-13-2010 03:33 AM
32-bit Number * 32-bit Number = 64-bit result Jeff.M Javascript 6 05-04-2009 09:21 PM
Convert from text string "Hello" to floating-point number issue ITrishGuru Java 15 10-03-2007 12:59 PM



Advertisments