-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
strconv: add ParseComplex #36771
Comments
I don't think it should import |
Exactly what format(s) do you think it should accept? |
Note that the language doesn't define any format for a complex expression. Instead it defines an imaginary literal (an integer or floating point constant followed by That said, we do have an implicit definition for parsing a complex constant in |
Let me know with what I've done so far after addressing your concerns. PR: #36815 |
It will accept:
|
Change https://golang.org/cl/216617 mentions this issue: |
Any news on this? |
For floats we have: func ParseFloat(s string, bitSize int) (float64, error) It seems like we'd need func ParseComplex(s string, bitSize int) (complex128, error) This seems like it is a hole we forgot to fill. /cc @robpike @griesemer |
Seems like a reasonable thing to add, in parallel with float64 support as @rsc says. It would be good to see if the fmt package adapts well to using it. |
What should
|
FormatComplex should probably put in the parens, same as fmt. For example, I believe these tests from fmt show what fmt='x' prec=3 should do:
|
cool. implemented FormatComplex. Will do unit tests later. |
Based on the discussion, this seems like a likely accept. |
No change in consensus, so accepted. |
So what happens now? Well it be in go 1.15? |
Is it possible to keep strings package out because it's heavily used in environments like gopher.js @odeke-em |
For sure, @pjebs, thanks for letting me know :) Let's take it to your CL, where I've posted feedback. |
@odeke-em Thank you for the feedback. I'll make various adjustments in accordance with your suggestions. I was told we were only accepting |
Gotcha! My interpretation of that comment was more of: the format isn’t
defined and yes we shouldn’t be accepting arbitrary expressions. However
the Go parser and compilers accept either of these forms for complex
constants:
REAL JOINING_SIGN IMAG
IMAG JOINING_SIGN REAL
So I think that naturally Go users will have an expectation of being able
to use any of:
-30-50i
-50i-30
Or:
-50i
Or:
-30
As well as other forms of IEEE 754 floats for the constants. As Ian
mentioned a complex is just a constant affixed with an “i”.
Your CL currently accepts: -30-50i but not -50i-30. I think we can make it
accept the latter form, by meeting in the middle for a hybrid of
suggestions.
…On Sat, Apr 11, 2020 at 7:21 PM pj ***@***.***> wrote:
@odeke-em <https://github.com/odeke-em> Thank you for the feedback. I'll
make various adjustments. I was told we were only accepting (N=Ni) format
as per #36771 (comment)
<#36771 (comment)>
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#36771 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABFL3V2XBQYA6D42ME45UB3RMEQUBANCNFSM4KLT6XNQ>
.
|
I got confused because the @ianlancetaylor pointed me to the "implicit definition" that Go currently accepts and it states:
My interpretation was that there is already code that only parses/accepts |
The format we accepted for this proposal is exactly the one in #36771 (comment) , which is simpler and more constraining than what @odeke-em suggests above. |
Thank you for the clarification on the interpretation and what was
accepted, Ian. Alright, in the future after we’ve merged the related CL. If
users request for it, would it be logical for us to accept Ni+N as well?
…On Sat, Apr 11, 2020 at 8:10 PM Ian Lance Taylor ***@***.***> wrote:
The format we accepted for this proposal is exactly the one in #36771
(comment)
<#36771 (comment)> ,
which is simpler and more constraining than what @odeke-em
<https://github.com/odeke-em> suggests above.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#36771 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABFL3V2UXNKMFQUTYKXBH53RMEWMFANCNFSM4KLT6XNQ>
.
|
In that case, @odeke-em can you remove the comments you made that are related to the expanded definition. I believe there were only cosmetic adjustments left. |
Acknowledged, thank you @pjebs and @ianlancetaylor for the clarification. I shall update my comments on the CL. |
@odeke-em Finished amending PR based on your comments. The only thing I didn't change was your suggestion that I change Thanks for the review. |
Awesome, I shall take a look and I'll also kindly rope in @griesemer for the final look.
Aye aye. Nice work and ergonimcs, I hope to use it sometime in the future! |
The code already (implicitly) rejects malformed parenthesis |
Issues:
Am I meant to change the commit message for my initial commit? Is that how I do it?
The
I have added more detailed comments to explain the logic. I feel that the algorithmic approach is simpler once the logic is understood. |
Thanks for the ping Pj!
Please use the CL or the respective PR to respond to code review comments
otherwise we now no longer have threaded conversations related to the code.
Please add those comments to the PR or better, even log in and comment
directly on the CL. I can’t correlate them via email and the actual CL :)
…On Mon, Apr 20, 2020 at 8:34 PM pj ***@***.***> wrote:
@odeke-em <https://github.com/odeke-em>
Issues:
PS8, Line 10
This suggestion wasn't added.
Am I meant to change the commit message for my initial commit? Is that how
I do it?
We need to ensure that malformed parenthesis throw out an error e.g.:
The ParseFloat function already takes care of rejecting malformed
parenthesis. There is no need to cater for it. See test cases.
This code isn't intuitive to me on what it does.
Would be nice to explain the logic of how we figure out what's real what's
imaginary.
I have added more detailed comments to explain the logic. I feel that the
algorithmic approach is simpler once the logic is understood.
Hexadecimal values e.g. ±0x10.3p±8±0x3p3i
*The ParseComplex function leverages on ParseFloat. I can't get even
ParseFloat to accept hexadecimal values. See:
https://play.golang.org/p/E1s39kCdmbV
<https://play.golang.org/p/E1s39kCdmbV>*
Can you please look at the playground and tell me what I'm doing wrong?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#36771 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABFL3V6IVZGCDXKV72XU35LRNUH57ANCNFSM4KLT6XNQ>
.
|
@odeke-em I responded on the official site. |
@ianlancetaylor @griesemer Will this make it to Go1.15? |
I've started looking at the code. |
Change https://golang.org/cl/230737 mentions this issue: |
parseFloatPrefix will make it easier to implement ParseComplex. Verified that there's no relevant performance impact: Benchmarks run on a "quiet" MacBook Pro, 3.3GHz Dual-Core Intel Core i7, with 16GB 2133MHz LPDDR3 RAM running macOS 10.15.4. name old time/op new time/op delta Atof64Decimal-4 38.2ns ± 4% 38.4ns ± 3% ~ (p=0.802 n=5+5) Atof64Float-4 41.1ns ± 3% 43.0ns ± 1% +4.77% (p=0.008 n=5+5) Atof64FloatExp-4 71.9ns ± 3% 70.1ns ± 1% ~ (p=0.063 n=5+5) Atof64Big-4 124ns ± 5% 119ns ± 0% ~ (p=0.143 n=5+4) Atof64RandomBits-4 57.2ns ± 1% 55.7ns ± 2% -2.66% (p=0.016 n=4+5) Atof64RandomFloats-4 56.8ns ± 1% 56.9ns ± 4% ~ (p=0.556 n=4+5) Atof32Decimal-4 35.4ns ± 5% 35.9ns ± 0% ~ (p=0.127 n=5+5) Atof32Float-4 39.6ns ± 7% 40.3ns ± 1% ~ (p=0.135 n=5+5) Atof32FloatExp-4 73.7ns ± 7% 71.9ns ± 0% ~ (p=0.175 n=5+4) Atof32Random-4 103ns ± 6% 98ns ± 2% -5.03% (p=0.008 n=5+5) Updates #36771. Change-Id: I8ff66b582ae8b468d89c9ffc35c569c735cf0341 Reviewed-on: https://go-review.googlesource.com/c/go/+/230737 Reviewed-by: Ian Lance Taylor <iant@golang.org>
Change https://golang.org/cl/231237 mentions this issue: |
parseFloatPrefix accepts a string if it has a valid floating-point number as prefix. Make sure that "infi", "infin", ... etc. are accepted as valid numbers "inf" with suffix "i", "in", etc. This is important for parsing complex numbers such as "0+infi". This change does not affect the correctness of ParseFloat because ParseFloat rejects strings that contain a suffix after a valid floating-point number. Updates #36771. Change-Id: Ie1693a8ca2f8edf07b57688e0b35751b7100d39d Reviewed-on: https://go-review.googlesource.com/c/go/+/231237 Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
parseFloatPrefix will make it easier to implement ParseComplex. Verified that there's no relevant performance impact: Benchmarks run on a "quiet" MacBook Pro, 3.3GHz Dual-Core Intel Core i7, with 16GB 2133MHz LPDDR3 RAM running macOS 10.15.4. name old time/op new time/op delta Atof64Decimal-4 38.2ns ± 4% 38.4ns ± 3% ~ (p=0.802 n=5+5) Atof64Float-4 41.1ns ± 3% 43.0ns ± 1% +4.77% (p=0.008 n=5+5) Atof64FloatExp-4 71.9ns ± 3% 70.1ns ± 1% ~ (p=0.063 n=5+5) Atof64Big-4 124ns ± 5% 119ns ± 0% ~ (p=0.143 n=5+4) Atof64RandomBits-4 57.2ns ± 1% 55.7ns ± 2% -2.66% (p=0.016 n=4+5) Atof64RandomFloats-4 56.8ns ± 1% 56.9ns ± 4% ~ (p=0.556 n=4+5) Atof32Decimal-4 35.4ns ± 5% 35.9ns ± 0% ~ (p=0.127 n=5+5) Atof32Float-4 39.6ns ± 7% 40.3ns ± 1% ~ (p=0.135 n=5+5) Atof32FloatExp-4 73.7ns ± 7% 71.9ns ± 0% ~ (p=0.175 n=5+4) Atof32Random-4 103ns ± 6% 98ns ± 2% -5.03% (p=0.008 n=5+5) Updates golang#36771. Change-Id: I8ff66b582ae8b468d89c9ffc35c569c735cf0341 Reviewed-on: https://go-review.googlesource.com/c/go/+/230737 Reviewed-by: Ian Lance Taylor <iant@golang.org>
parseFloatPrefix accepts a string if it has a valid floating-point number as prefix. Make sure that "infi", "infin", ... etc. are accepted as valid numbers "inf" with suffix "i", "in", etc. This is important for parsing complex numbers such as "0+infi". This change does not affect the correctness of ParseFloat because ParseFloat rejects strings that contain a suffix after a valid floating-point number. Updates golang#36771. Change-Id: Ie1693a8ca2f8edf07b57688e0b35751b7100d39d Reviewed-on: https://go-review.googlesource.com/c/go/+/231237 Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
strconv.ParseComplex(s string, bitSize int) (complex128, error)
The text was updated successfully, but these errors were encountered: