forum-msg-id-126255-edit

Original date:2015-01-15 15:31:46 Edited by: cargoan Subject: Re: text.e bugs?

Same as previous plus 64bits support.

Tests fails, expected a floating (1.2345e+17) but it is an integer in 64 bits, and expected a 32 bits sequence in binary format.

I use only Euphoria 4.1.

[$]: eutest "$EUDIR/tests/t_text.e"; 
 
interpreting /home/cargoan/euphoria-LINUX-4.1.0/tests/t_text.e: 
  failed: format 'I', expected: "The answer is 1.2345e+17" but got: "The answer is 123450000000000000.0000" 
  failed: format 'AH', expected: "11111111111111111111111101001111" but got: "1111111111111111111111111111111111111111111111111111111101001111" 
  178 tests run, 176 passed, 2 failed, 99% success 
FAILURE: /home/cargoan/euphoria-LINUX-4.1.0/tests/t_text.e program died with status 1 
 
Test results summary: 
    FAIL: /home/cargoan/euphoria-LINUX-4.1.0/tests/t_text.e 
Files (run: 1) (failed: 1) (0% success) 
[$]: cat prubits64.ex; eui prubits64.ex  
 
atom a = 1.2345e+17 
 
printf(1, "%.18g\n", a)   -- 64 bits has 18 digits (LDBL_DIG) 
printf(1, "%.15g\n", a)   -- 32 bits has 15 digits (LDBL_DIG = DBL_DIG) 
 
123450000000000000 
1.2345e+17 
 

Patch:

--- text.e	2015-01-15 21:29:54.034292000 +0100 
+++ text.e	2015-01-15 21:29:46.151292000 +0100 
@@ -1409,6 +1409,17 @@ 
 --   [[:sprintf]] 
 -- 
  
+ifdef BITS64 then 
+    constant ARCH_BITS =  64 
+    constant MAX_DIGS  =  18 
+    constant MAX_UINT  =  0xFFFF_FFFF_FFFF_FFFF 
+elsedef 
+    constant ARCH_BITS =  32 
+    constant MAX_DIGS  =  15 
+    constant MAX_UINT  =  0xFFFF_FFFF 
+end ifdef 
+ 
+ 
 public function format(sequence format_pattern, object arg_list = {}) 
 	sequence result 
 	integer in_token 
@@ -1441,7 +1452,9 @@ 
 	integer ep 
 	integer pflag 
 	integer count 
-	 
+	sequence fmt 
+	atom argval 
+		 
 	if atom(arg_list) then 
 		arg_list = {arg_list} 
 	end if 
@@ -1548,7 +1561,7 @@ 
 	    				end if 
 	    				width = width * 10 + pos - 1 
 	    				if width = 0 then 
-	    					zfill = '0' 
+	    					zfill = 1 
 	    				end if 
 	    			end while 
  
@@ -1677,18 +1690,15 @@ 
 							argtext = arg_list[argn] 
 						end if 
 						 
-					elsif integer(arg_list[argn])  
-					-- for consistent formatting, we need to test in case of 64-bit euphoria 
-					and arg_list[argn] <= 0x3fff_ffff 
-					and arg_list[argn] >= -0x4000_0000 then 
+					elsif integer(arg_list[argn]) then 
 						if istext then 
-							argtext = {and_bits(0xFFFF_FFFF, math:abs(arg_list[argn]))} 
+							argtext = {and_bits(MAX_UINT, math:abs(arg_list[argn]))} 
 							 
 						elsif bwz != 0 and arg_list[argn] = 0 then 
 							argtext = repeat(' ', width) 
 							 
 						elsif binout = 1 then 
-							argtext = stdseq:reverse( convert:int_to_bits(arg_list[argn], 32)) + '0' 
+							argtext = stdseq:reverse( convert:int_to_bits(arg_list[argn], ARCH_BITS)) + '0' 
 							if zfill != 0 and width > 0 then 
 								if width > length(argtext) then 
 									argtext = repeat('0', width - length(argtext)) & argtext 
@@ -1749,7 +1759,7 @@ 
  
 					elsif atom(arg_list[argn]) then 
 						if istext then 
-							argtext = {and_bits(0xFFFF_FFFF, math:abs(floor(arg_list[argn])))} 
+							argtext = {and_bits(MAX_UINT, math:abs(floor(arg_list[argn])))} 
 							 
 						else 
 							if hexout then 
@@ -1760,7 +1770,19 @@ 
 									end if 
 								end if 
 							else 
-								argtext = trim(sprintf("%15.15g", arg_list[argn])) 
+								argval = arg_list[argn] 
+								if argval < 0 then 
+								   argval = -argval 
+								end if 
+								if decs < 0 then 
+									fmt = sprintf("%%.%dg", MAX_DIGS) 
+								elsif argval >= power(10, MAX_DIGS) or 
+									  argval < 1e-4 then 
+									fmt = sprintf("%%.%de", decs) 
+								else 
+									fmt = sprintf("%%.%df", decs) 
+								end if 
+								argtext = sprintf(fmt, arg_list[argn]) 
 								-- Remove any leading 0 after e+ 
 								while ep != 0 with entry do 
 									argtext = remove(argtext, ep+2) 
@@ -1770,7 +1792,7 @@ 
 								if zfill != 0 and width > 0 then 
 									if width > length(argtext) then 
 										if argtext[1] = '-' then 
-											argtext = '-' & repeat('0', width - length(argtext)) & argtext[2..$] 
+											argtext = '-' & repeat('0', width - length(argtext) - msign) & argtext[2..$] 
 										else 
 											argtext = repeat('0', width - length(argtext)) & argtext 
 										end if 
@@ -1790,7 +1812,7 @@ 
 											argtext = '(' & argtext[2..$] & ')' 
 										else 
 											if argtext[2] = '0' then 
-												argtext = '(' & argtext[3..$] & ')' 
+												argtext = '(' & argtext[2..$] & ')' 
 											else 
 												argtext = argtext[2..$] & ')' 
 											end if 
@@ -1821,7 +1843,7 @@ 
 								argtext = tempv 
 							elsif integer(tempv) then 
 								if istext then 
-									argtext = {and_bits(0xFFFF_FFFF, math:abs(tempv))} 
+									argtext = {and_bits(MAX_UINT, math:abs(tempv))} 
 							 
 								elsif bwz != 0 and tempv = 0 then 
 									argtext = repeat(' ', width) 
@@ -1831,20 +1853,23 @@ 
  
 							elsif atom(tempv) then 
 								if istext then 
-									argtext = {and_bits(0xFFFF_FFFF, math:abs(floor(tempv)))} 
+									argtext = {and_bits(MAX_UINT, math:abs(floor(tempv)))} 
 								elsif bwz != 0 and tempv = 0 then 
 									argtext = repeat(' ', width) 
 								else 
-									argtext = trim(sprintf("%15.15g", tempv)) 
+									fmt = sprintf("%%%d.%dg", {MAX_DIGS, MAX_DIGS}) 
+									argtext = trim(sprintf(fmt, tempv)) 
 								end if 
 							else 
+								fmt = sprintf("%%.%dg", MAX_DIGS) 
 								argtext = pretty:pretty_sprint( tempv, 
-											{2,0,1,1000,"%d","%.15g",32,127,1,0} 
+											{2,0,1,1000,"%d",fmt,32,127,1,0} 
 											) 
 							end if 
 						else 
+							fmt = sprintf("%%.%dg", MAX_DIGS) 
 							argtext = pretty:pretty_sprint( arg_list[argn], 
-										{2,0,1,1000,"%d","%.15g",32,127,1,0} 
+										{2,0,1,1000,"%d",fmt,32,127,1,0} 
 										) 
 						end if 
 						-- Remove any leading 0 after e+ 
 
 

Thank you everyone for your patience.

Not Categorized, Please Help

Search



Quick Links

User menu

Not signed in.

Misc Menu