this post was submitted on 27 May 2024
134 points (94.1% liked)

Technology

59374 readers
6873 users here now

This is a most excellent place for technology news and articles.


Our Rules


  1. Follow the lemmy.world rules.
  2. Only tech related content.
  3. Be excellent to each another!
  4. Mod approved content bots can post up to 10 articles per day.
  5. Threads asking for personal tech support may be deleted.
  6. Politics threads may be removed.
  7. No memes allowed as posts, OK to post as comments.
  8. Only approved bots from the list below, to ask if your bot can be added please contact us.
  9. Check for duplicates before posting, duplicates may be removed

Approved Bots


founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 5 points 5 months ago (6 children)

Way too late for that. Every language I know makes some kind of auto conversion for numeric comparisons... and sometimes for strings as well.

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

I know of Rust, which is pedantic enough to not allow comparing integers to floats directly.

In certain situations, it even disallows making assumptions about equality and ordering between floats.

[–] [email protected] 2 points 5 months ago (2 children)

In certain situations, it even disallows making assumptions about equality and ordering between floats.

I'm guessing it does this when you define both floats in the same location with constant values.

The correct way to compare floats whose values you don't know ahead of time is to compare the absolute of their delta against a threshold.

i.e.

abs(a - b) <= 0.00001

The idea being that you can't really compare floats due to how they work. By subtracting them, you can make sure the values are "close enough" and avoid issues with precision not actually mattering all that much past the given threshold. If you define 2 constant values, though, the compiler can probably simplify it for you and just say "Yeah, these two should be the same value at runtime".

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

Rust has a warning (has it been promoted to error? I think it was supposed to be) about comparing floats. Nothing to do with same being const. You basically don't have an equality operator for them

load more comments (1 replies)
load more comments (3 replies)
load more comments (3 replies)