4 ms·
if we can do 1 + 1, we should be able to do vec2 + vec2, same with mat4 * mat4 Odin proved it that it can be made efficiently while keeping sanity
by Kukumber 4y ago
if we can do 1 + 1, we should be able to do vec2 + vec2, same with mat4 * mat4
Odin proved it that it can be made efficiently while keeping sanity
- stephc_int13 4y agoThe thing about operator overloading is that it was abused during one of the worst era of C++. And vec2 + vec2 can probably be unambiguously translated to assembly code while mat4 * mat4 is an other story. In most cases it should be a function call, and not a trivial one, with SIMD it can be relatively fast but still several orders of magnitude slower than 1 + 1. (we're talking about more than 500 scalar-equivalent operations) And unfortunately, from my experience, if you let people (especially new coders) use this kind of powerful syntactic sugar they tend to ignore the performance characteristics because it look so simple and basic, like if the CPU had a special instruction to multiply two 4x4 matrices. I prefer when the function call is explicit, it is a bit more cumbersome to write, but there is less hidden complexity.
- Kukumber 4y ago> I prefer when the function call is explicit, it is a bit more cumbersome to write, but there is less hidden complexity. you hide and obfuscate basic operations with functions, that's worse, specially when you have to chain arithmetic operations, with functions it becomes ugly and unreadable
- stephc_int13 4y agoShow me an example and I am pretty sure that I can rewrite it in a way that is neither ugly nor unreadable.
- stephc_int13 4y agoMatrix multiplication or even worse, matrix inversion, is NOT a basic operation. This is exactly the point.
- littlestymaar 4y agoInteger division isn't a simple one either though… Matrix multiplication are “basic operations” in the sense that you use them as the basic block of your algorithms, and in code using such blocks you really appreciate having a simple operator for those instead of littering your formula with functions calls (which is basically writing your formulas in Polish notation, not the most legible way to write formulas …)
- dolmen 4y agoMatrix multiplication is a niche in the programming world.
- littlestymaar 4y agoThe programming world is made of niches. That's why arithmetic operator overloading is nice, the language doesn't need to know which niche I'm in to satisfy my needs.
- cycomanic 4y agoI somewhat agree with you, I argue that mat*mat should be an element wise operation and use a different operator for matrix operations (like numpy does it).
- kllrnohj 4y ago> Matrix multiplication or even worse, matrix inversion, is NOT a basic operation. Neither is dividing floats, especially not on a softfloat & softdiv architecture. And yet, Zig is perfectly OK with you using division between 2 floats. Why does it get to insert expensive function calls behind operators but I can't? Zig also lets you do remainder division for floats, which is also not a "basic operation." It's slower even then taking the sqrt of a float! Zig then also has specialized operators like saturating addition, which also isn't a "basic operation" And then Zig also has `*` for array multiplication and `++` for array concatenation. Those are compile-time only, but still deviates from "basic operations only" territory surely. And also Zig overloads `||` to allow for the merging of enums (sorry, "error sets"), rather than only being a boolean OR.
- naikrovek 4y ago> if we can do 1 + 1, we should be able to do vec2 + vec2, same with mat4 * mat4 why? operator overloading doesn't help you solve any problems. you can have the readability with methods which are named appropriately. operator overloading seems so powerful and useful until you realize one day that it only changes the appearance of things, and makes no difference whatsoever to anything you are actually doing.
- cycomanic 4y agoBut we are using operator overloading already, + does work for the basic numeric types doesn't it? So why should the line be drawn at float or double (zig didn't have a complex type last I looked), why not vec?
- Kukumber 4y agoi'm not asking for operator _overloading_, i am asking for having operators for my math types
- renox 4y agoGood point, you could have + * / restricted to basic types, but have #* #+ #mod_add as (overloadable) operators.
- adgjlsfhk1 4y agoreadability is important. specifically, operator overloading is great because it makes code look more like math. when translating from math to code, this is highly useful.
- WalterBright 4y agoHere's a sweet example of proper use of operator overloading: https://dlang.org/phobos/std_checkedint.html https://dlang.org/phobos/std_checkedint.html where operator overloading is used to create variations on integer types, like specifying the behavior when overflow happens. Besides, `a + b / (c * d)` is far more readable than `add(a, div(b, mul(c, d)));