Python's decimals SOLVE the floating point problem!

Поделиться
HTML-код
  • Опубликовано: 6 янв 2025

Комментарии • 15

  • @eboyd53
    @eboyd53 День назад +9

    I learned from real world business programming decades ago to store amounts as integers and then divide by 10^precision to represent the amount. Mathematics with floating is not recommended if you need precision of the work. Accounting for instance needs to be precise and floating mathematics would always lose a penny or two depending on the number of operations.

  • @Axman6
    @Axman6 22 часа назад +4

    Haskell is full of interesting number representations; all the machine integer types from Word8 & Int8 to Word64 & Int64. The Integer type is like Python’s int, an arbitrary precision integer limited by the size of your RAM. Ratio takes two integral types and gives you a ration, with the type alias Rational = Ratio Integer - this gives as much accuracy as you like, as long as your computation is computable with rational numbers, so many trig functions but not irrational values like pi. Then there’s the Numeric modules which define CReal, a type that lets you represent many more arbitrary computations, and when it’s evaluated you specify the precision, so asking for sin(180°) will give you pi to as much precision as you want (if you wait long enough). The Scientific type is somewhat similar to a base 10 float, which is represented as an Integer base and an Int exponent - it’s used in the most popular JSON package to precisely represent any valid JSON, not just what an be stored in an ieee-754 double.

  • @boccobadz
    @boccobadz 38 минут назад

    Decimal type is almost everywhere because it's necessary for financial data.

  • @Dexter101x
    @Dexter101x День назад

    Shows you how much of a newbie I'm still am, I didn't know that we could type "py" in the terminal and do all the maths things, let alone a short version of running code. Thanks

  • @machadinhos
    @machadinhos День назад +1

    At the start of the video why is the camera so low? hahaha
    What a great video!

    • @Carberra
      @Carberra  День назад +1

      I forgot to raise the tripod lmao -- thankfully I realised for the rest I did in the batch!

  • @kirillsukhomlin3036
    @kirillsukhomlin3036 День назад +1

    It has been in many languages for at least 30 years.

  • @dipeshsamrawat7957
    @dipeshsamrawat7957 День назад

    Thank you 😊

  • @ryankulczycki4215
    @ryankulczycki4215 День назад

    what about one divided by three, store the answer, multiply the answer by three, intuitively this would be one - but does decimal give that answer?

    • @Carberra
      @Carberra  День назад +2

      No -- it gives 0.999... to as much precision as you like. I can see _why_ it's done that, and realistically however you would go about rounding or quantizing that it would be correct, but yeah that is a strange one. I guess it's just a byproduct of trying to be as exact as possible, and in doing so making no approximations whatsoever.

    • @interwebslinger
      @interwebslinger День назад

      It doesn't _necessarily_ give you 1, but you can get it to do so for most practical situations by quantizing it to a reasonable length, e.g. 10 decimals or whatever you need.
      In this sense I still find it more intuitive to work with than floats for most cases.

    • @costa_marco
      @costa_marco День назад +6

      @@interwebslinger You can use the 'fractions' module if you need exact rationals. If you need exact numbers, then you need to go full algebra like 'SymPy'.

    • @interwebslinger
      @interwebslinger День назад

      @@costa_marco I'll look into those, thanks!