One of C’s most commonly acknowledged shortcomings is the lack of operator overloading. If you’re writing code that deals with math or physics, and is dealing with vectors, matrices and quaternions, you’d have to wrap everything in function calls and that can get annoying very fast.

A few years ago, I stumbled across a trick from John Carmack in the original Quake source code. It’s super simple, and makes writing vector math in C a hell of a lot easier, and I haven’t seen anyone talk about it.

Let’s say, you want to compute E in this imaginary formula where A, B, C, D, E are all 3D vectors:

(E = (A \cdot B) + (C - D) \cdot \sqrt{\pi})

E = (A * B) + (C - D) * sqrt(pi)

With using the traditional approach of using helper functions, the code would look something like…

Similar Posts

Loading similar posts...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help