Skip to content

Conversation

@velievrauf
Copy link

I suggest to add stringToDouble feature that extends the existing functional

int decimalIndex = -1;
if (s.size > 0 && s.data[0] == '-') {
sign = -1;
stringRemove(&s, 0, 1);
Copy link
Owner

Choose a reason for hiding this comment

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

Deleting the first element takes O(n), it's too much
It'll be better just set some sort of start_position to 1 and then start loop not from 0 but from start_position

}
if (stringCharIsDigit(s.data[i])) {
result = result * 10.0 + (s.data[i] - '0');
}
Copy link
Owner

Choose a reason for hiding this comment

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

Else (when it's not a digit) set error like "Not a Number" using setError()

About error handling here

if (stringCharIsDigit(s.data[i])) {
result += (s.data[i] - '0') / divisor;
divisor *= 10.0;
}
Copy link
Owner

Choose a reason for hiding this comment

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

Also setError()

TString s1 = stringInitWithCharArr("3.14159");
TString s2 = stringInitWithCharArr("123");
TString s3 = stringInitWithCharArr("-100");

Copy link
Owner

Choose a reason for hiding this comment

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

Add test with empty string or invalid double -12pas7 8.14

P.S. you forgot to call this test in main()

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.

2 participants