• wischi
    link
    fedilink
    arrow-up
    1
    ·
    23 hours ago

    That’s not really true and it depends on what you mean. If your decimal datatype has the same number of bits it’s not more accurate than base 2 floats. This is often hidden because many decimal implementations aren’t 64 bit but 128 bit or more. But what it can do is exactly represent base 10 numbers which is not a requirement for a lot of applications.

    You can use floats everywhere where you don’t need numbers to be base 10. With base 2 floats the operations couldn’t be more accurate given the limit of 64 bits. But if you write f64 x = 0.1; and one assumes that the computer somehow stored 0.1 inside x they already made a wrong assumption. 0.1 can’t be converted into a float because it’s a periodic in base 2. A very very pedantic compiler wouldn’t even let you compile that and force you to pick a value that actually can be represented.

    Down the rabbit hole: https://zeta.one/floats-are-not-inaccurate/

    • RustyNova@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      18 hours ago

      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
        link
        fedilink
        arrow-up
        1
        ·
        16 hours 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?