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

I'm far from an expert on internals, but IIRC, the "internal representation" of a number in Perl is as a binary 64-bit IEEE-754 float. You've only been looking at 8 bits (B8) of the 64. Note that the positive and negative numbers below differ in one single bit in their binary FP representations.

c:\@Work\Perl\monks>perl -wMstrict -le "my @ra = (0.000008, -0.000008); ;; for my $fpn (@ra) { my $pfpn = pack 'F', $fpn; printf qq{%8s: length when F-packed: %d bytes \n}, $fpn, length $pf +pn; } ;; for my $fpn (@ra) { my $pfpn = pack 'F', $fpn; printf qq{%8s: hex '%s' \n}, $fpn, unpack 'H*', $pfpn; } ;; for my $fpn (@ra) { my $pfpn = pack 'F', $fpn; printf qq{%8s: bits '%s' \n}, $fpn, unpack 'B*', $pfpn; } " 8e-006: length when F-packed: 8 bytes -8e-006: length when F-packed: 8 bytes 8e-006: hex '8dedb5a0f7c6e03e' -8e-006: hex '8dedb5a0f7c6e0be' 8e-006: bits '100011011110110110110101101000001111011111000110111000 +0000111110' -8e-006: bits '100011011110110110110101101000001111011111000110111000 +0010111110'

Update: See Wikipedia article IEEE floating point. (Update: Oh, and also see What Every Computer Scientist Should Know About Floating-Point Arithmetic and What Every Computer Scientist Should Also Know About Floating-Point Arithmetic — how could I forget?)

Update 2: Changed code example to include hex unpacking.

  • 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 20, 2014 at 10:51 UTC

    Hello AnomalousMonk,

    Thank you for your time and effort, it start to makes sense what you said. The only problem is that I would like to have an 8 bit negative/positive floating point.

    But I will try to make it work for 64 bit and maybe work my way up from there.

    Thanks again.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      I would like to have an 8 bit negative/positive floating point.

      You want an 8-bit floating point value. Why?

      A floating point value consists of 3 parts:

      1. 1-bit to indicate the sign.
      2. A number of the remaining bits denote the scale of the value. (The exponent, characteristic or scale.)

        This scales the size of the value, and 1-bit indicates whether it is either bigger or smaller.

      3. A number of bits that represent the actual value. (The significand, coefficient or the mantissa.)

        These bits define the precision of the values tha can be represented.

      Assuming that you go for 4 bits of precision, that leaves 3 bits for scaling, means you could represent numbers between +/- 0.001 and +/- 150, which is a pretty useless format.


      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".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Hello BrowserUk,

        Thank you for your time and effort reading and replying to my question. Well I was planning to reach extreme timing accuracy but it does not make any sense. The number as you clearly pointed out will be extremely small with no point into it.

        At this point I should look for 32 bit positive and negative floating point conversions and stop thinking about such a small number since it will not result to anything useful, since the number will be so extremely small.

        Thank you, again for your time and effort reading and replying to my question.

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

      An eight-bit floating point representation would seem to offer an extremely limited dynamic range, but whatever floats your boat... According to the WP article referenced above, IEEE-754 already has a "binary16" Half-precision floating-point format definition; maybe that would be of use.

        Hello Anonymous Monk,

        Thank you for your time and effort reading and replying to my question. At this point and based on all the answers I assume that I was asking for something so extreme that has no use. Since even Half precision floating points (16 bit) are only used for accuracy not for calculations.

        I was asking for something extreme, I should start thinking about conversion of floating points in 32 bit. I assume that would be easier to get and achieve.

        Thank you for your time and effort to assist me with my question.

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

      So you want a floating-point number packed into 8 bits? Why?

      I'm not sure if there is a standardized format for those, since they're not all too useful, I've only found them referenced in tutorials and university courses on floating-point numbers for students to practice on (e.g. http://sandbox.mc.edu/~bennet/cs110/textbook/module4_2.html or http://www.toves.org/books/float/#s2 ). Wikipedia has an article on Minifloats. Perl doesn't have a built-in conversion for that.

      The smallest standardized IEEE format is 16 bits, and even those have their limitations.

        Hello Anonymous Monk,

        Thank you for your time and effort reading and replying to my question. At this point I assume that I made a mistake since it has been proven that even the smallest decimal number is up to 16 bits. Which is only used for extreme precision, not even for calculations reasons.

        I assume that my assumptions that I could get a floating point with 8 bits for even higher precision is useless.

        I think even 32 bits will do the work for my task.

        Thank you for your time and effort to help me with my question.

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

        Hello Anonymous Monk,

        First of all I want to say thank you for your time and effort reading and replying to my question. Sorry for the late reply but I was out of network area.

        To be honest I wanted to experiment with binary number and try to build my library with all positive and negative variations of decimals.

        Based on the replies that I got from my question I suppose that 8 bit decimal integer will not result to something useful. So probably that small number does not even exist.

        Than I should reconsider this part and possibly work with 32 bit floating points (positive/negative) that possibly exist.

        Again thank you for your time and effort.

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