Skip to content
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

seq: parse "infinity" and "-infinity" #5124

Merged
merged 3 commits into from
Sep 30, 2023
Merged

Conversation

shinhs0506
Copy link
Contributor

No description provided.

@tertsdiepraam
Copy link
Member

Hmm, I wonder if this is actually better? I think the comment in the code might have expected the parsing to simplify more than it actually did. The original is pretty clear and does not do redundant parsing of the float. Maybe we can refactor the original to something like this:

let float_val = match s.to_ascii_lowercase() {
   "inf" => ExtendedBigDecimal::Infinity,
   "-inf" => ExtendedBigDecimal::MinusInfinity,
   "nan" | "-nan" => return Err(ParseNumberError::Nan),
   _ => return Err(ParseNumberError::Float)
};

Ok(PreciseNumber::new(float_val, 0, 0))

@shinhs0506
Copy link
Contributor Author

im just assuming floating point comparison to be cheaper than string comparison. Its just unfortunate that the code doesn't look as clean because match statement is unusable in this case.

@tertsdiepraam
Copy link
Member

im just assuming floating point comparison to be cheaper than string comparison.

I'm not so sure actually, because the floating point parsing needs to do string comparisons as well to parse it in the first place. Even though it does it in a clever way: https://doc.rust-lang.org/src/core/num/dec2flt/parse.rs.html#200-243. In any case, if it's just for performance reasons, we need to back it up with benchmarks.

@shinhs0506
Copy link
Contributor Author

shinhs0506 commented Aug 6, 2023

I did some benchmarking and looks like theres no noticeable speed improvement. However, the current code still fails when given "infinty" and "-infinity" as arguments which can be fixed with this pr.

@github-actions
Copy link

github-actions bot commented Aug 6, 2023

GNU testsuite comparison:

Skipping an intermittent issue tests/rm/rm1

@shinhs0506 shinhs0506 changed the title seq: update inf and nan parsing seq: parse "infinity" and "-infinity" Aug 6, 2023
@github-actions
Copy link

github-actions bot commented Aug 6, 2023

GNU testsuite comparison:

Skipping an intermittent issue tests/rm/rm1

@tertsdiepraam
Copy link
Member

Good catch on the (-)infinity! That is indeed an improvement and something we should support, however we could do that with the new version I proposed quite easily too.

Why do you think this is better than string comparison?

@shinhs0506
Copy link
Contributor Author

shinhs0506 commented Aug 11, 2023

I don't necessarily think one is better than the other. Scratch what i said about float vs string comparison.
However, with what you have proposed, if IEEE 754 gets updated for some reason, we would also have to update our code accordingly.

@jfinkels
Copy link
Collaborator

I wrote that comment originally, but now that I'm looking at it, I also agree with @tertsdiepraam that it's not really any more practical to use f32 parsing than it is to just check for "inf", "nan", etc. as strings. Maybe if we were actually going to use the f32 value here, then it would be useful, but we just return an error in that case anyway. So I suggest we just remove the comment (and possibly take the simplification suggested here: #5124 (comment) )

@shinhs0506
Copy link
Contributor Author

@jfinkels thanks for the input, i have updated the code

Copy link
Member

@tertsdiepraam tertsdiepraam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Thanks! I'm rerunning the FreeBSD because something is up with it, but it looks ready!

@cakebaker cakebaker merged commit bc7877b into uutils:main Sep 30, 2023
44 of 45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants