Swift should support all arithmetic operator types on floating-point numbers
| Originator: | brent | ||
| Number: | rdar://17428831 | Date Originated: | 23-Jun-2014 05:56 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode6-Beta2 (6A216f) |
| Classification: | Feature (New) | Reproducible: | Always |
Summary:
For integers, Swift supports two types of operators: regular operators throw a runtime exception on overflow, and overflowing operators ignore overflows. A third type, optional arithmetic (see <rdar://17428664> for my radar on this), received a positive response from Chris Lattner and may be added to Swift in the future.
However, Swift only supports one type of floating-point arithmetic. I believe Swift should include analogous arithmetic operators for floating-point numbers, only for treatment of NaN results instead of overflows.
Steps to Reproduce:
1. Type the following into a Swift playground:
// 0 / 0
// 0 &/ 0
// 0.0 / 0.0
// 0.0 &/ 0.0
2. Some of these lines will cause errors that will prevent you from seeing the results of others, so uncomment them one by one to see what their results are.
Expected Results:
The results should be as follows:
Runtime error
0
Runtime error
(not a number)
Actual Results:
The results are as follows:
Runtime error
0
(not a number)
Syntax error
Notes:
NaNs are a lot like integer overflow—they’re sometimes useful, but more often they’re a surprise that requires special handling. I think it would be safer for Swift to treat them as errors by default and only create them when they’re specifically expected.
You could probably make an argument for these variant operators to treat infinities the same way they treat NaNs, but personally, I don’t think that’s necessary. Infinities act a lot more like “normal” numbers than NaNs do.
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!