Re: POLL: What should value do with underscores?

new topic     » goto parent     » topic index » view thread      » older message » newer message
 
function get_number()  
-- read a number or a comment  
-- ch is "live" at entry and exit  
	plus_or_minus sign, e_sign  
	integer ndigits, base  
	integer base_digit  
	atom mantissa, dec, e_mag  
	sequence number_string = {}  
  
	sign = +1  
	mantissa = 0  
	ndigits = 0  
	base = 10  
	  
	-- process sign  
	if ch = '-' then  
		number_string &= ch 
		get_ch()  
		if ch='-' then  
			return read_comment()  
		end if  
		sign = -1  
	elsif ch = '+' then  
		number_string &= ch 
		get_ch()  
	end if  
  
	while ch = '_' do 
		get_ch() 
	end while 
	 
	-- get base  
	if ch = '#' then  
		number_string &= ch  
		get_ch()  
		base = 16  
	elsif ch = '0' then  
		number_string &= ch  
		get_ch()  
		if find(ch, "btx") then  
			if ch = 'b' then  
				base = 2  
			elsif ch = 't' then  
				base = 8  
			elsif ch ='x' then  
				base = 16  
			end if  
			number_string &= ch  
			get_ch()  
		end if  
	end if  
  
	-- process integer part  
	while TRUE do  
		base_digit = find(ch - 32 * (ch >= 'a'), HEX_DIGITS[1..base] & '_') - 1  
		if base_digit >= 0 then  
			if base_digit < base then  
	 			ndigits += 1  
				mantissa = mantissa * base + base_digit  
				number_string &= ch  
			end if  
			get_ch()  
		else  
			exit  
		end if  
	end while  
  
	-- process decimal part  
	if ch = '.' then  
		number_string &= ch  
		get_ch()  
		dec = base  
		while TRUE do  
			base_digit = find(ch - 32 * (ch >= 'a'), HEX_DIGITS[1..base] & '_') - 1  
			if base_digit >= 0 then  
				if base_digit < base then  
					ndigits += 1  
					mantissa += base_digit / dec  
					dec *= base  
					number_string &= ch  
				end if  
				get_ch()  
			else  
				exit  
			end if  
		end while  
	end if  
  
	mantissa = sign * mantissa  
  
	if ch = 'e' or ch = 'E' then  
		-- get exponent sign  
		number_string &= ch  
		get_ch()  
		while ch = '_' do 
			get_ch() 
		end while 
		if ch = '-' or ch = '+' then  
			number_string &= ch  
			get_ch()  
		end if  
		  
		-- get exponent magnitude  
		while TRUE do  
			base_digit = find(ch - 32 * (ch >= 'a'), HEX_DIGITS[1..base] & '_') - 1  
			if base_digit >= 0 then  
				if base_digit < base then  
					ndigits += 1  
					number_string &= ch  
				end if  
				get_ch()  
			else  
				exit  
			end if  
		end while  
		mantissa = scientific_to_atom( number_string )  
	end if  
  
	if ndigits = 0 then  
		return {GET_FAIL, 0}  
	end if  
  
	return {GET_SUCCESS, mantissa}  
end function  
 

get value for binary, octal, decimal or hexadecimal from a string (integer or floating point).

 
[$]: cat valnum.ex  
 
include std/get.e.new 
 
? value("-1024.345") 
? value("-1_02___4.3_45_ ") 
? value("-#4_00__.5851_EB85_2") 
? value("-#4_00__.5851_eb85_2") 
? value("-0x4_0____0.__58_51EB852") 
? value("-0x4_0____0.__58_51eb852") 
? value("-0t2__0_00.260507534") 
? value("-0b100_00__00_0000.0101_1000_1") 
? value("-1_024.3_45__e-_2_5") 
? value("0x3.243F6A888") 
 
[$]: eui valnum.ex  
{0,-1024.345} 
{0,-1024.345} 
{0,-1024.345} 
{0,-1024.345} 
{0,-1024.345} 
{0,-1024.345} 
{0,-1024.345} 
{0,-1024.345703} 
{0,-1.024345e-22} 
{0,3.141592654} 
 

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu