-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
[stdlib] Add method atof()
to String
#2649
Conversation
atof
() to String
atof
() to String
atof()
to String
I think this also needs tests |
What do I have to do to make the 'Standard Library tests and examples' test pass? |
@fknfilewalker seems like some files are not well formatted, try running |
@fknfilewalker there is still the file 'test_string' that needs to be formated, that's why the test are failing |
@JoeLoser Would you be willing to review this or get someone from the proper team? Hope you're having a good day! |
Signed-off-by: Lukas Lipp <llipp@cg.tuwien.ac.at>
Signed-off-by: Lukas Lipp <llipp@cg.tuwien.ac.at>
Signed-off-by: Lukas Lipp <llipp@cg.tuwien.ac.at>
Signed-off-by: Lukas Lipp <llipp@cg.tuwien.ac.at>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the patch, this is gonna be awesome! I have a few suggestions and quibbles, but it's generally looking nice!
var shift: Int = 10 ** abs(exponent) | ||
if exponent > 0: | ||
result *= shift | ||
if exponent < 0: | ||
result /= shift |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since result
is a Float64
this will implicitly convert shift
anyway. Does this work?
var shift: Int = 10 ** abs(exponent) | |
if exponent > 0: | |
result *= shift | |
if exponent < 0: | |
result /= shift | |
var result *= 10.0 ** exponent |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the problem here is that mojo rounds the floats differently, and doing a division for negative exponents is closer to how mojo does it.
e.g., -0.3
mojo stores it as -0.29999999999999999
while the multiply method (3.0 * 0.1) returns -0.30000000000000004
the division method (3.0 / 10.0) produces -0.29999999999999999
so the result is different depending on whether doing a * 10
or / 0.1
or vice versa
A similar strange situation is the following
print(233.0 / 1000.0) #prints 0.23299999999999998
var x: Float64 = 233.0
var div: Float64 = 1000.0
print(x/div) #prints 0.23300000000000001
If this is a compiler issue and we can fix that then this suggestion would be definitely the better option
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, this makes sense actually. Can you please ensure we have at least one test case that hits this? Please leave a comment both here and at that test case to explain this. Thank you!
Co-authored-by: Laszlo Kindrat <laszlokindrat@gmail.com> Signed-off-by: Lukas Lipp <15105596+fknfilewalker@users.noreply.github.com>
Co-authored-by: Laszlo Kindrat <laszlokindrat@gmail.com> Signed-off-by: Lukas Lipp <15105596+fknfilewalker@users.noreply.github.com>
Signed-off-by: Lukas Lipp <llipp@cg.tuwien.ac.at>
Also, please add a changelog entry! |
Signed-off-by: Lukas Lipp <llipp@cg.tuwien.ac.at>
Signed-off-by: Lukas Lipp <llipp@cg.tuwien.ac.at>
Signed-off-by: Lukas Lipp <15105596+fknfilewalker@users.noreply.github.com>
Signed-off-by: Lukas Lipp <llipp@cg.tuwien.ac.at>
Signed-off-by: Lukas Lipp <llipp@cg.tuwien.ac.at>
[External] [stdlib] Add method `strip()` to `StringRef` This PR adds a `strip` method to `StringRef`. This PR is helpful for #2649. Where can I find the test cases for StringRef? ORIGINAL_AUTHOR=Lukas Lipp <15105596+fknfilewalker@users.noreply.github.com> PUBLIC_PR_LINK=#2683 Co-authored-by: Lukas Lipp <15105596+fknfilewalker@users.noreply.github.com> Closes #2683 MODULAR_ORIG_COMMIT_REV_ID: 5ac8b1f3b45c75a964c5d9368e3871e7fc617a88
Signed-off-by: Lukas Lipp <15105596+fknfilewalker@users.noreply.github.com>
Signed-off-by: Lukas Lipp <llipp@cg.tuwien.ac.at>
Signed-off-by: Lukas Lipp <llipp@cg.tuwien.ac.at>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great patch, thank you so much!
@JoeLoser Would it make sense to make this functionality parametric on a floating point type? I.e. something like fn atof[type: Dtype = Dtype.float64](str: String) raises -> Scalar[dtype]:
constrained[type.is_float(), "must be float"]()
... |
@fknfilewalker If you are interested, you could also do an overload for StringLiterals that returns arbitrary precision floats: fn atof(str_literal: StringLiteral) -> FloatLiteral: ... |
Is your goal to make it work for arbitrary-precision floats, or something else? |
Making it work for |
✅🟣 This contribution has been merged 🟣✅ Your pull request has been merged to the internal upstream Mojo sources. It will be reflected here in the Mojo repository on the nightly branch during the next Mojo nightly release, typically within the next 24-48 hours. We use Copybara to merge external contributions, click here to learn more. |
Landed in 8641d49! Thank you for your contribution 🎉 |
[External] [stdlib] Add method `atof()` to `String` This PR adds a function that can convert a `String` to a `Float64`. Right now it is implemented just for Float64 but maybe we should add other precisions? This supports the following notations: ```python "-1236.233" "2.25" "2." "1.7E+3" # as well as the f/F postfix notation "-1236.233f" "2.25F" "2.f" "1.7E+3F" ``` ORIGINAL_AUTHOR=Lukas Lipp <15105596+fknfilewalker@users.noreply.github.com> PUBLIC_PR_LINK=#2649 Co-authored-by: Lukas Lipp <15105596+fknfilewalker@users.noreply.github.com> Closes #2649 MODULAR_ORIG_COMMIT_REV_ID: b8d2c4ef38faa639e749957a0c1ba1a9c02a28cf
Sure, is there a reason why we have the inner |
In order for this to work with FloatLiteral we would need to use StringLiteral I guess? How does it work to create a function for runtime values and compile time values? |
Could |
[External] [stdlib] Add method `strip()` to `StringRef` This PR adds a `strip` method to `StringRef`. This PR is helpful for modularml#2649. Where can I find the test cases for StringRef? ORIGINAL_AUTHOR=Lukas Lipp <15105596+fknfilewalker@users.noreply.github.com> PUBLIC_PR_LINK=modularml#2683 Co-authored-by: Lukas Lipp <15105596+fknfilewalker@users.noreply.github.com> Closes modularml#2683 MODULAR_ORIG_COMMIT_REV_ID: 5ac8b1f3b45c75a964c5d9368e3871e7fc617a88
[External] [stdlib] Add method `atof()` to `String` This PR adds a function that can convert a `String` to a `Float64`. Right now it is implemented just for Float64 but maybe we should add other precisions? This supports the following notations: ```python "-1236.233" "2.25" "2." "1.7E+3" # as well as the f/F postfix notation "-1236.233f" "2.25F" "2.f" "1.7E+3F" ``` ORIGINAL_AUTHOR=Lukas Lipp <15105596+fknfilewalker@users.noreply.github.com> PUBLIC_PR_LINK=modularml#2649 Co-authored-by: Lukas Lipp <15105596+fknfilewalker@users.noreply.github.com> Closes modularml#2649 MODULAR_ORIG_COMMIT_REV_ID: b8d2c4ef38faa639e749957a0c1ba1a9c02a28cf
I don't think so, maybe the implementation used to be different and this was left over.
Yes.
Yes, but notice that the overload I suggested returns a fn atof(str_literal: StringLiteral) -> FloatLiteral: ... FloatLiteral is arbitrary precision, and they idea here would be that this function can only be invoked at compile time. For this to work, we need to make |
[External] [stdlib] Add method `strip()` to `StringRef` This PR adds a `strip` method to `StringRef`. This PR is helpful for modularml#2649. Where can I find the test cases for StringRef? ORIGINAL_AUTHOR=Lukas Lipp <15105596+fknfilewalker@users.noreply.github.com> PUBLIC_PR_LINK=modularml#2683 Co-authored-by: Lukas Lipp <15105596+fknfilewalker@users.noreply.github.com> Closes modularml#2683 MODULAR_ORIG_COMMIT_REV_ID: 5ac8b1f3b45c75a964c5d9368e3871e7fc617a88
[External] [stdlib] Add method `atof()` to `String` This PR adds a function that can convert a `String` to a `Float64`. Right now it is implemented just for Float64 but maybe we should add other precisions? This supports the following notations: ```python "-1236.233" "2.25" "2." "1.7E+3" "-1236.233f" "2.25F" "2.f" "1.7E+3F" ``` ORIGINAL_AUTHOR=Lukas Lipp <15105596+fknfilewalker@users.noreply.github.com> PUBLIC_PR_LINK=modularml#2649 Co-authored-by: Lukas Lipp <15105596+fknfilewalker@users.noreply.github.com> Closes modularml#2649 MODULAR_ORIG_COMMIT_REV_ID: b8d2c4ef38faa639e749957a0c1ba1a9c02a28cf
This PR adds a function that can convert a
String
to aFloat64
. Right now it is implemented just for Float64 but maybe we should add other precisions?This supports the following notations: