Lemmy: Bestiverse
  • Communities
  • Create Post
  • Create Community
  • heart
    Support Lemmy
  • search
    Search
  • Login
  • Sign Up
RSS BotBM to Lobste.rsEnglish · 1 month ago

Adding Floating-Point Decimals for Fun and Profit

blog.vero.site

external-link
message-square
0
link
fedilink
1
external-link

Adding Floating-Point Decimals for Fun and Profit

blog.vero.site

RSS BotBM to Lobste.rsEnglish · 1 month ago
message-square
0
link
fedilink
Many people know that you shouldn’t do decimal calculations, such as those involving U.S. dollars and cents, with the floating-point numbers in most programming languages. This is because decimal numbers can’t be expressed exactly as such floating-point numbers, so you will encounter rounding errors. Famously, with IEEE double-precision floats, 0.1 + 0.2 is not 0.3, but rather, 0.30000000000000004. On the other hand, I use a Python REPL to add up decimal numbers for receipts all the time. Of course, my stakes are lower; I’m summing things in the $1–$100 range and know to manually round the microscopic errors off before copying the sum somewhere. But actually it’s quite often that those errors don’t appear at all. If we sum every pair of multiples of 0.01 up to 1.00 and look at whether the printed result is too large (blue), too small (red), or correct, we get a cool pattern: Figure 1: How floating-point affects summing two multiples of 0.01, up to 1.00 Where does this pattern come from?

Comments

alert-triangle
You must log in or register to comment.

Lobste.rs

lobsters

Subscribe from Remote Instance

You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: !lobsters@lemmy.bestiver.se
lock
Community locked: only moderators can create posts. You can still comment on posts.

RSS Feed of lobste.rs

Source of the RSS Bot

Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 39 users / day
  • 163 users / week
  • 422 users / month
  • 1.49K users / 6 months
  • 2 local subscribers
  • 415 subscribers
  • 17K Posts
  • 1.23K Comments
  • Modlog
  • mods:
  • patrick
  • RSS BotB
  • BE: 0.19.21
  • Modlog
  • Instances
  • Docs
  • Code
  • join-lemmy.org