• morrowind@lemmy.ml
    link
    fedilink
    English
    arrow-up
    11
    ·
    1 year ago

    Ok but actually though, can someone explain why we can’t use integers to emulate floats. Why is there a dedicated fpu

    • bl_r@lemmy.dbzer0.com
      link
      fedilink
      English
      arrow-up
      18
      ·
      1 year ago

      It’s been a long while since I last dealt with the float specification (IEE 754), but what I recall is this:

      Floating points allow better precision along a wide range of numbers with a smaller data type compared to fixed point data in most cases. Fixed point can often be emulated by integers. Its far from perfect, but for scientific computing it’s often good enough.

      If you use integers to emulate floats, there will be less precision in either the integer part, or the decimal part. You have to make a tradeoff. If you know about what types of data you will be collecting, you can get better precision compared to floating point.

      Integer arithmetic can be implemented with simple logic gate layouts, and in most processors, addition/subtraction/multiplication takes very few cycles, and is generally done with a single assembly instruction. IIRC, you can, for the most part, use standard add/subtract with fixed point numbers. Floating point operations, on the other hand, are much more complex due to the nature of floating point numbers potentially having the decimal in different places. In order to break that down into standard integer based assembly code, you must do many more steps. In order to make things faster, you can create dedicated processors with more complex logic gate layouts designed around floats (FPUs) for significantly faster floating point operations.

    • anton
      link
      fedilink
      English
      arrow-up
      6
      ·
      1 year ago

      Let’s say our integers are 64 bit, say we put half before the point. Now our largest value is just below 4 294 967 296, a 32 bit float goes to 3.4*10^38. The representation also enables some operations to be faster (multiplication, division iirc).

      • TheChurn@kbin.social
        link
        fedilink
        arrow-up
        6
        ·
        1 year ago

        Floats have a wider range, at the cost of not having full coverage over that range. Even the integers in that range cannot be exactly represented

  • theblueredditrefugee@lemmy.dbzer0.com
    link
    fedilink
    English
    arrow-up
    2
    ·
    1 year ago

    I feel like a lot of conditionals involving floats make more sense if you convert it to an integer and do bitwise operations lmao. Like to check if something is a real number just check to see if the exponent is not in the space used by infinities and nans