From: <sc...@op...> - 2011-04-24 15:23:45
|
changeset: e0665ef03f46 in /home/euweb/scm/hg_repos/euphoria author: Matt Lewis url: http://scm.openeuphoria.org/hg/euphoria?cmd=changeset;node=e0665ef03f46 description: * std/get.e now uses std/scinot.e to parse scientific notation * fixes ticket 631 diffstat: include/std/get.e | 40 ++++++++++++++++++++-------------------- include/std/scinot.e | 22 +++++++++++++++++++--- 2 files changed, 39 insertions(+), 23 deletions(-) diffs (184 lines): diff -r caa7863342ae -r e0665ef03f46 include/std/get.e --- a/include/std/get.e Sun Apr 24 10:40:10 2011 -0400 +++ b/include/std/get.e Sun Apr 24 11:21:23 2011 -0400 @@ -9,6 +9,7 @@ include std/error.e include std/io.e +include std/scinot.e --**** -- === Error Status Constants @@ -169,6 +170,7 @@ natural ndigits integer hex_digit atom mantissa, dec, e_mag + sequence number_string = { ch } sign = +1 mantissa = 0 @@ -178,23 +180,27 @@ if ch = '-' then sign = -1 get_ch() + number_string &= ch if ch='-' then return read_comment() end if elsif ch = '+' then get_ch() + number_string &= ch end if -- get mantissa if ch = '#' then -- process hex integer and return get_ch() + number_string &= ch while TRUE do hex_digit = find(ch, HEX_DIGITS)-1 if hex_digit >= 0 then ndigits += 1 mantissa = mantissa * 16 + hex_digit get_ch() + number_string &= ch else if ndigits > 0 then return {GET_SUCCESS, sign * mantissa} @@ -210,17 +216,20 @@ ndigits += 1 mantissa = mantissa * 10 + (ch - '0') get_ch() + number_string &= ch end while if ch = '.' then -- get fraction get_ch() + number_string &= ch dec = 10 while ch >= '0' and ch <= '9' do ndigits += 1 mantissa += (ch - '0') / dec dec *= 10 get_ch() + number_string &= ch end while end if @@ -231,40 +240,31 @@ mantissa = sign * mantissa if ch = 'e' or ch = 'E' then + -- get exponent sign - e_sign = +1 - e_mag = 0 get_ch() + number_string &= ch if ch = '-' then - e_sign = -1 get_ch() + number_string &= ch elsif ch = '+' then get_ch() + number_string &= ch end if + -- get exponent magnitude if ch >= '0' and ch <= '9' then - e_mag = ch - '0' - get_ch() - while ch >= '0' and ch <= '9' do - e_mag = e_mag * 10 + ch - '0' + + while ch >= '0' and ch <= '9' with entry do + number_string &= ch + entry get_ch() end while else return {GET_FAIL, 0} -- no exponent end if - e_mag *= e_sign - if e_mag > 308 then - -- rare case: avoid power() overflow - mantissa *= power(10, 308) - if e_mag > 1000 then - e_mag = 1000 - end if - for i = 1 to e_mag - 308 do - mantissa *= 10 - end for - else - mantissa *= power(10, e_mag) - end if + + mantissa = scientific_to_atom( number_string ) end if return {GET_SUCCESS, mantissa} diff -r caa7863342ae -r e0665ef03f46 include/std/scinot.e --- a/include/std/scinot.e Sun Apr 24 10:40:10 2011 -0400 +++ b/include/std/scinot.e Sun Apr 24 11:21:23 2011 -0400 @@ -6,6 +6,7 @@ end ifdef include std/convert.e +include std/dll.e --**** -- == Scientific Notation Parsing @@ -47,6 +48,9 @@ -- === Floating Point Types public enum type floating_point +--** NATIVE: Use whatever is the appropriate format based upon the version of +-- euphoria being used (DOUBLE for 32-bit, EXTENDED for 64-bit) + NATIVE, --** DOUBLE: IEEE 754 double (64-bit) floating point format. -- The native 32-bit euphoria floating point representation. DOUBLE, @@ -55,6 +59,13 @@ EXTENDED end type +integer NATIVE_FORMAT +if sizeof( C_POINTER ) = 32 then + NATIVE_FORMAT = DOUBLE +else + NATIVE_FORMAT = EXTENDED +end if + -- taken from misc.e to avoid including function reverse(sequence s) -- reverse the top-level elements of a sequence. @@ -283,12 +294,14 @@ -- -- Returns: -- Sequence of bytes that represents the physical form of the converted floating point number. -public function scientific_to_float( sequence s, floating_point fp ) +public function scientific_to_float( sequence s, floating_point fp = NATIVE ) integer dp, e, exp sequence int_bits, frac_bits, mbits, ebits, sbits integer significand, exponent, min_exp, exp_bias - + if fp = NATIVE then + fp = NATIVE_FORMAT + end if if fp = DOUBLE then significand = DOUBLE_SIGNIFICAND exponent = DOUBLE_EXPONENT @@ -464,7 +477,10 @@ -- Returns: -- Euphoria atom floating point number. -public function scientific_to_atom( sequence s, floating_point fp ) +public function scientific_to_atom( sequence s, floating_point fp = NATIVE ) + if fp = NATIVE then + fp = NATIVE_FORMAT + end if sequence float = scientific_to_float( s, fp ) if fp = DOUBLE then return float64_to_atom( float ) |