vector - Is it possible to store floating point numbers using perl's vec() function? -


I have a Perl script that processes millions of performance data, so I need to store metric information efficiently. I searched the function of Pearl, which allows you to manipulate the bits of the string directly, it can be used to simulate a array memory efficiently.

And it works very well to store integer values. But for floating point values, it does not work very well.

Here's an example:

  #! / Usr / bin / perl my ($ s) = ''; Vec ($ s, 0, 32) = 1234; Vec ($ s, 1, 32) = 12.34; Vec ($ s, 2, 32) = pack ('f', 12.34); Print "First vec:". Vec ($ s, 0, 32) "(must be '1234') \ n"; Print "2 VCC:" Vec ($ s, 1, 32) "('12 .34 ') \ n"; Print "3 vc:" unpack ('f', vce ($ s, 2, 32)) "(must be '.34') \ n";   

To run this code, on my machine (Mac OS X 10.6.7, Pearl 5.8.9) gives the following:

  First vec : 1234 (must be '1234') second vec: 12 ('12 .34 ') 3 VC: (must be '12 .34')   

As you can see In the simplest case, simply perl floating point number to the nearest absolute integer, using pack () / unpack () I also tried to be fancy It's all bits out of zero The Purchaser.

I have tried many different changes, increasing the #bits, googling around, etc. have no advantage. It really feels like it should work, because at the end of the day, it's all just bits.

Thank you.

vec writes an integer only, which is why 12.34 in the second example Converted to 12. In the third example, the pack string is converted to 0, and unpack ('F', 0) fails.

, 12.34)); Print Unpack ('F', Pack ('L', Week ($ S, 2, 32)), "\ n";


However, it is best to use the roam through Wake / Faster, and instead use 4 logic versions of substr Specify (Expr, Offset, Length, Replacement):

  Abstract ($ s, 2 * 4, 4, pack ("f", 12.34)); Print unpack ("F", substrate ($ S, 2 * 4)), "\ n";    

Comments