RE: Little incosistency in Euphoria 2.2

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

This is a multi-part message in MIME format.

------=_NextPart_000_0057_01C0D00E.77E9BB60
	charset="iso-8859-1"

I think the problem is that we try to explain what value() "thinks" or is
doing, and not what it *should* do. For example, in case the argument is
78E6, it should of course return a valid result of 78 * 10 ^6, and in case
the argument is 78ER it should return also a valid result of 78 and not an
error. That is, once it got past the 'E' and finds that no valid exponent
follows, it should back up to the point it found 'E' and proceed as if it
got an 'A', to be consistent.
This is what I have attempted to do in the attached function. Comments in it
explain its workings. It reaches to a point previous to obtaining a numeric
value, and does not handle the case of a representation of an Euphoria
sequence, but only admits a character string. I have not thoroughly tested
it.
----- Original Message -----
From: Derek Parnell <ddparnell at bigpond.com>
To: EUforum <EUforum at topica.com>
Sent: Friday, April 27, 2001 10:06 PM
Subject: Re: Little incosistency in Euphoria 2.2


>
>
> ----- Original Message -----
> From: <rforno at tutopia.com>
> To: "EUforum" <EUforum at topica.com>
> Sent: Saturday, April 28, 2001 9:42 AM
> Subject: RE: Little incosistency in Euphoria 2.2
>
>
>
> > I cannot agree with you. If e- is bad, why a- is not?
>
> Because the letter 'e', when it immediately follows a series of digits, is
> regarded by value() to be a part of an embedded encoding of a number in
> scientific notation. If detected, it MUST be followed by another series of
> digits. If not, then value() believes you have a badly formatted number.
In
> the case of "a-", the 'a' character signals the END of a number and is not
> regarded as a part of the number's notation.
>
> However, I agree that this is not the best idea.
>
> Sorry for those who don't long posts but here is an attempt I made at
> writing a sequence-to-number conversion. I wanted to use the function
> seqtonumber() in an expression so it always returns an atom. To detected
> errors, I've had create another function that returns the error position.
> Finally, it does not support scientific notation (yet).
>
> ----------------------------
> integer vBase, vPeriod, vComma, vMoney
> sequence vLegalChars
> vBase = 10
> vPeriod = '.'
> vComma = ','
> vMoney = '$'
> vLegalChars = "0123456789abcdef-+.,"
>
> ---
> -- Sets the default number base to be used when converting strings to
> numbers.
> global function setnumberbase(integer pNewBase)
>     integer lOldBase
>
>     lOldBase = vBase
>
>     if pNewBase > 1 and pNewBase <= 16 then
>         vBase = pNewBase
>     end if
>
>     return lOldBase
> end function
>
> ---
> -- Sets the default punctuation characters.
> -- If any parameter is zero, then the corresponding punctuation symbol is
> not changed.
> -- All three punctuation symbols must be different from each other.
> -- Returns a sequence of three elements containing the current punctuation
> chars.
> --   1) The 'decimal place' character. Initially '.'
> --   2) The digit separator character. Initially ','
> --   3) The money symbol. Initially '$'
> global function setnumberpunct(integer pPeriod, integer pComma, integer
> pMoney)
>     sequence lOldValues
>
>     lOldValues = {vPeriod, vComma, vMoney}
<snip>

>
>
>
------=_NextPart_000_0057_01C0D00E.77E9BB60
Content-Type: application/octet-stream;
	name="Example.ex"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="Example.ex"

function extract(sequence s)
    --Extracts a subsequence representing a number from an ASCII =
sequence
    --It returns a sequence with 3 elements:
    -- 1) An integer which reports if the 2nd element is a valid
    --  representation of a number or not (1 or 0 respectively)
    -- 2) A subsequence starting at the beginning of the argument which
    --  contains a valid or invalid representation of a number, =
including
    --  the (possible) termination character
    -- 3) The remaining part of the argument
    --For example, from "123E-P345 678" it will return
    -- {1,"123E","-P345 678"}
    integer i,  --Used as index into s
	    r  --Marks OK or error
    i =3D 1  --Initialize
    r =3D 0  --Set to FALSE
    while i <=3D length(s) and find(s[i], " \n\r\t") do  --Skip over =
whitespace
	i +=3D 1  --Bump index
    end while
    if i > length(s) then  --Test end of s
	return {r, s[1..i-1], {}}  --Returns 'error', result, remaining data
    elsif s[i] =3D '+' or s[i] =3D '-' then  --Check for sign
	i +=3D 1
    end if
    while i <=3D length(s) and find(s[i], "0123456789") do  --Whole part
	i +=3D 1
	r =3D 1  --Mark at least 1 mantissa digit
    end while
    if i <=3D length(s) and s[i] =3D '.' then  --Process period
	i +=3D 1
	while i <=3D length(s) and find(s[i], "0123456789") do  --Decimals
	    i +=3D 1
	    r =3D 1  --Mark at least 1 mantissa digit
	end while
    end if
    if i <=3D length(s) and (s[i] =3D 'e' or s[i] =3D 'E') then  =
--Exponent mark
	i +=3D 1
	if i > length(s) or 0 =3D find(s[i], "0123456789-+") then  --Unexpected
	    return {r, s[1..i-1], s[i..length(s)]}  --Return extracted sequence
	elsif s[i] =3D '-' or s[i] =3D '+' then  --Exponent sign
	    i +=3D 1
	end if
	if i > length(s) or 0 =3D find(s[i], "0123456789") then  --Unexpected
	    if s[i-1] =3D '-' or s[i-1] =3D '+' then  --Back up
		i -=3D 1
	    end if
	else
	    while i <=3D length(s) and find(s[i], "0123456789") do  --Exponent
		i +=3D 1
	    end while
	end if
    end if
    if i <=3D length(s) then
	i +=3D 1
    end if
    return {r, s[1..i-1], s[i..length(s)]}  --In any other case
end function


------=_NextPart_000_0057_01C0D00E.77E9BB60--

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

Search



Quick Links

User menu

Not signed in.

Misc Menu