Skip to content

[SIL Diagnostics] Warn when assigning an integer literal to a variable of floating-point type results in loss of precision. #17274

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

Merged
merged 1 commit into from
Jun 22, 2018

Conversation

ravikandhadai
Copy link
Contributor

This feature was requested in the radar issue: rdar://15224041.

@ravikandhadai
Copy link
Contributor Author

The warnings are disabled when the conversion happens through explicit constructor call. E.g.

$ let f3: Float = 16777217
warning: '16777217' loses precision during conversion to 'Float'
$ Float(16777217) // no warning

@ravikandhadai
Copy link
Contributor Author

@swift-ci please smoke test

@stephentyrone
Copy link
Contributor

Would it be more clear if we tell the user what value actually results? Maybe something along the lines of: "Warning: '16777217' is not exactly representable as 'Float'; 'f3' has value '16777216'.

@ravikandhadai
Copy link
Contributor Author

@swift-ci please smoke test

@ravikandhadai
Copy link
Contributor Author

@stephentyrone Sounds good to me. I have pushed a commit that prints the new value in the warning messages. E.g.

$ let f3: Float = 16777217
warning: '16777217' is not exactly representable as 'Float'; it becomes '16777216'

@stephentyrone
Copy link
Contributor

Nice! If you update the PR with conflicts resolved, I can review later today or tomorrow.

// 2^128 * (2^25-1)/2^25
var _ /*float32_max*/ : Float32 = (340282346638528859811704183484516925440) // in hexfloat = 0x1.fffffep127

var _ /*float32_near_max_but_imprecise*/ : Float32 = (340282326356119256160033759537265639425) // expected-warning {{'340282326356119256160033759537265639425' is not exactly representable as 'Float32' (aka 'Float'); it becomes '3.40282326E+38'}}
Copy link
Contributor

Choose a reason for hiding this comment

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

We probably want to be printing these with full precision, to make it clear what the difference is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jrose-apple Yeah sure, let me make that change.

@ravikandhadai
Copy link
Contributor Author

@swift-ci please smoke test

@ravikandhadai
Copy link
Contributor Author

@jrose-apple Done. Now the new float value is displayed like an integer to make the imprecision obvious E.g.
var _ : Float32 = (340282326356119256160033759537265639425)
> warning: '340282326356119256160033759537265639425' is not exactly representable as 'Float32' (aka 'Float'); it becomes '340282326356119256160033759537265639424'

@stephentyrone I have fixed the merged conflicts. Thanks for your help with this.

@ravikandhadai
Copy link
Contributor Author

@swift-ci please smoke test

Copy link
Contributor

@stephentyrone stephentyrone left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@ravikandhadai
Copy link
Contributor Author

@swift-ci please test and merge

2 similar comments
@ravikandhadai
Copy link
Contributor Author

@swift-ci please test and merge

@shahmishal
Copy link
Member

@swift-ci please test and merge

variable of floating-point type results in loss of precision.
<rdar://15224041>
@ravikandhadai
Copy link
Contributor Author

@swift-ci please test and merge

1 similar comment
@ravikandhadai
Copy link
Contributor Author

@swift-ci please test and merge

@swift-ci swift-ci merged commit 395aa00 into swiftlang:master Jun 22, 2018
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.

5 participants