From: Tayss
Subject: CLisp api for parsing floats?
Date: 
Message-ID: <5627c6fa.0310091550.430dee85@posting.google.com>
I can't get onto the clisp-discuss mailing list.  Does anyone know of
an exposed CLisp api that allows me to parse floats?  Erik Naggum
wrote about this earlier, but his solution was Franz-specific.
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=off&threadm=3169487578816052%40naggum.no&rnum=2&prev=/groups%3Fq%3Dparse-float%2Bauthor:naggum%2Bgroup:comp.lang.lisp.*%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26group%3Dcomp.lang.lisp.*%26safe%3Doff%26selm%3D3169487578816052%2540naggum.no%26rnum%3D2

Right now, my solution is the quick hack of using read-string, setting
*read-eval* to nil as Graham's ANSI CL mentions, but I suspect it
still has a gaping security hole.  But I could ensure that a string I
receive only contains +,-,.,digits.

Or is everyone using Matthew Danish's parse-number?
http://mapcar.org/~mrd/utilities/parse-number.lisp

Basically I don't know the idiomatic CL way of handling this, if there
is one.  Thanks if anyone has advice to give.

From: David Lichteblau
Subject: Re: CLisp api for parsing floats?
Date: 
Message-ID: <oelllrukl1m.fsf@vanilla.rz.fhtw-berlin.de>
··········@yahoo.com (Tayss) writes:
> Right now, my solution is the quick hack of using read-string, setting
> *read-eval* to nil as Graham's ANSI CL mentions, but I suspect it
> still has a gaping security hole.  But I could ensure that a string I
> receive only contains +,-,.,digits.

Yes, it would be nice if Lisp had PARSE-FLOAT in addition to
PARSE-INTEGER.

Verifying the syntax using a regular expression or state machine and
then calling READ will typically be enough, though.  That is what I am
doing to parse Java floats anyway (with some adjustments for differences
in syntax, of course).
From: Tayss
Subject: Re: CLisp api for parsing floats?
Date: 
Message-ID: <5627c6fa.0310092228.29c0a0f4@posting.google.com>
David Lichteblau <············@lichteblau.com> wrote in message news:<···············@vanilla.rz.fhtw-berlin.de>...
> Verifying the syntax using a regular expression or state machine and
> then calling READ will typically be enough, though.  That is what I am
> doing to parse Java floats anyway (with some adjustments for differences
> in syntax, of course).

Out of curiosity, was using Java's BigDecimal(String) or
Float.parseFloat() a bad idea?  That sounds like the easy solution,
unless I'm misunderstanding your requirements.
From: Thomas F. Burdick
Subject: Re: CLisp api for parsing floats?
Date: 
Message-ID: <xcvr81lpp7v.fsf@famine.OCF.Berkeley.EDU>
··········@yahoo.com (Tayss) writes:

> David Lichteblau <············@lichteblau.com> wrote in message news:<···············@vanilla.rz.fhtw-berlin.de>...
> > Verifying the syntax using a regular expression or state machine and
> > then calling READ will typically be enough, though.  That is what I am
> > doing to parse Java floats anyway (with some adjustments for differences
> > in syntax, of course).
> 
> Out of curiosity, was using Java's BigDecimal(String) or
> Float.parseFloat() a bad idea?  That sounds like the easy solution,
> unless I'm misunderstanding your requirements.

  parseFloat
  public static float parseFloat(String s)
                          throws NumberFormatException
...
  Throws:
  NumberFormatException - if the string does not contain a parsable
  float.

You might want to verify and correct anything before you pass it to
this method.  I guess you could wait until after, then try again, but
you might as well do it right from the start, sabes?

-- 
           /|_     .-----------------------.                        
         ,'  .\  / | No to Imperialist war |                        
     ,--'    _,'   | Wage class war!       |                        
    /       /      `-----------------------'                        
   (   -.  |                               
   |     ) |                               
  (`-.  '--.)                              
   `. )----'                               
From: David Lichteblau
Subject: Re: CLisp api for parsing floats?
Date: 
Message-ID: <oeld6d5ksgq.fsf@vanilla.rz.fhtw-berlin.de>
··········@yahoo.com (Tayss) writes:
> Out of curiosity, was using Java's BigDecimal(String) or
> Float.parseFloat() a bad idea?  That sounds like the easy solution,
> unless I'm misunderstanding your requirements.

I meant implementing parseDouble(), not using it.
From: james anderson
Subject: Re: CLisp api for parsing floats?
Date: 
Message-ID: <3F86E521.78E2FA37@setf.de>
one might consider exploring the tools which are available for building such
parsers / state machines [0], in particular, meta is pretty handy [1],

both cltl2 and the hyperspec describe the syntax of floats, and a meta-based
parser for those is but a couple dozen very sparse lines of code.

Tayss wrote:
> 
> David Lichteblau <············@lichteblau.com> wrote in message news:<···············@vanilla.rz.fhtw-berlin.de>...
> > Verifying the syntax using a regular expression or state machine and
> > then calling READ will typically be enough, though.  That is what I am
> > doing to parse Java floats anyway (with some adjustments for differences
> > in syntax, of course).
> 
> Out of curiosity, was using Java's BigDecimal(String) or
> Float.parseFloat() a bad idea?  That sounds like the easy solution,
> unless I'm misunderstanding your requirements.

[0] http://www.cliki.net/admin/search?words=parsers
[1] http://www.cliki.net/Meta
From: David Lichteblau
Subject: Re: CLisp api for parsing floats?
Date: 
Message-ID: <oel7k3dkof7.fsf@vanilla.rz.fhtw-berlin.de>
james anderson <··············@setf.de> writes:
> both cltl2 and the hyperspec describe the syntax of floats, and a meta-based
> parser for those is but a couple dozen very sparse lines of code.

Understanding float syntax is not hard.  Computing the floating-point
number efficiently and accurately is non-trivial.
From: james anderson
Subject: Re: CLisp api for parsing floats?
Date: 
Message-ID: <3F86EFF2.FA6B1436@setf.de>
David Lichteblau wrote:
> 
> james anderson <··············@setf.de> writes:
> > both cltl2 and the hyperspec describe the syntax of floats, and a meta-based
> > parser for those is but a couple dozen very sparse lines of code.
> 
> Understanding float syntax is not hard.  Computing the floating-point
> number efficiently and accurately is non-trivial.

ok. perhaps it is all moot, in that the issue has been resolved by opening up
the clisp source and availing onself of what it offers. in the event, however
that it has not, both the original reference
(http://mapcar.org/~mrd/utilities/parse-number.lisp) and m.antoniotti's
referenced module
(http://www-2.cs.cmu.edu/afs/cs/project/ai-repository/ai/lang/lisp/code/math/atof/)
both ultimately devolve to some form of

 (* (number-value whole-place)
    (expt (base-for-exponent-marker exp-marker)
          (number-value exp-place)))

if there is an alternative which the o.p. should prefer, he might benefit from
a concrete pointer.

...
From: David Lichteblau
Subject: Re: CLisp api for parsing floats?
Date: 
Message-ID: <oel3ce1kmpz.fsf@vanilla.rz.fhtw-berlin.de>
james anderson <··············@setf.de> writes:
> both ultimately devolve to some form of
>
>  (* (number-value whole-place)
>     (expt (base-for-exponent-marker exp-marker)
>           (number-value exp-place)))

...and calling FLOAT on that result.  So while it _is_ tricky to solve
this task, there is a function to do it.  Nice.
From: Marco Antoniotti
Subject: Re: CLisp api for parsing floats?
Date: 
Message-ID: <9gBhb.23$KR3.4841@typhoon.nyu.edu>
David Lichteblau wrote:

> ··········@yahoo.com (Tayss) writes:
> 
>>Right now, my solution is the quick hack of using read-string, setting
>>*read-eval* to nil as Graham's ANSI CL mentions, but I suspect it
>>still has a gaping security hole.  But I could ensure that a string I
>>receive only contains +,-,.,digits.
> 
> 
> Yes, it would be nice if Lisp had PARSE-FLOAT in addition to
> PARSE-INTEGER.
> 
> Verifying the syntax using a regular expression or state machine and
> then calling READ will typically be enough, though.  That is what I am
> doing to parse Java floats anyway (with some adjustments for differences
> in syntax, of course).

There is a portable PARSE-FLOAT in the AI.Repository

Cheers
--
Marco