Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: How to convert negative and positive floating points to binary and vice versa

by Laurent_R (Canon)
on Sep 20, 2014 at 14:13 UTC ( [id://1101391]=note: print w/replies, xml ) Need Help??


in reply to How to convert negative and positive floating points to binary and vice versa

I wonder why you want to do that. This almost always unneeded in Perl, because Perl usually does the conversion for you. Internally, numbers are used as binary numbers and Perl usually computes with double-precision floating-point values. When you enter a number in decimal format, Perl usually stores it in binary format (well, in some cases, it might initially store it as a string if Perl does not know that it is supposed to be a number, but it will do the conversion when you use that string as a number, for example in an arithmetic operation). When you are trying to "convert" numbers either way, you actually don't convert them, you are only converting string representations of these numbers. When you write:
my $value1 = 255; my $value2 = 0377; # 377 octal, equal to 255 decimal my $value3 = 0xff; # FF hex, same as 255 decimal my $value4 = 0b11111111; # also 255 decimal
all these values are the same to Perl.

The only thing for which I can see the conversions you are trying to do to be useful is for input and output of special representations of those numbers. Or, of course, for training purposes but you will not have to use that very often.

I can remember of one single case where I had to do that in Perl, for modeling a game with white and black stones where is was convenient to represent the game board (and the moves) with 16-bit integers, where, for example, a 0 would represent a white stone and 1 a black stone (still, although my program was using heavily the bit-wise operators, I needed binary representation only for the purpose of displaying conveniently debugging information, the program itself did not need it).

The octal representation is sometimes needed for such thing as Unix file permissions. Sometimes you might use bit vectors and need to see what it looks like, or need to visualize individual bits for other low level operations, but it is still rather uncommon in day to day Perl programming to need to use the binary representation of numbers.

  • Comment on Re: How to convert negative and positive floating points to binary and vice versa
  • Download Code

Replies are listed 'Best First'.
Re^2: How to convert negative and positive floating points to binary and vice versa
by thanos1983 (Parson) on Sep 21, 2014 at 02:41 UTC

    Hello Laurent_R,

    Wow really nice explanation, it helped my to understand many things. Well I was planning to increase the accuracy of a timing process that I want to use, but as everyone pointed out the number that I am going to get is so small that probably will be out of range.

    At this point I think I should look for 32 bit floating points with positive and negative range values. I think they will do my work just fine, and in case that they can I will take it from there and start digging again to my next problem.

    Again I want to say thank you, for your time and effort. You helped me to understand a lot.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-24 18:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found