this post was submitted on 04 Jun 2025
1020 points (98.6% liked)

Programmer Humor

23861 readers
4140 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] Hadriscus@lemm.ee 8 points 1 day ago (3 children)

I know nothing about javascript, what is wrong with using + for math? perhaps naively, I'd say it looks suited for the job

[–] __Lost__@lemmy.dbzer0.com 15 points 1 day ago (1 children)

The correct way to do it is to load a 500mb library that has an add function in it.

[–] bitjunkie@lemmy.world 0 points 1 day ago* (last edited 1 day ago)

Point taken but the one I use is only ~200k for the whole package, ~11k for the actual file that gets loaded

[–] Quibblekrust@thelemmy.club 6 points 1 day ago

It's much better to make your own function that uses bitwise operations to do addition.

function add(a, b) {
    while (b !== 0) {
        // Calculate carry
        let carry = a & b;

        // Sum without carry
        a = a ^ b;

        // Shift carry to the left
        b = carry << 1;
    }
    return a;
}

(For certain definitions of better.)

[–] bitjunkie@lemmy.world 1 points 1 day ago* (last edited 1 day ago)

The native arithmetic operators are prone to floating point rounding errors