aetilius / pHash

pHash - the open source perceptual hash library
https://www.phash.org
GNU General Public License v3.0
565 stars 81 forks source link

Rounding logic looks strange #25

Closed ams2990 closed 2 years ago

ams2990 commented 3 years ago

There's a common rounding pattern throughout phash.cpp like (int)std::floor(d + ROUNDING_FACTOR(d)) where ROUNDING_FACTOR is #define ROUNDING_FACTOR(x) (((x) >= 0) ? 0.5 : -0.5). This produces some unusual results:

-1.0  → -2
-0.75 → -2
-0.5  → -1
-0.25 → -1
 0.0  →  0
 0.25 →  0
 0.5  →  1
 0.75 →  1
 1.0  →  1

The weirdest one there is -0.75 rounds to -2, which seems wrong. I'd be happy to fix this, but I can't tell what the desired behavior is.

aetilius commented 3 years ago

Hi @ams2990 you’re right this does seem like a bug. I’m not sure the rounding mode matters much as long as it’s consistent. Would be happy to review a PR.