Parsing complex numbers

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Parsing complex numbers

Jérémy Béjanin
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
```
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Parsing complex numbers

Yichao Yu
On Fri, Oct 28, 2016 at 3:53 PM, Jérémy Béjanin
<[hidden email]> 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Parsing complex numbers

Jérémy Béjanin
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
<<a href="javascript:" target="_blank" gdf-obfuscated-mailto="kUPXur1uAQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">jeremy....@...> 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Parsing complex numbers

Tamas Papp
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
>> <[hidden email] <javascript:>> 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.
>>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Parsing complex numbers

Jeffrey Sarnoff
In reply to this post by Jérémy Béjanin
+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
<[hidden email]> 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.
Loading...