lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi everyone,
Lua-5.2 will have support for hexadecimal floats.

Please, find attached a patch against LuaJIT-2.0 HEAD  [1] that adds hex floats.
You can use "%A" or "%a" format specifier to string.format to
serialize Lua number to its exact hexadecimal string representation.
You can also read it back from the floating point hex representation.

Francesco, please, consider including this patch into GSL Shell.

sh$ luajit
LuaJIT 2.0.0-master.LR -- Copyright (C) 2005-2011 Mike Pall. http://luajit.org/
JIT: ON CMOV SSE2 SSE3 SSE4.1 fold cse dce fwd dse narrow loop abc fuse
> =("%a"):format(1.0987654321e-20)
0x1.9f1a12718ed76p-67
> = 0x1.9f1a12718ed76p-67
1.0987654321e-20
> =tonumber("0x1.9f1a12718ed76p-67")
1.0987654321e-20
>

--Leo--

 [1]  git clone http://luajit.org/git/luajit-2.0.git
From 3ab9e19b4149b63bd0bc8f7c26f6e9ee4fc2b140 Mon Sep 17 00:00:00 2001
From: Leo Razoumov <LEOR@pionL2>
Date: Sat, 12 Nov 2011 12:13:20 -0500
Subject: [LuaJIT-2 patch] support for hexadecimal floats

lua_Number (double) has no exact decimal representation because base_2 and base_10
are incompatible bases. As such, one cannot serialize lua_Number to a decimal string 
and read it back without, in general, changing its value. To address this problem
C99 as well as many modern libc representations support exact hexadecimal 
representation of floats in printf/scanf ("%a", "%A"), strtod functions. 

This patch adds support for hexadecimal floats to Luajit. 
Now LuaJIT can serialize to strings and read back lua_Number(s) exactly.

sh$ luajit
LuaJIT 2.0.0-master.LR -- Copyright (C) 2005-2011 Mike Pall. http://luajit.org/
JIT: ON CMOV SSE2 SSE3 SSE4.1 fold cse dce fwd dse narrow loop abc fuse
> =("%a"):format(1.0987654321e-20)
0x1.9f1a12718ed76p-67
> = 0x1.9f1a12718ed76p-67
1.0987654321e-20
> =tonumber("0x1.9f1a12718ed76p-67")
1.0987654321e-20
>

************************************************************************
This patch is against luajit-2.0 HEAD

To apply this patch issue
  sh$ cd /path/to/luajit-2/distribution
  sh$ patch -p1 < hex-floats-luajit2-HEAD.diff
************************************************************************

Signed-off-by: Leo Razoumov <LEOR@pionL2>
---
 src/lib_string.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/lib_string.c b/src/lib_string.c
index 5cbe6e4..c790b4b 100644
--- a/src/lib_string.c
+++ b/src/lib_string.c
@@ -780,14 +780,14 @@ LJLIB_CF(string_format)
 	addintlen(form);
 	sprintf(buff, form, num2uintfrm(L, arg));
 	break;
-      case 'e':  case 'E': case 'f': case 'g': case 'G': {
+      case 'e':  case 'E': case 'f': case 'g': case 'G': case 'a': case 'A': {
 	TValue tv;
 	tv.n = lj_lib_checknum(L, arg);
 	if (LJ_UNLIKELY((tv.u32.hi << 1) >= 0xffe00000)) {
 	  /* Canonicalize output of non-finite values. */
 	  char *p, nbuf[LJ_STR_NUMBUF];
 	  size_t len = lj_str_bufnum(nbuf, &tv);
-	  if (strfrmt[-1] == 'E' || strfrmt[-1] == 'G') {
+	  if (strfrmt[-1] == 'E' || strfrmt[-1] == 'G' || strfrmt[-1] == 'A') {
 	    nbuf[len-3] = nbuf[len-3] - 0x20;
 	    nbuf[len-2] = nbuf[len-2] - 0x20;
 	    nbuf[len-1] = nbuf[len-1] - 0x20;
-- 
1.7.4.5.LR1