Skip to content

Less surprising parseFloat() handling of multiple '.' #2280

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

Closed
wants to merge 3 commits into from

Conversation

AbstractBeliefs
Copy link

When handling decimal points in incoming strings, break on any 2nd point.

If sending "1.2.3" with base, parseFloat() will return 1.23, which is probably not the intention.
With this update, it will return 1.20.

Here's a quick sketch to compare the difference: https://gist.github.com/AbstractBeliefs/3352f6fbd378375b70db

@mikaelpatel
Copy link

Shouldn't it return NAN?

@AbstractBeliefs
Copy link
Author

Possibly it should, but right now it doesn't and the docs confirm that's by design:
Serial.parseFloat() returns the first valid floating point number from the Serial buffer. Characters that are not digits (or the minus sign) are skipped. parseFloat() is terminated by the first character that is not a floating point number.

parseFloat() pulls chars off the buffer until it reaches something invalid - it doesn't have to understand the entire line, merely stop when we reach the end of the float.
Previous behaviour was to have . as an acceptable char in all cases, so:
1 (.2.3xyz in buffer)

  1.   (2.3xyz in buffer)
    

1.2 (.3xyz in buffer)
1.2 (3xyz in buffer, . ignored)
1.23 (xyz - end of float)

instead I feel a second '.' should be a break char rather than it being unconditionally acceptable - in essence, 1.2 is the first float in '1.2.3xyz' rather than 1.23.

@matthijskooijman
Copy link
Collaborator

Proposal and implementation looks good to me. @cmaglie, I think this can just be merged (and ported over to 1.5.x as well)?

@matthijskooijman matthijskooijman added Component: Core Related to the code for the standard Arduino API Type: Bug Version: 1.5.x and removed Version: 1.5.x labels Sep 11, 2014
@Chris--A
Copy link
Contributor

This can be resolved with: #3337

@ffissore ffissore added this to the Release 1.6.6 milestone Jun 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Core Related to the code for the standard Arduino API Type: Bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants