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

Reproducer for S1905 FP: Floating point arithmetic #9520

Merged
merged 2 commits into from
Jul 12, 2024

Conversation

gregory-paidis-sonarsource
Copy link
Contributor

Repro for #9498

void Sample()
{
float x = 3.14f;
float res = 1 / (float)(x * 2); // Fixed
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We do not fix casts, only .Cast<T> and as T expressions.

Copy link
Contributor

@Tim-Pohlmann Tim-Pohlmann left a comment

Choose a reason for hiding this comment

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

For the reproducer, the result of the calculation does not change. To properly document this, we need to find an example where the change is visible.

As a side note, I do not know how we would implement a fix for this.

Comment on lines 124 to 125
float x = 3.14f;
float res = 1 / (float)(x * 2); // Noncompliant FP
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you find an example where the precision change is visible?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think it matters, as the solution would probably be what Jared suggested on the post.
Which is to be conservative and raise only in very few circumstances, when the RHS is a variable.

Even more conservative, we could also not raise on floating point arithmetic at all.

What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should document a case where this is actually an FP and think about a concrete solution later (or consider it a niche case and ignore it).

Copy link
Contributor

@Tim-Pohlmann Tim-Pohlmann left a comment

Choose a reason for hiding this comment

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

See the comment above

Copy link
Contributor

@Tim-Pohlmann Tim-Pohlmann left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link

sonarcloud bot commented Jul 12, 2024

Copy link

sonarcloud bot commented Jul 12, 2024

@gregory-paidis-sonarsource gregory-paidis-sonarsource merged commit 823fec5 into master Jul 12, 2024
29 checks passed
@gregory-paidis-sonarsource gregory-paidis-sonarsource deleted the greg/repro_9498 branch July 12, 2024 11:19
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