this post was submitted on 30 Mar 2024
1 points (100.0% liked)

Programmer Humor

19154 readers
2012 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 1 year ago
MODERATORS
 

Courtesy of @[email protected] though not sure if he's the original author of it.

top 7 comments
sorted by: hot top controversial new old
[–] [email protected] 0 points 5 months ago (3 children)
[–] [email protected] 1 points 5 months ago

Positive or negative zero?

[–] [email protected] 1 points 5 months ago

It does. x-x == 0 is true unless x is NaN or infinity.

[–] [email protected] 0 points 5 months ago* (last edited 5 months ago) (1 children)

Are you sure? I've never seen that inequality before.

Edit: and at least python agrees with me

print(0.1 + 0.2)  # 0.300...0004
print(1.0-1.0)  # 0.0
[–] [email protected] 0 points 5 months ago (1 children)

I think it's equal zero in this case. I'd have to look up the IEEE specification to make sure. AFAIK it's just not guaranteed for any numbers and depends on the floating point implementation. A general rule of thumb for programmers is not to use 'equal' with floating point numbers.

[–] [email protected] 0 points 5 months ago* (last edited 5 months ago) (1 children)

The example is wrong, because they used 1.0.

But in general x-x does not have to equal 0, that is true. I'm pretty sure Nan and infinity would yield not 0.0, but Nan instead.

And if you reach x with two different calculations, e.g. x1 = a - b - c and x2 = a - c - b it is certainly not guaranteed that x1 - x2 == 0.0

[–] [email protected] 1 points 5 months ago

This is correct. Additionally, if x is NaN, then x ≠ x.