That's a defensible position indeed. Same argument applies to other project/target/file settings but it should not be used as a justification to allow it.
Speaking of scoped access, it's allowed on a function level, could it be also allowed here with some compiler support?
protocol RelaxedMath {} // Compiler knows about this marker protocol
extension Double: RelaxedMath {}
extension Double {
init(relaxed: RelaxedMath) {
// some magic here
}
}
Usage example:
func foo(a: Double, b: Double, c: Double, d: Double) {
let x = a * b + c * d) // normal math
let y = Double(relaxed: a * b + c * d) // relaxed math
let z = a * b // normal math
let t = Double(relaxed: z + c * d) // relaxed math for z, c, d
}
If not that - my preference is for a function marker attribute,
and failing that for the ~+~*, ... spelling.
This is a great feature, but the spelling is a trap.
In particular people who are not paying a lot of attention can easily learn that &+ is “somehow faster” because when used with Float and Double blah-blah-blah (and then their eyes glaze over and they totally miss the “with Int and Int64 they mean a totally different thing”) and then they start using it anywhere, or anywhere they “want fast”. To a small extent they are even right (checking for overflow traps is low but not zero cost), but they also basically just adopted unsafe C-style integer math and unlocked some unsafe C-style attacks against anything they write. Fun times.
I prefer the Double(relaxed: expr) variant from above, but I’m also fine with choosing an alternative punctuation for “relaxed” that is not confusable with “bitwise and the result”.
Yes, but Swift has chosen for those to be “safe”, in that Int + traps and won’t leave you with a value you will regret using, and FP + gives you an infinity which…welll…is kind of not a value you will regret using?
Or less flippantly those edge cases have a decent outcome for the unwary while the ampersand version has a potentially disastrous outcome for the unwary in half the cases. True it is the half the cases that this proposal is not inventing.
So I think my argument that &+, &* and friends as proposed for Double and Floating are an attractive nuisance for the Int/IntN/UIntN type’s operators of the same spelling. One set should read “danger, could be a security risk” while the other should read “faster and maybe more accurate but has platform differences”…but both look the same.
I think &+ is much more of a security risk for integers than for floats. In both cases, the operator is not “unsafe” in the sense that Swift uses the term.
When faced with a floating point operation that is faster than the standard operation, less accuracy is almost a given, otherwise it would be the standard operation. I would not worry too much about abuse from the unwary in this regard, in the same way I don’t see people abusing &+ for ints today.
I always read &+ not so much as 'addition without overflow trap' specifically but more like 'arithmetic without the guard rails', so for me it feels fitting as a relaxed float operator.
&+ is already "somehow faster" for integer arithmetic for exactly the same reason that it's faster for floating-point: it makes the operations associative, which enables vectorization, and it permits multiply-add formation (because it does not need to respect possible intermediate traps, rather than intermediate rounding for floating-point). So I would argue that this hazard already exists.
Ok, we agree they are caused by the same thing, but my problem is less that the “and it is faster” isn’t true. My problem is one sometimes for reasons people don’t always understand makes the low bits differ, while the other for reasons people don’t always understand makes the HIGH bits differ.
The result of not understanding everything about what is going on is different.
Which might be acceptable. A language shouldn’t be limited just because some people don’t understand what is going on in every case. It depends on how much we feel that avoiding a particular spelling for a feature is “limiting” a language as opposed to making things clearer to people that don’t fully understand what is going on, including LLMs as well as people that totally do know what the difference is but are in a hurry to just knock out this code review and get to lunch and thus might overlook something they do actually know.
Yes, my point is that the level of risk is radically different so someone that first encounters &+ for one may misapply it for the other without realizing how exactly they differ (one differs in the low order bits, the other in the high order bits...the low order bits might not make a difference at all, or the maybe make to shapes not intersect and you get a unhappy 3D model for printing...high order bits in an integer can be vastly unhappier)
I think that if a program is going to be broken by inappropriate use of relaxed operators by somebody who doesn't understand what they're writing then there's a good chance it will be equally broken by them writing an inappropriate order of operations in the first place.
I'm indifferent about what name is used, whether &+, &*, etc. or something else, but I do think that controlling the behaviour through individual operators is considerably preferable than by directives, scopes, flags, or whatever.
No, I would expect the compiler to choose the best instructions for the order I specify using normal notation, with brackets where necessary. If I ask for 2 * 3 / 4 then I want the multiplication first, not 2 / 4 * 3 even if the latter happened to be much faster. But if the order is numerically unimportant then I might like the compiler to choose the best one for me, in which case it would be nice to write 2 &* 3 &/ 4 or somesuch to indicate this.
I agree with others' comments that this is best controlled by operators instead of a blanket switch at some wider scope or higher level. I'm not averse in principle to ideas like Double(relaxed:) or new types like a FastDouble, but while the numerical problems associated with the optimisations are caused by the nature of the types, they are manifested at the operator level and so that is where the control should lie, in my opinion.