• psud@aussie.zone
    link
    fedilink
    arrow-up
    41
    arrow-down
    3
    ·
    1 year ago

    If working in currency, work in cents and divide by 100 and round to 2 decimals for output

          • psud@aussie.zone
            link
            fedilink
            arrow-up
            3
            ·
            edit-2
            1 year ago

            I just recalled, in that project I did have to divide money, which would leave fractional cents

            It was a budgeting program, I could put rogue cents where I liked. I think my solution made accounts due $12.553333333… (internally 1255.3333…) each pay period get 12.54, so after n/3 pay periods they’d be 2n cents over. I could deal with that imprecision.

    • janAkali@lemmy.one
      link
      fedilink
      English
      arrow-up
      20
      ·
      edit-2
      1 year ago

      Most languages have decimal libraries to correctly handle floating point arithmetics, where precision is necessary.

      • Slotos@feddit.nl
        link
        fedilink
        arrow-up
        1
        ·
        1 year ago

        They are as incapable of handling one third of a dollar as binary positional notation is incapable of handling one fifth (0.2).

        It’s not really a float problem. It’s a positional notation one. Some perfectly rational numbers refuse to squeeze into that mold.

    • Jajcus@kbin.social
      link
      fedilink
      arrow-up
      18
      ·
      1 year ago

      If working with currency use types and formating functions appropriate for currency. Not float.

      • psud@aussie.zone
        link
        fedilink
        arrow-up
        4
        arrow-down
        1
        ·
        edit-2
        1 year ago

        I was recalling a project in perl, which doesn’t have a variety of types. If you add values, you get a scalar, which will be a float if the numbers are not integers.

        I am aware my statement isn’t true in several languages

      • CanadaPlus@lemmy.sdf.org
        link
        fedilink
        arrow-up
        12
        ·
        1 year ago

        You’re telling me there’s someone that has more than 20 million dollars? /s

        If you’re handling people’s money you should probably be using arbitrary-precision arithmetic. I mean, you might get away with a long int, but finance is serious business and the amount of data you’re going to be processing relative to your funding is probably going to be small.

        • psud@aussie.zone
          link
          fedilink
          arrow-up
          1
          ·
          1 year ago

          Not the project I was thinking about above, but at work my team delivered software handling 13 digit numbers, but that’s in COBOL which does fine with money

    • Buckshot@programming.dev
      link
      fedilink
      arrow-up
      9
      ·
      1 year ago

      And remember not all currencies are 2dp so get a list and use the appropriate exponent.

      I had to update our currency database this week because there’s new currencies. It’s almost as bad as timezones.

    • redcalcium@lemmy.institute
      link
      fedilink
      arrow-up
      2
      ·
      1 year ago

      Some programming languages use different rounding method. Might bite you in the ass if you’re not aware of it and using multiple programming language in your application to handle different areas.

  • xigoi@lemmy.sdf.org
    link
    fedilink
    arrow-up
    26
    ·
    1 year ago

    If you’re working with floating point, you should be aware it’s just an approximation.

    • kevincox@lemmy.ml
      link
      fedilink
      arrow-up
      16
      arrow-down
      1
      ·
      1 year ago

      People think floats are too magical. Calling it an approximation is sort of leaning into this. Floats have limited precision like every other fixed size representation of a number.

      This is sort of saying that integers are an approximation because int(1.6) + int(2.6) = 5. What do you mean‽ Clearly 1.6 + 2.6 = 4.2 ~= 4!

      Floating points can’t perfectly represent 0.1 or 0.2 much like integers can’t represent 1.6. So it is rounded to the nearest representable value. The addition is then performed perfectly accurately using these values and the result is the rounded to the nearest representable value. (Much like integers division is “rounded”). That result happens to not be equal to the nearest representable value to 0.3.

      It is definitely a bit surprising. And yes, because of rounding it is technically approximate. But the way people talk about floating point makes it sound like it is some nebulous thing that gives a different result every time or just sort of does the correct thing.

      • r00ty@kbin.life
        link
        fedilink
        arrow-up
        7
        ·
        1 year ago

        I think specifically, they have amazing precision. But the boundaries just don’t fall perfectly on round numbers we humans would expect. That’s what gets people confused.

        Rounding can resolve these problems, or don’t use float if you don’t need to.

      • merc@sh.itjust.works
        link
        fedilink
        arrow-up
        3
        arrow-down
        1
        ·
        1 year ago

        The difference is that when you input a specific, precise floating point number, the number that’s stored isn’t what you entered.

        When you enter integers and store them in ints, as long as the number is small enough, what’s stored is exactly what you entered.

        If you tell your program that the radius of the circle is 0.2 units exactly, it says OK and stores 0.200000000000000011102230246251565404236316680908203125.

        Of course everybody knows that there’s a limit to how many digits get stored. If you tried to store Pi, there’s obviously some point where it would have to be cut off. But, in life we’re used to cutting things off at a certain power of 10. When we say Pi is 3.14 the numbers after the 4 are all zero. If we choose 3.14159 instead, it’s the numbers after the 9 that are zero. If we represent these as fractions one is 314/100 the other is 314159/100000. The denominator is always a power of 10.

        Since computers are base 2, their denominator is always a power of 2, so there’s a mismatch between the rounded-off numbers we use and the rounded-off numbers the computer uses.

  • HKPiax@lemmy.world
    link
    fedilink
    arrow-up
    10
    ·
    1 year ago

    I’ve always tried understanding this floating point thing, but I never could. Could anyone here please ELI5?

    • Rentlar@lemmy.ca
      link
      fedilink
      arrow-up
      58
      ·
      edit-2
      1 year ago

      My ELI5 is this. Pretend you have a robotic pizza cutter, but the only thing it can do with a pizza or pizza slice is cut it in half. If you ask for a tenth of a pizza, well it can give you an 1/8 or a 1/16 by repeatedly cutting a slice.

      It can also cut you a 1/16 slice, make 1/32, 1/256, 1/512 and 1/2048 slices separately, shift them together for you and be like: “There. Here’s 0.100098 of a pizza. You happy?”

      (You can also think of it as the robot cutting the pizza into 2048 slices and mushing together 205 of them to make your “tenth”).

      • HKPiax@lemmy.world
        link
        fedilink
        arrow-up
        14
        ·
        1 year ago

        This is absolutely brilliant. Perfect ELI5, thanks! Would you be so kind to make it so it’s about the binary thing with computers?

        • Rentlar@lemmy.ca
          link
          fedilink
          arrow-up
          4
          arrow-down
          1
          ·
          edit-2
          1 year ago

          Sure I can try to connect the metaphors, in a binary number system you have numbers 1 or 0 to pick from. How it applies in this scenario is that you can only have up to one slice of each size. That works because if you want two of it, you would instead replace it with a one size bigger slice that is equivalent to 2x the smaller size.

          For a general understanding of floating point I suggest you learn binary first and separately, because at the end of the day it’s just another way to write base-10 numbers. Floating point representation is useful because the basic principle is you pick some fraction that is so small relative to what you’re measuring, so you can do most math accurately. (e.g. If I say this dwarf planet is the size of our moon and a 20 tennis balls, the tennis balls don’t really change your idea of how big it is)

          At the end of the day, FP is like scientific notation but with like 10 significant digits.

    • jana@leminal.space
      link
      fedilink
      English
      arrow-up
      8
      ·
      1 year ago

      Computers are binary, yeah? So we have to represent fractional numbers with binary, too.

      In decimal, numbers past the decimal point are 10^-1, 10^-2, … etc. In binary, they’re 2^-1, 2^-2, …

      2^-1 is one half, so 0.1 in binary is 0.5 in decimal. 2^-2 is one quarter. 0.11 in binary is 0.75 in decimal. And of course you’ve got 0.01 = 0.25

      The problem comes when representing decimal numbers that don’t have neat binary representations. For instance, 0.1 in decimal is actually a repeating binary number: 0.0001100110011…

  • PixxlMan@lemmy.world
    link
    fedilink
    arrow-up
    4
    arrow-down
    1
    ·
    1 year ago

    To everyone commenting that you have to convert to binary to represent numbers because computers can’t deal with decimal number representations, this isn’t true! Floating point arithmetic could totally have been implemented with decimal numbers instead of binary. Computers have no problem with decimal numbers - integers exist. Binary based floating point numbers are perhaps a bit simpler, but they’re not a necessity. It just happens to be that floating point standards use binary.

      • PixxlMan@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        1 year ago

        Wrong. Sounds like you think only fixed point/precision could be implemented in decimal. There’s nothing about floating point that would make it impossible to implement in decimal. In fact, it’s a common form of floating point. See C# “decimal” type docs.

        The beginning of the Wikipedia article on floating point also says this: “In practice, most floating-point systems use base two, though base ten (decimal floating point) is also common.” (https://en.m.wikipedia.org/wiki/Floating-point_arithmetic) Also check this out: https://en.m.wikipedia.org/wiki/Decimal_floating_point

        Everything in my comment applies to floating point. Not fixed point.

        • JackbyDev@programming.dev
          link
          fedilink
          English
          arrow-up
          1
          ·
          1 year ago

          I generally interpret “decimal” to mean “real numbers” in the context of computer science rather than “base 10 numbers”. But yes, of course you can implement floating point in base 10, that’s what scientific notation is!