inconsistent behavior of >FLOAT




The word >FLOAT from the floating point word set appears to have inconsistent behavior on different Forth systems for the case of a string with zero length:

s" " >FLOAT

There is exactly one space between the quote characters in the above line, which should represent a string of zero length.

The return value on top of the data stack is a flag, with true indicating that the string represents a valid floating point number. Values of the resulting flag in recent versions of various Forth systems are:

gforth TRUE
PFE TRUE
VFX Forth FALSE ; using Hfp387.fth


Below is the DPANS94 spec for >FLOAT. Should the specification be made more precise with respect to a string of zero length? I suspect that C's atof() function will return a zero, while the C function strtod() will flag an error.


Krishna


-----------
12.6.1.0558 >FLOAT
to-float FLOATING

( c-addr u -- true | false ) ( F: -- r | )
or ( c-addr u -- r true | false)

An attempt is made to convert the string specified by c-addr and u to internal floating-point representation. If the string represents a valid floating-point number in the syntax below, its value r and true are returned. If the string does not represent a valid floating-point number only false is returned.

A string of blanks should be treated as a special case representing zero.

The syntax of a convertible string := <significand>[<exponent>]

<significand> := [<sign>]{<digits>[.<digits0>] |
..<digits> }
<exponent> := <marker><digits0>
<marker> := {<e-form> | <sign-form>}
<e-form> := <e-char>[<sign-form>]
<sign-form> := { + | - }
<e-char> := { D | d | E | e }

See: A.12.6.1.0558 >FLOAT
------------

Note: A.12.6.1.0558 does not mention the zero length string case.


.



Relevant Pages

  • Re: weird problem
    ... I already told you that the comparison between an integer and a float ... to strcmpwhich expects a pointer to a string. ... And now a question about something else: why do you use floating ... int,float, char, etc. ...
    (comp.lang.c)
  • Re: weird problem
    ... I already told you that the comparison between an integer and a float ... And now a question about something else: why do you use floating ... use then to copy a float into a char *1. ... binary representation doesn't resemble a string like "123.46343" ...
    (comp.lang.c)
  • REPRESENT revisited
    ... Forth-94 has few floating point display functions [none in the ... If u is greater than zero the character string shall consist ... If u is zero the string shall consist of one digit representing ...
    (comp.lang.forth)
  • Re: inconsistent behavior of >FLOAT
    ... empty string, strings with leading and trailing blanks. ... I think it is too late for>FLOAT to rescind the strong suggestion in its spec that this "should" be the case. ... As long as the spec is tightened to require that a string of blanks must be treated as a valid zero, a programmer would know to filter such a case, if needed, before it is passed to>FLOAT. ... I think ignoring leading and trailing blanks may be reasonable for>FLOAT, as long as it does not violate the current spec. ...
    (comp.lang.forth)
  • Re: numpy: handling float(NaN) different in XP vs. Linux
    ... Microsoft Windows XP ... ValueError: invalid literal for float(): NaN ... Convert a string or a number to floating point. ...
    (comp.lang.python)