Python's decimals SOLVE the floating point problem!

  Рет қаралды 4,417

Carberra

Carberra

Күн бұрын

Пікірлер: 31
@Axman6
@Axman6 16 күн бұрын
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.
@alian714
@alian714 14 күн бұрын
another haskeller in the wild, pog
@Dexter101x
@Dexter101x 16 күн бұрын
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
@Carberra
@Carberra 16 күн бұрын
Best calculator in the world!
@valtersanches3124
@valtersanches3124 16 күн бұрын
It's the REPL
@eboyd53
@eboyd53 16 күн бұрын
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.
@OtherTheDave
@OtherTheDave 14 күн бұрын
Unfortunately, most people just use Excel for financial stuff and assume it’s right.
@MoiMagnus1er
@MoiMagnus1er 14 күн бұрын
​@@OtherTheDaveI'm still frustrated that Excel doesn't use shifted floating points by a factor 100. That would mean using integer arithmetics for number with only 2 digits after the point, and only having precision issues from floats at the third digit (which is rarely used).
@ChasRMartin
@ChasRMartin 10 күн бұрын
Good old cobol
@eboyd53
@eboyd53 10 күн бұрын
@@ChasRMartin I actually learned this using DEC BASIC. COBOL storage would have limited the amount of floating point math being done.
@BboyKeny
@BboyKeny 9 күн бұрын
I built a project management + billing system and this is how I did it. For example VAT of 21%, I do "price in cents times 121" and then divide by 10000 just before presenting the price.
@boccobadz
@boccobadz 15 күн бұрын
Decimal type is almost everywhere because it's necessary for financial data.
@MLGJuggernautgaming
@MLGJuggernautgaming 14 күн бұрын
The syntax you use for f strings, how does that work? f”{ x=}”
@ryoschinlot9153
@ryoschinlot9153 14 күн бұрын
I assume it is a pre-defined python thing, however the addition of this syntax contributes to feature creep, imo. Especially because the alternative is no more than `print(f"x = {x}")`
@MLGJuggernautgaming
@MLGJuggernautgaming 13 күн бұрын
@@ryoschinlot9153 Yeah yet another case where is more confusing than before.
@anon_y_mousse
@anon_y_mousse 14 күн бұрын
I know it's not the intent for them to be useful for all math operations, but it'd still be nice if you could use the trigonometric functions. It looks like it stores them in a similar manner to `bc`, which I find strange. Also, I applaud your use of a shorthand when importing. I see so many people not bother to rename imports and use long and unwieldy names and it's frankly weird to me. Of course, it would be even easier if Python would add UDL's like C++ did. If you use C++ at all, I recommend you look up UDL's and make use of them, they are awesome.
@scarletevans4474
@scarletevans4474 14 күн бұрын
2:10 is such rule of rounding really used while dealing with money too? Logic would say that rounding half a cent like that will lead to MILLIONS of difference over the course of billion transactions, where exactly do they use such rounding? And who takes that extra money that comes from it? 😀 (probably the bank does, but who wouldn't want a "free money" from rounding cents!)
@machadinhos
@machadinhos 17 күн бұрын
At the start of the video why is the camera so low? hahaha What a great video!
@Carberra
@Carberra 16 күн бұрын
I forgot to raise the tripod lmao -- thankfully I realised for the rest I did in the batch!
@kirillsukhomlin3036
@kirillsukhomlin3036 16 күн бұрын
It has been in many languages for at least 30 years.
@dipeshsamrawat7957
@dipeshsamrawat7957 17 күн бұрын
Thank you 😊
@cmd_midac0522
@cmd_midac0522 15 күн бұрын
I prefer using mpmath (with gmpy2) for precise python calculations.
@ryankulczycki4215
@ryankulczycki4215 16 күн бұрын
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 16 күн бұрын
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 16 күн бұрын
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 16 күн бұрын
@@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 16 күн бұрын
@@costa_marco I'll look into those, thanks!
@00wheelie00
@00wheelie00 15 күн бұрын
Irrelevant for financial (accounting) data: you are never ever going to have an irrational monetary amount ever. That said, there is plenty of financial models that do not need this accuracy and are much much faster with floating point math vs fixed point math, because of available hardware. But even then you always have to be carefull of accumulating errors; especially if you are adding large and very small number together.
@dlbiggins
@dlbiggins 15 күн бұрын
It's a bit misleading to present this as a specifically Python solution. Almost all languages have variations on Decimal/Money types available either natively or as libraries.
@Carberra
@Carberra 15 күн бұрын
It doesn't present them as if they're the _only_ solution -- it presents them as a solution in Python. There's nothing misleading here.
@dlbiggins
@dlbiggins 14 күн бұрын
​@@Carberra Strongly disagree, sorry.
What IS typing.Annotated?
9:54
Carberra
Рет қаралды 8 М.
Is Python ACTUALLY faster without the GIL?
13:48
Carberra
Рет қаралды 2,5 М.
She made herself an ear of corn from his marmalade candies🌽🌽🌽
00:38
Valja & Maxim Family
Рет қаралды 18 МЛН
VIP ACCESS
00:47
Natan por Aí
Рет қаралды 30 МЛН
小丑教训坏蛋 #小丑 #天使 #shorts
00:49
好人小丑
Рет қаралды 54 МЛН
What Everyone Gets Wrong About E=mc²
9:49
The Science Asylum
Рет қаралды 121 М.
Making Smallest Possible Linux Distro (x64)
27:43
Nir Lichtman
Рет қаралды 92 М.
The BIGGEST feature in EVERY Python release (ft. @Indently)
13:44
Computer Timescales Mapped onto Human Timescales - Computerphile
28:41
Nox is the ONLY package you'll EVER need
15:55
Carberra
Рет қаралды 3,8 М.
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
Are metaclasses the HARDEST thing in Python?
20:41
Carberra
Рет қаралды 5 М.
The rarest move in chess
17:01
Paralogical
Рет қаралды 2,5 МЛН
Creating Your Own Programming Language - Computerphile
21:15
Computerphile
Рет қаралды 221 М.