Beehaw
  • Communities
  • Create Post
  • search
    Search
  • Login
  • Sign Up
Trev13 ( Trev13@programming.dev )  to Programming@programming.dev · 1 year ago

Big Decimals: Stop Using Floats or Cents for Money

medium.com

external-link
message-square
19
link
fedilink
63
external-link

Big Decimals: Stop Using Floats or Cents for Money

medium.com

Trev13 ( Trev13@programming.dev )  to Programming@programming.dev · 1 year ago
message-square
19
link
fedilink
Just a moment...
medium.com
external-link
alert-triangle
You must log in or # to comment.
  • Mark with a Z ( markz@suppo.fi ) 
    link
    fedilink
    arrow-up
    19
    ·
    1 year ago

    Stop Using Floats

    no shit

    or Cents

    huh…?

    That was a good point.

    • underscores ( underscores@lemmy.zip ) 
      link
      fedilink
      English
      arrow-up
      2
      ·
      1 year ago

      I think maybe they meant using integers for cents

  • randy ( randy@lemmy.ca ) 
    link
    fedilink
    arrow-up
    11
    ·
    1 year ago

    I got hung up on this line:

    This requires deterministic math with explicit rounding modes and precision, not the platform-dependent behavior you get with floats.

    Aren’t floats mostly standardized these days? The article even mentions that standard. Has anyone here seen platform-dependent float behaviour?

    Not that this affects the article’s main point, which is perfectly reasonable.

    • nimpnin ( nimpnin@sopuli.xyz ) 
      link
      fedilink
      arrow-up
      18
      ·
      1 year ago

      Mostly standardized? Maybe. What I know is that float summation is not associative, which means that things that are supposed to be equal (x + y + z = y + z + x) are not necessarily that for floats.

    • KRAW ( KRAW@linux.community ) 
      link
      fedilink
      English
      arrow-up
      17
      ·
      1 year ago

      The IEEE standard actually does not dictate a rounding policy

    • a1studmuffin ( a1studmuffin@aussie.zone ) 
      link
      fedilink
      English
      arrow-up
      10
      ·
      1 year ago

      Floating-Point Determinism | Random ASCII - tech blog of Bruce Dawson https://randomascii.wordpress.com/2013/07/16/floating-point-determinism/

      The short answer to your questions is no, but if you’re careful you can prevent indeterminism. I’ve personally ran into it encoding audio files using the Opus codec on AMD vs Intel processors (slightly different binary outputs for the exact same inputs). But if you’re able to control your dev environment from platform choice all the way down to the assembly instructions being used, you can prevent it.

      • randy ( randy@lemmy.ca ) 
        link
        fedilink
        arrow-up
        3
        ·
        1 year ago

        Thanks, that’s an excellent article, and it’s exactly what I was looking for.

    • bleistift2 ( bleistift2@sopuli.xyz ) 
      link
      fedilink
      English
      arrow-up
      2
      ·
      1 year ago

      If you count the programming language you use as ‘platform’, then yes. Python rounds both 11.5 and 12.5 to 12.

      • FizzyOrange ( FizzyOrange@programming.dev ) 
        link
        fedilink
        arrow-up
        2
        ·
        1 year ago

        That is default IEEE behaviour: https://en.wikipedia.org/wiki/Rounding#Rounding_half_to_even

        This is the default rounding mode used in IEEE 754 operations for results in binary floating-point formats.

        Though it’s definitely a bad default because it’s so surprising. Javascript and Rust do not do this.

        Not really anything to do with determinism though.

    • pinball_wizard ( pinball_wizard@lemmy.zip ) 
      link
      fedilink
      arrow-up
      1
      ·
      1 year ago

      The real standard is whatever Katherine in accounting got out of the Excel nightmare sheets they reconcile against.

  • ∃∀λ ( eah@programming.dev ) 
    link
    fedilink
    arrow-up
    3
    ·
    1 year ago

    I become suspicious when I see a Medium user posting well-written deep articles as frequently as this user appears to be doing. How can we tell whether this is AI slop or not?

    • jasory ( jasory@programming.dev ) 
      link
      fedilink
      arrow-up
      3
      ·
      1 year ago

      Their articles aren’t that deep and they mostly focus on similar topics.

      I think it’s perfectly possible for someone to have a backlog of work/experience that they are just now writing about.

      If it were AI spam, I would expect many disparate topics at a depth slightly more than a typical blog post but clearly not expert. The user page shows the latter, but not the former.

      However, the Rubik’s cube article does seem abnormal. The phrasing and superficiality makes it seem computer-generated, a real Rubik’s afficionado would have spent some time on how they cube.

      Of course I say this as someone much more into mathematics than “normal” software engineering. So maybe their writing on those topics is abnormal.

  • Kissaki ( Kissaki@programming.dev ) 
    link
    fedilink
    English
    arrow-up
    3
    ·
    1 year ago

    Scroll to the second paragraph, get a subscribe popover. So annoying. I haven’t even read any reasonable amount of content yet.

    • Vulwsztyn ( Vulwsztyn@programming.dev ) 
      link
      fedilink
      arrow-up
      1
      ·
      11 months ago

      “stop using medium”

  • falseWhite ( falseWhite@programming.dev ) 
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    8 months ago

    deleted by creator

    • TehPers ( TehPers@beehaw.org ) 
      link
      fedilink
      English
      arrow-up
      1
      ·
      1 year ago

      The medium (lol) is annoying, but it didn’t ask me to pay. Is the article not free for you?

  • Vulwsztyn ( Vulwsztyn@programming.dev ) 
    link
    fedilink
    arrow-up
    1
    ·
    11 months ago

    Stop using floats and cents for money medium ffs

  • MonkderVierte ( MonkderVierte@lemmy.zip ) 
    link
    fedilink
    arrow-up
    1
    ·
    1 year ago

    deleted by creator

  • DarkAri ( DarkAri@lemmy.blahaj.zone ) 
    link
    fedilink
    arrow-up
    1
    ·
    1 year ago

    You could just limit the precision of the float. If you are writing banking software or something you could just add a special case to flip flop the remainder or something. I think pretty much all modern languages support this. You should also be using doubles for any numbers that could potentially grow really large like fiat currencies. The issues with floats really is that it will often favor precision over range. You could end up with lots of numbers after a decimal and very little integer range which could cause overruns or something.

    • TehPers ( TehPers@beehaw.org ) 
      link
      fedilink
      English
      arrow-up
      3
      ·
      1 year ago

      The article goes into depth about what you should be using. Floats and doubles are not designed for use with base 10 fractions. They’re good at estimating them, but not accurate enough for real financial use.

      There’s also not much reason to reinvent the wheel for an already solved problem. Many languages have this data type already built into the language, and the rest usually have it available through a package.

      • DarkAri ( DarkAri@lemmy.blahaj.zone ) 
        link
        fedilink
        arrow-up
        2
        ·
        1 year ago

        Oh nice I’m far from a professional programmer. Thxs for the explanation.

Programming@programming.dev

programming@programming.dev

Subscribe from Remote Instance

Create a post
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: !programming@programming.dev

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person’s post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you’re posting long videos try to add in some form of tldr for those who don’t want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



Visibility: Public
globe

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

  • 57 users / day
  • 336 users / week
  • 1.68K users / month
  • 4.15K users / 6 months
  • 449 local subscribers
  • 28.6K subscribers
  • 2.93K Posts
  • 22.9K Comments
  • Modlog
  • mods:
  • snowe ( snowe@programming.dev ) 
  • Ategon ( Ategon@programming.dev ) 
  • UlrikHD ( UlrikHD@programming.dev ) 
  • bugsmith ( bugsmith@programming.dev ) 
  • Spyro ( Spyro@programming.dev ) 
  • BE: 0.19.15
  • Modlog
  • Legal
  • Instances
  • Docs
  • Code