Re: [julia-users] Parsing complex numbers
+1 (imo) It would be a real contribution to Julia's way of just working well with this as with that for one with the requisite skill to make quick work of implementing parse(Complex, complex) and parse(Rational, rational) into v0.6 On Friday, October 28, 2016 at 6:25:47 PM UTC-4, Jérémy Béjanin wrote: > > How easy would it be to make one assuming the standard julia notation > 3+4im? > > On Friday, October 28, 2016 at 3:58:08 PM UTC-4, Yichao Yu wrote: >> >> On Fri, Oct 28, 2016 at 3:53 PM, Jérémy Béjanin >>wrote: >> > I've noticed that parsing a string representing a real number yields a >> real >> > number, but parsing a string representing a complex number yields an >> > expression that must subsequently be evaluated. Is there a reason for >> that >> > behaviour? I'd like to avoid that behaviour considering I am reading >> > user-inputted data. >> > >> > ```julia >> > julia> typeof(parse("1.60254+3im")) >> > Expr >> > >> > julia> typeof(parse("1.60254")) >> > Float64 >> > ``` >> >> Do no use `parse(::String)` to parse numbers. It is for parsing >> generic julia code. Use `parse(Float64, str)` to parse a floating >> point number. >> >> I don't think we have a parsing function for complex number likely >> because there isn't a universal standard. >> >
Re: [julia-users] Parsing complex numbers
Not too complicated: you can either extract from the Expr, or parse the tokens iterating over (token, position) = parse(string, position, greedy = false) and validate. The only tricky part is the signs. On Sat, Oct 29 2016, Jérémy Béjanin wrote: > How easy would it be to make one assuming the standard julia notation 3+4im? > > On Friday, October 28, 2016 at 3:58:08 PM UTC-4, Yichao Yu wrote: >> >> On Fri, Oct 28, 2016 at 3:53 PM, Jérémy Béjanin >>wrote: >> > I've noticed that parsing a string representing a real number yields a >> real >> > number, but parsing a string representing a complex number yields an >> > expression that must subsequently be evaluated. Is there a reason for >> that >> > behaviour? I'd like to avoid that behaviour considering I am reading >> > user-inputted data. >> > >> > ```julia >> > julia> typeof(parse("1.60254+3im")) >> > Expr >> > >> > julia> typeof(parse("1.60254")) >> > Float64 >> > ``` >> >> Do no use `parse(::String)` to parse numbers. It is for parsing >> generic julia code. Use `parse(Float64, str)` to parse a floating >> point number. >> >> I don't think we have a parsing function for complex number likely >> because there isn't a universal standard. >>
Re: [julia-users] Parsing complex numbers
How easy would it be to make one assuming the standard julia notation 3+4im? On Friday, October 28, 2016 at 3:58:08 PM UTC-4, Yichao Yu wrote: > > On Fri, Oct 28, 2016 at 3:53 PM, Jérémy Béjanin >wrote: > > I've noticed that parsing a string representing a real number yields a > real > > number, but parsing a string representing a complex number yields an > > expression that must subsequently be evaluated. Is there a reason for > that > > behaviour? I'd like to avoid that behaviour considering I am reading > > user-inputted data. > > > > ```julia > > julia> typeof(parse("1.60254+3im")) > > Expr > > > > julia> typeof(parse("1.60254")) > > Float64 > > ``` > > Do no use `parse(::String)` to parse numbers. It is for parsing > generic julia code. Use `parse(Float64, str)` to parse a floating > point number. > > I don't think we have a parsing function for complex number likely > because there isn't a universal standard. >
Re: [julia-users] Parsing complex numbers
On Fri, Oct 28, 2016 at 3:53 PM, Jérémy Béjaninwrote: > I've noticed that parsing a string representing a real number yields a real > number, but parsing a string representing a complex number yields an > expression that must subsequently be evaluated. Is there a reason for that > behaviour? I'd like to avoid that behaviour considering I am reading > user-inputted data. > > ```julia > julia> typeof(parse("1.60254+3im")) > Expr > > julia> typeof(parse("1.60254")) > Float64 > ``` Do no use `parse(::String)` to parse numbers. It is for parsing generic julia code. Use `parse(Float64, str)` to parse a floating point number. I don't think we have a parsing function for complex number likely because there isn't a universal standard.