2

I read this question, and I checked it myself.

I use the following snippet:

f = io.open("file.file", "wb")
f:write(1.34)
f:close()

This creates the file, into which 1.34 is written. This is same as : 00110001 00101110 00110011 00110100 , that is binary codes for the digit 1, the decinal point, then 3 and finally 4.

However, I would like to have printed 00111111 10101100 11001100 11001101, which is a true float representation. How do I do so?

Community
  • 1
  • 1
Sean
  • 789
  • 6
  • 26
  • 1
    Use hexafloats: `f:write(('%.13a'):format(1.34))` – Egor Skriptunoff Jul 23 '14 at 17:48
  • I'm not sure I understand the question entirely but you might need something like [lpack](http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/#lpack) (or similar) for this. – Etan Reisner Jul 23 '14 at 17:50
  • @EgorSkriptunoff's answer is not valid for lua 5.1, but is valid in lua 5.2. (I have no idea if it is correct.) – Etan Reisner Jul 23 '14 at 17:51
  • BTW, you can simply store 17 decimal digits of the number `f:write(('%.17g'):format(1.34))` to avoid any rounding errors and receive exactly the same number with `tonumber`. This works in Lua 5.1 – Egor Skriptunoff Jul 23 '14 at 18:23
  • @EgorSkriptunoff and the corresponding read command would be f:read(('%.17g'):format(1.34)) ? – Sean Jul 23 '14 at 19:36
  • @Sean - read command would be `local num = f:read'*n'` – Egor Skriptunoff Jul 23 '14 at 23:26
  • I thumbed you down for using a roundabout way of saying that the string "3.14" was written into the file. You could have said it plainly instead of forcing us to run your code. – Niccolo M. Jul 24 '14 at 10:45
  • @NiccoloM. I believe it is a good practice, as mentioned in Stackoverflow guidlines, to give example of the used code. I personally, when seeing the snippet will unambiguously understand, regardless of formulation of the description of problem, what is happening - without running the code. So moderator please assist me, to understand what it is about the "Forcing" mentioned here. – Sean Jul 24 '14 at 11:47
  • @Sean: I apologize for being rude. I reverted my unjustified thumb down. Mea culpa. You posted simple code. Then, instead of writing `into which "1.34" is written` or `... the string "1.34" is written`, you wrote `into which 1.34 is written`, which necessitated your explaining the nature of this unspecified 1.34, and caused me to question my own understanding of the code. Since I'm impatient in nature I gave up grokking your "windy" explanation and just run the code. My thumb down was retribution. Again I apologize. Probably all people here would find the fault in me, not in you ;-) – Niccolo M. Jul 24 '14 at 21:38
  • (Hmmm... the site won't let me revert my thumbdown. It seems the question has to be edited first. Sorry.) – Niccolo M. Jul 24 '14 at 21:41
  • That is okey. The discussion should serve as a clarification. – Sean Jul 25 '14 at 16:35

1 Answers1

2

You may need to convert it to binary representation, using something similar to this answer. This discussion on serialization of lua numbers may also be useful.

Community
  • 1
  • 1
Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56