this post was submitted on 26 Feb 2024
754 points (95.8% liked)

Programmer Humor

20743 readers
554 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
[–] RustyNova@lemmy.world 1 points 3 weeks ago (1 children)

Good and bad use-cases for floats

Floats can be used everywhere where it doesn’t matter that you can’t store a 100% accurate base ten representations. For example positions and speeds in 3D games and animations, “analog” values like temperatures, speed of a vehicle, geo positions with longitude and latitude, a persons weight or heart pressure. In fact if you develop games there is no way around 32 bit floats because GPUs are f32 number crunching beasts. Modern 3D games wouldn’t be possible without all those fast f32 calculations.

You shouldn’t use binary floats if you need or expect accurate base ten calculations (addition, subtraction, multiplication, - note that divisions also introduce errors quickly in decimal types) and for dimensions that have a smallest unit that can’t be broken down, for example like money. If you need to handle money just store the amount of cents as integers and only divide by 100 in your display function.

This is exactly my point. Don't use floats when you need to get accurate stuff, but use it when you need a "feel" for it

[–] wischi@programming.dev 0 points 3 weeks ago

Don't use floats when you need to get accurate stuff

Floats are accurate. Could you name a situation (except money) where you think floats are not accurate enough to handle it?