From: fireblade
Subject: How to convert string to float ? Right way to use template?
Date: 
Message-ID: <1176966554.534758.316070@b58g2000hsg.googlegroups.com>
Could someone suggest a way to convert string like "123.516"  to
123.516?

thanks
bobi

From: Petter Gustad
Subject: Re: How to convert string to float ? Right way to use template?
Date: 
Message-ID: <7dfy6w27kt.fsf@www.gratismegler.no>
fireblade <·················@gmail.com> writes:

> Could someone suggest a way to convert string like "123.516"  to
> 123.516?

(type-of (with-input-from-string (is "123.516")
            (read is)))
=> SINGLE-FLOAT

Petter

-- 
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
From: Edi Weitz
Subject: Re: How to convert string to float ? Right way to use template?
Date: 
Message-ID: <uhcrcvnku.fsf@agharta.de>
On 19 Apr 2007 00:09:14 -0700, fireblade <·················@gmail.com> wrote:

> Could someone suggest a way to convert string like "123.516" to
> 123.516?

  http://www.cs.cmu.edu/afs/cs/project/ai-repository/ai/lang/lisp/code/math/atof/0.html
  http://common-lisp.net/project/bese/docs/arnesi/html/api/function_005FIT.BESE.ARNESI_003A_003APARSE-FLOAT.html

I'm sure Google will find more.

-- 

Lisp is not dead, it just smells funny.

Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: fireblade
Subject: Re: How to convert string to float ? Right way to use template?
Date: 
Message-ID: <1176967459.959100.202860@n59g2000hsh.googlegroups.com>
Edi Weitz íàïèøà:
> On 19 Apr 2007 00:09:14 -0700, fireblade <·················@gmail.com> wrote:
>
> > Could someone suggest a way to convert string like "123.516" to
> > 123.516?
>
>   http://www.cs.cmu.edu/afs/cs/project/ai-repository/ai/lang/lisp/code/math/atof/0.html
>   http://common-lisp.net/project/bese/docs/arnesi/html/api/function_005FIT.BESE.ARNESI_003A_003APARSE-FLOAT.html
>
> I'm sure Google will find more.
>
> --
>
> Lisp is not dead, it just smells funny.
>
> Real email: (replace (subseq ·········@agharta.de" 5) "edi")

Doesn't have to parse-float is already in lw, thank you very much Edi.

bobi
From: Frank Buss
Subject: Re: How to convert string to float ? Right way to use template?
Date: 
Message-ID: <ifu3lqq7k74x.ln9e1jldge0m.dlg@40tude.net>
fireblade wrote:

> Could someone suggest a way to convert string like "123.516"  to
> 123.516?

If you want to use Common Lisp, only, you could use read-from-string. If
you read unsafe input data, you could use something like this:

(defun parse-float (string)
  (let (*read-eval*)
    (let ((object (read-from-string string)))
      (unless (numberp object)
        (error 'parse-error))
      object)))

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: ············@gmail.com
Subject: Re: How to convert string to float ? Right way to use template?
Date: 
Message-ID: <1177011460.294466.121850@n59g2000hsh.googlegroups.com>
On Apr 19, 12:32 am, Frank Buss <····@frank-buss.de> wrote:
> If you want to use Common Lisp, only, you could use read-from-string. If
> you read unsafe input data, you could use something like this:
>
> (defun parse-float (string)
>   (let (*read-eval*)
>     (let ((object (read-from-string string)))
>       (unless (numberp object)
>         (error 'parse-error))
>       object)))

Did you mean perhaps "(let ((*read-eval* nil)) ...)", as in shutting
off "#." ?

I've always been a little confused about the security of READ and its
variants on untrusted strings. I know that the following doesn't
actually _execute_ (INCF K):

> (defconstant str "(incf k)")
> (defparameter k 42)
> (defparameter thing (read-from-string str))
> thing
(INCF K)
> k
42

but is it true for sure that disallowing #. expressions prevents READ
and its variants from executing _any_ side effects?

mfh
From: Zach Beane
Subject: Re: How to convert string to float ? Right way to use template?
Date: 
Message-ID: <m3abx4ayai.fsf@unnamed.xach.com>
·············@gmail.com" <············@gmail.com> writes:

> On Apr 19, 12:32 am, Frank Buss <····@frank-buss.de> wrote:
> > If you want to use Common Lisp, only, you could use read-from-string. If
> > you read unsafe input data, you could use something like this:
> >
> > (defun parse-float (string)
> >   (let (*read-eval*)
> >     (let ((object (read-from-string string)))
> >       (unless (numberp object)
> >         (error 'parse-error))
> >       object)))
> 
> Did you mean perhaps "(let ((*read-eval* nil)) ...)", as in shutting
> off "#." ?

What Frank wrote is equivalent.

Zach
From: Raffael Cavallaro
Subject: Re: How to convert string to float ? Right way to use template?
Date: 
Message-ID: <2007041923311616807-raffaelcavallaro@pasdespamsilvousplaitmaccom>
On 2007-04-19 15:37:40 -0400, ·············@gmail.com" 
<············@gmail.com> said:

> Did you mean perhaps "(let ((*read-eval* nil)) ...)", as in shutting
> off "#." ?

To fully flog this late equid, the default binding of a var in a let or 
let* form is nil:

"For both let and let*, if there is not an init-form associated with a 
var, var is initialized to nil."

So lets of the following form are perfectly ok:

(let (a (b 1) c) (list a b c))

=> (NIL 1 NIL)
From: Rob St. Amant
Subject: Re: How to convert string to float ? Right way to use template?
Date: 
Message-ID: <f0ab7d$3n5$1@blackhelicopter.databasix.com>
Raffael Cavallaro
<················@pas-d'espam-s'il-vous-plait-mac.com> writes:

> On 2007-04-19 15:37:40 -0400, ·············@gmail.com"
> <············@gmail.com> said:
>
>> Did you mean perhaps "(let ((*read-eval* nil)) ...)", as in shutting
>> off "#." ?
>
> To fully flog this late equid, the default binding of a var in a let
> or let* form is nil:
>
> "For both let and let*, if there is not an init-form associated with a
> var, var is initialized to nil."
>
> So lets of the following form are perfectly ok:
>
> (let (a (b 1) c) (list a b c))
>
> => (NIL 1 NIL)

For clarity, I try to have an explicit initialization to nil if the
code depends on a variable having that value when it's first used.  I
also avoid things like

(let (a b) . . .)

because when browsing through code it's too easy for me to mistake
it for a binding of 'a' to 'b'.
From: Raffael Cavallaro
Subject: Re: How to convert string to float ? Right way to use template?
Date: 
Message-ID: <2007042010590616807-raffaelcavallaro@pasdespamsilvousplaitmaccom>
On 2007-04-20 08:21:24 -0400, ·······@ncsu.edu (Rob St. Amant) said:

> For clarity, I try to have an explicit initialization to nil if the
> code depends on a variable having that value when it's first used.  I
> also avoid things like
> 
> (let (a b) . . .)
> 
> because when browsing through code it's too easy for me to mistake
> it for a binding of 'a' to 'b'.

As a stylistic matter I agree and do the same as you. For better or 
worse though such uninitialized let/let* forms are perfectly legal and 
not even deprecated so anyone using common lisp needs to be aware of 
this default binding aspect of let/let*.
From: Drew Crampsie
Subject: Re: How to convert string to float ? Right way to use template?
Date: 
Message-ID: <4627b145$0$6888$88260bb3@free.teranews.com>
fireblade wrote:
> Could someone suggest a way to convert string like "123.516"  to
> 123.516?

<drewc> minion: parse-number?

<minion> parse-number: parse-number is a Library of functions which
accept an arbitrary string and attempt to parse it into one of the
standard Common Lisp number types, if possible, or else it signals an
error of type invalid-number. http://www.cliki.net/parse-number


hth,

drewc



> 
> thanks
> bobi
> 

-- 
Posted via a free Usenet account from http://www.teranews.com