Why Computers Screw up Floating Point Math

  Рет қаралды 338,174

Mental Outlaw

Mental Outlaw

Күн бұрын

Пікірлер: 851
@teamrabbitalec
@teamrabbitalec 3 жыл бұрын
I remember when learning this in my comp sci class that using floats for banking is an easy way to get yourself fired.
@saschadenzler848
@saschadenzler848 3 жыл бұрын
always use AT LEAST decimals.
@trolololo720
@trolololo720 3 жыл бұрын
@Anon Some languages give you a built in fixed-point decimal type, if you don't have one then you'd probably just go for a long or an actual object
@JohnDoe-qn2ej
@JohnDoe-qn2ej 3 жыл бұрын
Either doubles or decimals
@askhowiknow5527
@askhowiknow5527 3 жыл бұрын
@Anon double is short for “double-precision floating point” By decimal they mean fixed precision or integers
@saschadenzler848
@saschadenzler848 3 жыл бұрын
@Anon Sorry. i stroked out a little. I meant that its preferable to use double in finance software. decimals would be too computing intensive and needlessly sloww down the programm. integers can only hold full numbers so thats not too accurate.:D
@censoredterminalautism4073
@censoredterminalautism4073 3 жыл бұрын
Imagine trying to divide a piece of gold in two but it has an odd number of atoms so you split one of the atoms only to find out that it has an odd number of protons. That would make me very upset.
@groszak1
@groszak1 3 жыл бұрын
Split proton in a perfect half
@no-better-name
@no-better-name 3 жыл бұрын
"split one of the atoms" that'd cost you more than the piece of gold you already have xd
@no-better-name
@no-better-name 3 жыл бұрын
@@groszak1 problem: proton is made of 3 quarks, and those are indivisible
@censoredterminalautism4073
@censoredterminalautism4073 3 жыл бұрын
@@no-better-name Again, REEE. The universe is broken. We must fix it.
@no-better-name
@no-better-name 3 жыл бұрын
@@censoredterminalautism4073 god is yandere dev confirmed, who else would mess up this badly? xd
@gavinridley5727
@gavinridley5727 3 жыл бұрын
Gets way worse when you're adding lots of floats, too.
@WandererOfWorlds0
@WandererOfWorlds0 3 жыл бұрын
It depends if you need that level of precision or not. Machine learning requires multiply add operations with billions of floats but since it doesn't need the precision, you can get away with it. If you're doing some high precision scientific math and need something like float64, then yes, you need to be extremely careful about your math because of this rounding error.
@p0t4toePotato
@p0t4toePotato 3 жыл бұрын
or if you ad 1 to ten million, you get ten million not ten million 1, if you try to add 1 in a loop 1 million times, you'd still have 10 million
@machineman8920
@machineman8920 3 жыл бұрын
PAIN
@glax2174
@glax2174 3 жыл бұрын
Found that out trying to build a Rendering-Tool for OpenStreetMap data. I didn't want to draw nodes that arent even in the field of view, so I created a function to return the distance from one coordinate to another. During testing on the same Map-Data I got stumped on why during execution I would get different amounts of Rendered Nodes until I figured out that I use 4byte floats for coordinates...
@DoggyBingBong
@DoggyBingBong 3 жыл бұрын
floating point errors: cool for minecraft world gen HORRIBLE for banking
@charautreal
@charautreal 3 жыл бұрын
Welp, not for positions in bedrock tho
@DoggyBingBong
@DoggyBingBong 3 жыл бұрын
@@charautreal bedrock is shit
@charautreal
@charautreal 3 жыл бұрын
@@DoggyBingBong Yeah, a lot of things were crap in 2018
@glitchgaming766
@glitchgaming766 2 жыл бұрын
@@charautreal +now
@kiyu3229
@kiyu3229 2 жыл бұрын
@@charautreal they are shit now too
@Samarai-hf9si
@Samarai-hf9si 3 жыл бұрын
I'm extremely new to programming and just experienced my first instance of something not working as it 'should' (based on my extremely limited knowledge) because I was trying to check if two values were equal, but one was a floating point. I'm glad others share in the frustration and confusion I felt when changing it to a double made it work fine.
@prakhar6334
@prakhar6334 2 жыл бұрын
Thanks, you're a life saver
@khhnator
@khhnator 2 жыл бұрын
NEVER use equals with floats or doubles that had any operation done to it, most languages have some variation of a "Close Enough?" function for that purpose. but even then you need to be careful to some extent. if you did a lot of operations with that value the error propagation can be so out of whack that even that might fail
@ko-Daegu
@ko-Daegu 2 жыл бұрын
@@khhnator example of those? I see people say be careful but none explaining what's careful
@khhnator
@khhnator 2 жыл бұрын
@@ko-Daegu is not something you need to worry about often, only be aware of. but if you doing lots of math or have a program that runs for a very long time you might have a issue. the quick version of it is, don't accumulate results of operations indefinitely. myfloat = something * myfloat; because you just introduced a approximation error there. and every time you repeat it, the difference between the number you got and the real result will only increase if that will ever be a problem depends on how many operations you doing, the range of the numbers you using and what you doing with those numbers easy way to not have a problem, is to never reuse results of operations. rewrite your math to use know error less values and recalculate it from the start when you need the result. instead of modifying the value of a previous operation. myfloat = something * initial value * number of operations; of course, that's not always doable, and then you need to figure out if a error margin will break your code or not.
@justonefra
@justonefra 2 жыл бұрын
@@ko-Daegu Usually you do an operation like Math.Abs(a - b) < PRECISION when you need to check equality with a given precision. Using the cash register example you would use a 0.01 precision because if the difference between the two numbers is smaller than 0.01 you don't care and you can treat them as equal.
@wolverine9632
@wolverine9632 2 жыл бұрын
I remember figuring this out on my own when I was writing a pac-man clone in python. Using a speed of 0.1 tiles per frame, the players glitched through walls, because I assumed it should reach integer places. When I used values like 0.25, 0.125, etc. everything worked correctly. Powers of 2 at work.
@no-better-name
@no-better-name 2 жыл бұрын
yup, perfectly alignes with how the mantissa and the exponent works in floats
@whig3982
@whig3982 3 жыл бұрын
>Calling "bits" "decimal places" repeatedly Unsubbed and flagged for racism.
@onetwobits7558
@onetwobits7558 3 жыл бұрын
1 decimal place != 1 bit
@GrEEnEyE089
@GrEEnEyE089 3 жыл бұрын
in this context it should be binary places
@hossamdash
@hossamdash 3 жыл бұрын
Should've just used digits
@sodiboo
@sodiboo 3 жыл бұрын
@@hossamdash digits are for decimal, bits are the same thing but in binary - decimal places would be binary places when talking about binary numbers
@hossamdash
@hossamdash 3 жыл бұрын
@@sodiboo lol no a digit is the general name for any base number system. BIT is literally short for Binary digIT.
@laharl2k
@laharl2k 3 жыл бұрын
The playstation 1 is a good example of doing floting point math using ints. (edit: yeah techinically its called fixed point math, but you get the idea)
@Ozzianman
@Ozzianman 3 жыл бұрын
The PS1 does not do floating point at all, which is why the polygons flicker as the polygons snaps into place and gives PS1 games that recognizable look.
@SIGSEGV1337
@SIGSEGV1337 3 жыл бұрын
That's called fixed point computation, not floating point
@joey199412
@joey199412 3 жыл бұрын
It's not floating point math if you don't use floating point. But yeah that's why the polygons and textures look so wonky on the PS1. It's because the pixel position snaps to the nearest integer coordinate as they can't do fractions.
@مقاطعمترجمة-ش8ث
@مقاطعمترجمة-ش8ث 3 жыл бұрын
​@61 Cygni You don't had to just look at PS1 polygons(triangles) how mess they are, I read once that game-cube had way better geometry calculation than PS1 but weaker texture filling power, I think the games changes in gaming industry started with Voodoo
@مقاطعمترجمة-ش8ث
@مقاطعمترجمة-ش8ث 3 жыл бұрын
@@letusplay2296 Yeah I mean N64, I always forgot that console name, since it was never released in my county(we were under kinda sanctions back then)
@skaruts
@skaruts 2 жыл бұрын
During all my years learning game development all I've been hearing is _"computers have become so good at dealing with floats, there's no point using integers anymore"._ Except then there's all sorts of issues with floats that everyone has to keep working around (e.g. transforms must be orthonormalized frequently). There's a reason why in old engines like Quake and Doom your character is about 180 units tall and his speed is 250 units/s.
@tankfire20
@tankfire20 2 жыл бұрын
why are the character speeds set to 250 u/s?
@skaruts
@skaruts 2 жыл бұрын
@@tankfire20 to avoid floats, is my best guess. I mean, I could be wrong, but I'd bet my left nut on it. Basically, 1 meter was about 100 units. These days, modern game engines expect 1 meter = 1 unit.
@Megalomaniakaal
@Megalomaniakaal 2 жыл бұрын
Who ever says that? People who only work with double precision?
@tankfire20
@tankfire20 2 жыл бұрын
@@skaruts Makes sense
@Pokornz
@Pokornz 2 жыл бұрын
180 units tall... Sounds like centimeters ;)
@tomaspecl1082
@tomaspecl1082 3 жыл бұрын
Once I made a program to do matrix inversion. I wanted exact values so I used pairs of integers as fractions. After each calculation I had to calculate gcd of the integer pair and divide them so that the fractions would not get huge. Basically it did math same as I would do it. It worked pretty well.
@no-better-name
@no-better-name 2 жыл бұрын
oh yeah, i remember implementing a fraction class of two long ints and using GCD to reduce them every time one is constructed. expensive but worth it
@theairaccumulator7144
@theairaccumulator7144 2 жыл бұрын
Was it in python? That way you get something that runs at the speed of snail^2
@yoavmor9002
@yoavmor9002 2 жыл бұрын
I'm pretty sure both C# and Python have linear algebra libraries fyi. Once made Numpy try to invert a Matrix with a zero determinant. Unfortunately, it crashed, so use the libraries with caution
@jamesmnguyen
@jamesmnguyen 2 жыл бұрын
I did that too, it was pretty satisfying to see how my matrix inversion spit out the exact answer while regular floating point tools had huge errors.
@carlosdumbratzen6332
@carlosdumbratzen6332 2 жыл бұрын
Tbh that was some of the most fun I had in highschool (or the equivalent to it). Implementing the stuff from math class into code was both a great way to better understand the mathematical concept and the programming concept.
@jonathanrash4146
@jonathanrash4146 3 жыл бұрын
I would pay to watch a coding tutorial made by you. So many resources seem over-complicated or over-simplified, but your videos make a good balance.
@SantaClaauz
@SantaClaauz Жыл бұрын
Where are the over-complicated ones?
@MrC0MPUT3R
@MrC0MPUT3R 2 жыл бұрын
In my company we store all money values as a Long and have and use an accuracy of 1/10th of a penny. When rounding we use 'bankers rounding' which has slightly different rules to normal "half up" rounding you learn in school. The reason for this is that over the long run, half up rounding will result in a slightly higher total where as banker's rounding will even out over time.
@ercole1488
@ercole1488 3 жыл бұрын
Can't spell Chad without C
@purrplaysLE
@purrplaysLE 3 жыл бұрын
Without capital C specifically? Ok, chad
@binarycat1237
@binarycat1237 3 жыл бұрын
This sounds hilariously obvious out of context.
@AlizerLeHaxor
@AlizerLeHaxor 3 жыл бұрын
ok random guy who cant code
@eter9nitysslut131
@eter9nitysslut131 3 жыл бұрын
@@AlizerLeHaxor how do you know he can't code? lmao
@reinhold1616
@reinhold1616 3 жыл бұрын
@@eter9nitysslut131 just people assuming and acting suprerior.
@PortocaliusMaximus
@PortocaliusMaximus 3 жыл бұрын
>that constant 99% CPU usage
@sharoyveduchi
@sharoyveduchi 3 жыл бұрын
He's using FFMPEG or whatever that cringe program was called that's terminal based instead of using OBS which isn't such a CPU hog.
@notuxnobux
@notuxnobux 3 жыл бұрын
@@sharoyveduchi OBS uses ffmpeg
@sharoyveduchi
@sharoyveduchi 3 жыл бұрын
@@notuxnobux and? Pulse audio uses Alsa but nobody actually uses Alsa by itself.
@notuxnobux
@notuxnobux 3 жыл бұрын
@@sharoyveduchi Stupid comparison. How do you expect OBS to be better than ffmpeg when it uses ffmpeg directly? you can use hardware acceleration with ffmpeg if you provide the correct options. Its very easy. Its just one command.
@sharoyveduchi
@sharoyveduchi 3 жыл бұрын
@@notuxnobux no it's not a stupid comparison. OBS knows how to configure FFMPEG better than some dumb config file some amateur wrote and posted on his blog. If it was "very easy" then Mental Outlaw's CPU usage wouldn't be at 99% unless he's recording at some absurdly high bitrate or he's recording on a one core toaster. There is literally no excuse for this, even fraps and bandicam run better than this. What do you mean "how do you expect OBS to better"? Of course I know it's better! I use OBS and I didn't have that happen even on hardware from 10 years ago! I speak from experience. I'd rather use FREE SOFTWARE that "just works" than free software that requires more effort and hinders my performance even more than non-free solutions unless I know exactly what I'm doing.
@ShadowHunter120
@ShadowHunter120 3 жыл бұрын
The 4 hidden beneath layers of 0s, truly terrifying.
@Binary_Bloom
@Binary_Bloom 3 жыл бұрын
9:40 Yea and you can also use integers with functions such as “range( )” that don’t accept floats. If you want range from .01 to .1, just use 1 to 10 and divide output by 100.
@WandererOfWorlds0
@WandererOfWorlds0 3 жыл бұрын
NEVER EVER use floating point numbers in logical statements.
@WandererOfWorlds0
@WandererOfWorlds0 3 жыл бұрын
@pfp is a distro logo in my photos I did work with fixed point numbers in the past but didn't know that Python has a module for this.Thank you very much.
@WandererOfWorlds0
@WandererOfWorlds0 3 жыл бұрын
@linlinö önilnil The classic is 0.1+0.2 == 0.3 and it returns false, but yeah, something like that.
@miguelangelmartinezcasado8935
@miguelangelmartinezcasado8935 3 жыл бұрын
@@WandererOfWorlds0 In those cases you just have to be more cautious, like: If(Abs(0.1+0.2 - 0.3)
@0xCAFEF00D
@0xCAFEF00D 3 жыл бұрын
Terrible suggestion. Learn floating point.
@Howtheheckarehandleswit
@Howtheheckarehandleswit 2 жыл бұрын
Well, no, you can. You just usually shouldn’t check for strict equality, instead test for being in a certain range of values. Some languages provide built in functions for this, but you could always just chain together some comparisons. Floating point shouldn’t be used in situations where absolute precision is necessary, but for something like a physics engine in a game or normalized positions in graphics that don’t need to be perfect, floats are great, because they are much faster to compute and use less memory than representing non-integers as fractions.
@YaBoiKuma
@YaBoiKuma 3 жыл бұрын
From the title I expected an explanation on the binary representation of floating point numbers. Most of the video was examples of screwups.
@monstertrucks9357
@monstertrucks9357 3 жыл бұрын
Actually, floating point works perfectly if you understand what it is and what it's supposed to do. It does NOT randomly error; it's actually perfectly accurate within its domain. But it was never intended to store base-10 decimals precisely. Floating point would have been messed up if it DID!
@BasedPureblood
@BasedPureblood 3 жыл бұрын
What exactly are they used to? I'm curious. I always turn everything into int.
@MarceloDezem
@MarceloDezem 2 жыл бұрын
C# has a Decimal type, with 128 bit precision. Works pretty well, but it's orders of magnitude slower than float/double.
@the_expidition427
@the_expidition427 Жыл бұрын
Base-12 and the imperial system
@MrLittlePredator
@MrLittlePredator 3 жыл бұрын
I remember making a program in C or C++ that used floats in logic. Everything was coded correctly but the program didn't do what i was expecting at all...sad thing is i never figured out why until now xD
@DsiakMondala
@DsiakMondala 2 жыл бұрын
Doesn't even has to be any of that. If it was C it could be a quirk from your compiler of choice, similarly if it was C++... jesus almight deliver me from this evil.
@Assassin_Droid
@Assassin_Droid 2 жыл бұрын
Bruh
@unknownguywholovespizza
@unknownguywholovespizza Жыл бұрын
Easy, use Rust
@plasmatch8
@plasmatch8 3 жыл бұрын
Normally you should use the 'decimal' type for monetary calculations. from decimal import Decimal Decimal("0.2") + Decimal("0.1") == Decimal("0.3")
@mystcat3
@mystcat3 3 жыл бұрын
That also comes with a very rare error, if you change the country which uses comma, it will give you errors
@WorBlux
@WorBlux 3 жыл бұрын
But very few cpu's support the decimal type natively.
@diablo.the.cheater
@diablo.the.cheater 3 жыл бұрын
@@alexvitkov No, you add those 5 layers of abstraction at the API with the DB. Computing is all about abstraction anyway
@nonenull1490
@nonenull1490 3 жыл бұрын
@@alexvitkov there is a standard SQL type specifically for fixed decimal precision (ie. for representing decimal fractions). It's called NUMERIC (there's also BIGNUMERIC with less precision but a higher scale). The overwhelming majority of SQL implementations will have many many more than that with configurable precision for specific applications (DECIMAL, SMALLMONEY, MONEY). You will learn that FLOAT is *always just an approximation* by reading just one sentence in your engine-of-choice's documentation.
@nonenull1490
@nonenull1490 3 жыл бұрын
@@alexvitkov you are perfectly capable of implementing any of the data types your engine has in any language. People who wrote the engine did that before you after all. Even if you don't have the necessary information to recreate the exact datatype or "reinvent" it for your use case, the library you're using to operate on the database will have to provide those data types to you. Since the dawn of SQL, you can even just construct queries to make the database engine do all the math for you; due to this interoperability and "nativeness" is irrelevant. Beyond that, many languages natively support fixed-point arithmetic (that includes COBOL) and even C has had extensions for that for almost 20 years (ISO/IEC DTR 18037). If you're working on x86, even assembly has support for binary coded decimals you can use to easily implement fixed-point arithmetic (as native as it gets). I don't know what you mean in that last part, but fractions of a cent are needed so that money doesn't just "disappear" in calculations due to rounding.
@joansalazar5884
@joansalazar5884 3 жыл бұрын
I think it would be more correct to say that computers are better at COMPUTATION than better at math. Being good at math goes way beyond numbers and equations, and it involves a capacity for creativity and abstract reasoning.
@toilet_cleaner_man
@toilet_cleaner_man Жыл бұрын
I'd compare Computational Math to atomic physics, and "Math" as the general field of study to the whole of Physics, including quantum physics; it has the elements of atomic physics present, but when we get into anything more complex, like theories on sub-subatomic particles and whether forces are a Particle or not it alot like the more existential portions of math that are abstract and far beyond any basic understanding. You wouldn't expect an 11th grader learning what friction is to understand nuanced theories on whether gravity is a particle, and you shouldn't expect the computer to understand what the hell the number 0.25 means.
@ghostedyoutuber263
@ghostedyoutuber263 3 жыл бұрын
round off error is a bitch... especially when dealing with chemistry and computers.
@Calajese
@Calajese 3 жыл бұрын
8:13 The moustache man inside the bunker be like
@test-cp8uz
@test-cp8uz 3 жыл бұрын
hi
@test-cp8uz
@test-cp8uz 3 жыл бұрын
@Nicefisher bruh
@ClifffSVK
@ClifffSVK 3 жыл бұрын
@@test-cp8uz bruh moment
@test-cp8uz
@test-cp8uz 3 жыл бұрын
@@ClifffSVK bruh
@jakubramaseuski1979
@jakubramaseuski1979 3 жыл бұрын
German moustache man? You meant Chaplin, right? EDIT: I know , I know you meant the other guy, but the others probably didn't.
@ChimeraX0401
@ChimeraX0401 3 жыл бұрын
In the past when I need to do precision math in floating point numbers I always use the FMA(Fused Multiplication Addition) function plus some other tricks. The downside of this is that my program will not work on some intel machines because they lack FMA support....
@tolkienfan1972
@tolkienfan1972 2 жыл бұрын
Intel has FMA
@SebiBubble
@SebiBubble 2 жыл бұрын
@@tolkienfan1972 keyword of the day is "SOME"
@tolkienfan1972
@tolkienfan1972 2 жыл бұрын
@@SebiBubble that is true. You can have both implementations and use cpuid to choose which to use. Tis a common technique.
@Jankoekepannekoek
@Jankoekepannekoek 3 жыл бұрын
Douglas Crockford defined a number type called DEC64 to deal with this issue. It's not widely adopted in programming languages unfortunately.
@ThankYouESM
@ThankYouESM 3 жыл бұрын
took me over a year to find out that the decimals were throwing my artificial learning computations... then soon realized to make it tumblers of integers instead which works perfectly.
@sephondranzer
@sephondranzer 3 жыл бұрын
3:15 in the morning and I finally understand why my excel trackers kept adding .00000001 increments to the results!! ... Going to sleep now
@entcraft44
@entcraft44 2 жыл бұрын
Another nice bug with floats is if you have to add a long row of ever increasing floats. If you start from the lowest value, you get the correct (approximate) result. If you start from the highest, the low ones all get rounded to zero, and if there are many of those small ones, the error may be very large.
@egg5474
@egg5474 3 жыл бұрын
"And if you turn to page 2, you'll see homotopy type theory, which is left as a migraine for the student"
@carsonfball4
@carsonfball4 3 жыл бұрын
3:53 Just a slight correction because I'm a pedant. You still only need 3 decimal places to represent 255. However, you need 8 binary places to represent it. Decimal places refer specifically to base 10 (dec).
@randysterling4700
@randysterling4700 2 жыл бұрын
Total beginner here, Thanks for such a clear concise explanation on the subject made it really easy to understand!
@radarpixel1304
@radarpixel1304 2 жыл бұрын
The amount of information that you cover on your channel, is staggering. I'm a happy subscriber.
@todayonthebench
@todayonthebench 2 жыл бұрын
A good highlight of the biggest issue of floating point. Also why a lot of central banks the world over more or less require integers (or fixed point) variables when handling transaction data. For statistics and such, then floats can be a nice thing to have at times. However, a lot of tasks that people use floating point for don't have too much benefit from using float. 3d graphics is one such application.
@p0t4toePotato
@p0t4toePotato 3 жыл бұрын
who the hell stores monetary value with floating point?
@Jamiered18
@Jamiered18 3 жыл бұрын
C# decimal type. It's a floating point number type, rather than floating binary type. Safe for finance. So, technically, you can store money using floating point, just not float
@CoolJosh3k
@CoolJosh3k 3 жыл бұрын
Should have mentioned the use of an epsilon value and approximation so that 0.3*3==0.9 can still return true.
@tutacat
@tutacat Жыл бұрын
Float is for fast. If you are writing a calculator, either use double long, or just decimal. Another way is to convert to integers so the value is discrete instead of fractional
@Adam_Lyskawa
@Adam_Lyskawa 3 жыл бұрын
C# Interactive: > 0.1 + 0.2 0.30000000000000004 > 0.1m + 0.2m 0.3 (decimal)Math.PI 3.14159265358979 > Math.PI 3.1415926535897931 The CPU calculates correctly. The result is exact (up to the LSB). There is an error introduced while converting between binary floating point numbers and decimal numbers. When you don't have the decimal type in the programming language you use, but you need it (working with money, for example) - the good solution is to make one or use a library that provides it.
@marioschroers7318
@marioschroers7318 3 жыл бұрын
I love how this man is able to explain stuff. Natural born teacher. Excellent!
@lool8421
@lool8421 3 жыл бұрын
some random guy who doesn't know anything about computers: so why don't computers use base-10?
@trogdorstrngbd
@trogdorstrngbd 3 жыл бұрын
Because at the transistor and logic gate level (i.e., the flow of electrons along nanometer-sized "wires"), everything is effectively "on" or "off" with no meaningful in between.
@Neophage
@Neophage 3 жыл бұрын
Probably because they can't be bothered. idk.
@rockman7perez
@rockman7perez 2 жыл бұрын
I think the first computers did use base-10, then they switched to base-2 because it is faster.
@honeybeeanie
@honeybeeanie 3 жыл бұрын
This mans CPU is pinned at 100% when using python interpreter..5:33
@codechamp27
@codechamp27 2 жыл бұрын
"People's believe in a computer's mathematical ability will start declining just like their ability to do it" - the funniest line I have ever heard in programming
@km5405
@km5405 2 жыл бұрын
fixed point integers is another workaround for this that also sometimes can be more performant depending on the application/processor.
@stumbling
@stumbling 3 жыл бұрын
Binary representation of 1/3: (k = 1 to ∞) Σ 2^-2k
@JuddMan03
@JuddMan03 3 жыл бұрын
Binary representation of 1/3: 1/11 If you're allowed to use Σ, i'm allowed to use /
@gfuentes8449
@gfuentes8449 2 жыл бұрын
One of the most powerful ways to teach is by metaphor/comparison and this brings it home really well
@Tubeytime
@Tubeytime 2 жыл бұрын
It's important to remember that humans chose to make computers use base 2 out of convenience, and furthermore, ANY base counting system would include floating point errors as well.
@average-neco-arc-enjoyer
@average-neco-arc-enjoyer 2 жыл бұрын
what about my boi the Unary numeral system?
@tolkienfan1972
@tolkienfan1972 2 жыл бұрын
Floats are really useful, but there is a lot to know. Some examples: float addition is not associative. Some calcs are subject to catastrophic cancellation. Round off errors can accumulate. Floats can hold reasonably sized integers exactly. There is a negative zero and 0 == -0. a < b does not imply !(a >= b) because of NaNs...
@angeldude101
@angeldude101 2 жыл бұрын
This is the real problem with floats, but everyone just focus on the simple rounding errors. Duh! Of course rounding errors happen. Anyone who's tried using a third in decimal should know that. Floating point being non-associative, and equality being non-reflexive? _Those_ can make one want to tear their hair out. Heck, multiplying by and adding 0 doesn't compile to a no-op because of -0.
@tolkienfan1972
@tolkienfan1972 2 жыл бұрын
@@angeldude101 right? And some compiler options *assume associativity* and other invalid assumptions... meaning the code can be correct but be turned into incorrect assembly!
@angeldude101
@angeldude101 2 жыл бұрын
@@tolkienfan1972 I can understand when floats are non-associative _because of intermediate rounding, but I have to seriously question any developer who actually wants this kind of behavior. Personally, I have a project with a bunch of specialized types that all defer to a single 32 x 32 monster product with unused fields say to 0. Unfortunately, -0 (and NaN) means that every single term of that Monster product needs to be evaluated unless I use fast-math. Then the problem becomes the fact that a NaN or infinity sneaking into the code causes undefined behavior, which could just crash, or it could put my program into an infinite loop.
@tolkienfan1972
@tolkienfan1972 2 жыл бұрын
@@angeldude101 are you saying you have values known at compile time to be zero, but the compiler won't optimize them away because of negative zero?
@angeldude101
@angeldude101 2 жыл бұрын
@@tolkienfan1972 Yes.
@ff-jt8un
@ff-jt8un 3 жыл бұрын
>python is the language of choice for mathematicians and physicists >can't even do math properly Check mate python normies
@hanteroes
@hanteroes 3 жыл бұрын
Language that magically changes computer architecture when?
@laharl2k
@laharl2k 3 жыл бұрын
Python isnt doing the math, the cpu is. Youd get the same result be it in C or in visual basic.
@____-gy5mq
@____-gy5mq 3 жыл бұрын
@@laharl2k I'll create a GUI user interface in visual basic, see if I can track the floating point calculations.
@corejake
@corejake 3 жыл бұрын
@@laharl2k lmao i use C++, no problem with error rates with such simple calculations. I depends on what and how you use it. Python is like a scratch from programming. You dont write big amd complex stuff on python for a reason.
@joseagustinrios9666
@joseagustinrios9666 3 жыл бұрын
@@corejake You know Tensorflow exists?
@hhhyyy4375
@hhhyyy4375 3 жыл бұрын
There are workarounds. This is a nice video to introduce an idiosyncrasy most scripters have seen.
@blacklight683
@blacklight683 2 жыл бұрын
This video made me realize what if we add more numbers? There has to be a number that we can always divide equally with out having the 0.333/0.666/0.999
@SashaFYI
@SashaFYI 3 жыл бұрын
Subbed for privacy tips, yet perpetually confused by vids with coding and math.
@hhhyyy4375
@hhhyyy4375 3 жыл бұрын
Inevitable on a tech channel.
@shadowqueen2134
@shadowqueen2134 2 жыл бұрын
Literally knew nothing about floats or comp sci but u explained it well without me feeling dumb
@ConernicusRex
@ConernicusRex 2 жыл бұрын
It’s so funny when I come across someone in my field so literate in CS they can explain things like floats to laymen; BUT STILL GET FOOLED INTO INVESTING IN CRYPTO. 🤣🤣🤣🤣
@jensschroder8214
@jensschroder8214 2 жыл бұрын
I needed 6 digits before the decimal point and 2 digits after the decimal point. I simply defined the digits before the decimal point with an unsined long. I define an 8-bit byte for the two digits after the decimal point. Whenever the byte jumps from 99 to 100, I subtract 100 from the byte and add 1 to the unsined long. If I notice too late that the decimal places are already at 101, that doesn't matter either. That way I never lose one bit when counting up.
@mbahmarijan789
@mbahmarijan789 3 жыл бұрын
free class in the morning... nice 👍👍
@rex_melynas
@rex_melynas 3 жыл бұрын
*uses js, so I don't technically have integers* I know yeah, the VM actually does and calculates your values as integers, but they are technically integers disguised as floats xD
@monstertrucks9357
@monstertrucks9357 3 жыл бұрын
There is a range of floating points which work perfectly as integers. JS just uses that range. It does do floating point math on your integers. It's just that integers happen to be represented with 100% accuracy within a certain range. So it's the other way around, it's floats disguised as integers.
@LordOfNihil
@LordOfNihil 3 жыл бұрын
the second one (3+7*6) is an order of operations problem, its doing multiply last. then the next 5 are common fp magic numbers where known rounding errors occur with those numbers. then when using floats in a conditional, you need an epsilon value to compensate for rounding errors. for example ((0.3*3)>(0.9-e)) && ((0.3*3
@JuddMan03
@JuddMan03 3 жыл бұрын
Second one is fine. He gets 45. If it were wrong he would get 60. The "fp magic" is what he is showcasing here. For new programmers.
@LordOfNihil
@LordOfNihil 3 жыл бұрын
@@JuddMan03 and i kind of watched it with my speakers off. oops.
@JohnnieHougaardNielsen
@JohnnieHougaardNielsen Жыл бұрын
In banking, those fuzzy decimals are just *bad*. Where I've worked, the solution was fixed-point decimals. A number declared with 2 decimals would be stored as an integer, and the compiler would do the right scaling during calculations and formatting.
@alice20001
@alice20001 2 жыл бұрын
In Japan there is no 100th break in currency. It is as if everything was counted in cents. The only place I've seen the Japanese Yen have any decimal points is in currency exchange where they have 2 decimal places (as opposed to the common 4, since the Yen does not have decimal breaks).
@PatchCornAdams723
@PatchCornAdams723 2 жыл бұрын
The way you talk kinda reminds me of Styxhexenhammer, lol. Love your vids man, I'm binging them tonight.
@Excludos
@Excludos 2 жыл бұрын
A more elegant way of fixing this issue for comparison cases is to simply just add in a tolerance. aka. instead of "if (a + b == 0.3)" you do "if (Math.Abs(a + b - 0.3) < 0.00000001)" This amount of zeroes is usually well below anything you care about, and still half the amount of decimal places for any float rounding errors. So you are good in either direction. And it's a lot easier than finding out how big your numbers are and adding the correct amount of zeroes to it, or even worse, adding extra string variables into the mix
@GeraldBenson
@GeraldBenson 2 жыл бұрын
I write a POS and accounting software package, I run into this rounding error all the time. Where sometimes 5 would round down instead of up. The technique I use to to resolve it is to weight the number at the 7th decimal place (as money never goes beyond 4 decimal places). By adding 0.0000001 to the number before rounding. Calculating taxes on an item by item basis vs calculating taxes on a whole invoice makes this problem appear fairly regularly.
@salsamancer
@salsamancer 2 жыл бұрын
Never use floats, unless you have to. Makes me glad I don't work in finance or graphics, I can't even remember the last time I used a float
@sophiacristina
@sophiacristina 2 жыл бұрын
I tell this to people that do music, since normally we use trigonometric functions which goes in the range '-1' to '1', they are going one moment or another hit a '(1/n) * n' that not equals '1'... Anyway, people in music should consider that samples are based on the amount of bit per sample, so instead of using float. It is better to adapt yourself to use things like 255, 65536 and etc...
@Tyler-Kearney
@Tyler-Kearney 2 жыл бұрын
I remember when I was taking Programming for Business Applications (we used C#) I asked why we were using decimal instead of double, and was simply told in C# you use decimal for anything with money. Guess I know why now.
@anon_y_mousse
@anon_y_mousse 2 жыл бұрын
Watch one video talking about floating point and suddenly it's all over your recommendations. I only watched because it's you, but now I'm wanting you to do something more in-depth. It'd be neat if someone would break down how floating point math is done. Had a friend years back who was a math genius and he wrote out a few functions for me demonstrating how you could handle multiplication and division in software without an FPU and it was awesome. If I could just figure out which hard drive I put it on, need an index or to put everything on a giant NAS.
@camiloospinaa
@camiloospinaa 3 жыл бұрын
I would only like to correct the fact that 0.33333 in binary is not 0.011011. For the decimal places the binary representation is an approximation of adding negative powers of 2. Therefore 0.5 would be 0.1, 0.25 would be 0.01, 0.125 would be 0.001 so on and so forth. How many decimals you want to store and with which precision depends on the standard used, for instance IEEE754.
@johndiss8275
@johndiss8275 2 жыл бұрын
Good catch! 0.011011... is actually 3/7, not 1/3
@tombert512
@tombert512 2 жыл бұрын
The slight beacon of hope on this is that a lot of languages come with a "decimal" or "money" type now, which is slower but represents fractional types internally as integers or strings. Your arithmetic can be exact and you can safely use equality statements without things being confusing. Most of the time when money is involved, you should probably use these decimal types.
@skilz8098
@skilz8098 3 жыл бұрын
1111 1111 in binary will only be 255 with unsigned integers or unsigned chars. If it is a signed integer or char, especially when using twos complement, then that value would be -128.
@shiinondogewalker2809
@shiinondogewalker2809 2 жыл бұрын
ok but 99 would only be 99 in 2 digit unsigned integers
@shiinondogewalker2809
@shiinondogewalker2809 2 жыл бұрын
also 1111 1111 signed char is -1 isn't it. -128 is 1000 0000 -128 signed int would be 11111111 11111111 11111111 10000000
@johng.1703
@johng.1703 2 жыл бұрын
we use base 10 because of the decimal point. it makes it really easy to multiply and divide by 10 by simply moving the decimal point either to the left or to the right.
@user-dh8oi2mk4f
@user-dh8oi2mk4f 2 жыл бұрын
No. In every base, if you multiply by the base you just shift the number. Base 10 is not special. Multiplying by 16 in base 16 moves the number to the left.
@soanvig
@soanvig 2 жыл бұрын
From practical point of view base 12 or something would be more useful, since you get one more denominator than in base 10 without going too much into inventing new "digits" for this system. And actually dividing things in half (1/2) or thirds (1/3) or fourths (1/4) is more common in real life, than dividing by 1/5.
@Kacper-qp8kg
@Kacper-qp8kg 2 жыл бұрын
Wrong. This is true for every base, the same thing happens if you multiply by 2 in binary for example.
@franspai7415
@franspai7415 3 жыл бұрын
This one 10 min video taught me more than 6h of cs classes at uni.
@aussiekai
@aussiekai 2 жыл бұрын
1:46, i love it how u say, probably because "most" people have ten fingers.
@Linuxdirk
@Linuxdirk 2 жыл бұрын
Please keep in mind that base 2 has no “decimal places”. Binary representation of numbers also depends on the endianness of the system (1000 vs. 0001).
@crispyrice
@crispyrice 3 жыл бұрын
I feel like it would've been nicer if you called binary decimal places, just binary places, since decimal literally means "of base ten"
@turwaith
@turwaith 2 жыл бұрын
I learned this when my boss in my internship jumped at me screaming why I was using == to compare two doubles.
@chrisidema
@chrisidema 2 жыл бұрын
1) Decimal digits do NOT represent multiples of 10, but powers of 10. Big difference. 2) A decimal place is NOT the same as a decimal digit, it refers only to a decimal digit in the fraction part of the number (to the right of the decimal separator).
@hi6477
@hi6477 Жыл бұрын
Base 10 is the only system with decimal places. In base 2 they are called bits and in an overall sense the term you are looking for it digits
@sanduhr3247
@sanduhr3247 3 жыл бұрын
IEEE 754 allows a clear expression of 1/3rd, 1/5th, etc and is the standard for floats
@JuddMan03
@JuddMan03 3 жыл бұрын
You sure? I tried to google this and didnt find that mentioned. In python what does this print out? '%.18f' % ((1.0/3))
@asdasddas100
@asdasddas100 3 жыл бұрын
More programming videos please :)
@censoredterminalautism4073
@censoredterminalautism4073 3 жыл бұрын
Missed the opportunity to run a Forth interpreter to dab on people that don't know Reverse Polish notation, so they have to pick up Leo Brodie's Starting Forth before watching the video and learn about the notation and about data stacks. Not Lisp because too many people here know prefix notation. That would be more ideal to do in a channel about finance.
@emmata98
@emmata98 2 жыл бұрын
5:00 but we can create a new data fomat that includes the info, that there are an infinite ammount of 3
@edgardosantamaria5399
@edgardosantamaria5399 2 жыл бұрын
Thank you man, for doing this videos
@venn7257
@venn7257 2 жыл бұрын
as a newbie, found this recently when trying to make a simple mock calculator. Solution was to round the decimal
@TheRealBeady
@TheRealBeady 2 жыл бұрын
"Bitcoins about 40000 it might have dipped a little bit today" Man that did not age well
@materialknight
@materialknight 2 жыл бұрын
1:55 I think it's better to say that each position to the left represents the next POWER of 10
@joona7556
@joona7556 2 жыл бұрын
If you need precise numbers, you will have to implement new struct that can represent fractional numbers (nominator & denominator as int) and arithmetic actions for it. This way you will always get precise results. But often there is no need for it, because the error is so small if floating point numbers are used.
@matthewfala
@matthewfala 2 жыл бұрын
‘We all “float” down here’
@emmata98
@emmata98 2 жыл бұрын
3:09 it will get there. You just need infinite decimal places. This is normal process in mathematics, witch is called convergence
@shiinondogewalker2809
@shiinondogewalker2809 2 жыл бұрын
except you have no process that will get to infinite decimal places, so it will never get there after all
@emmata98
@emmata98 2 жыл бұрын
@@shiinondogewalker2809 and then we get to the definition of convergence, that combats that
@shiinondogewalker2809
@shiinondogewalker2809 2 жыл бұрын
@@emmata98 nah not really, I don't deny adding 3s forever converges to 1/3, I meant that because infinite time is needed it will never happen
@wege8409
@wege8409 2 жыл бұрын
Another option, you could also choose to use the fractions or decimals modules for increased precision.
@wege8409
@wege8409 2 жыл бұрын
@Tah BOO lol I mean I saw the video, integers are king and everything, but it seems like kind of a strange workaround, having to convert back and forth between the values when numbers are the meat and potatoes of computers.
@wege8409
@wege8409 2 жыл бұрын
@Tah BOO What do you mean by it being intentional? Like someone is doing this on purpose to mess with the economy with floating point errors whenever unaware banking programmers use floats? Cause I like that idea, I think it's funny, but I think it's a performance thing.
@JorgeEscobarMX
@JorgeEscobarMX 2 жыл бұрын
I see, youtube recommended this on the anniversary of the video.
@xeridea
@xeridea 2 жыл бұрын
Computers can do decimal places accurately if you want. You need to use a decimal data type (depends on language support, or there are many open source libraries to do it in common languages). You can have arbitrarily long or short decimal capabilities, even hundreds or thousands of decimals. Floating point is normally used because it is faster, either 32 or 64 bits off accuracy. Note you don't get 32 or 64 bits of decimal, some is used for the whole numbers, and to know where decimal place is, they are in memory kind of like scientific notation, except decimal can be anywhere. For the formatting, just always keep in floating point, only format to display on screen or save to database, if there is a rounding error 15 decimals in, generally this is acceptable.
@xCwieCHRISx
@xCwieCHRISx 2 жыл бұрын
Comparing 2 floats: C/C++, C#, Java, Javascript: (a>b ? a-b : b-a) < delta Python: (a-b if a>b else b-a) < delta with delta being a very small float
@em_the_bee
@em_the_bee 10 ай бұрын
class Float def sorta_eq?(another_float, delta=0.001) (self - another_float).abs < delta end end (0.1+0.2).sorta_eq?(0.3) #=> true
@Winnetou17
@Winnetou17 3 жыл бұрын
You used "decimal places" in many places where "digits" was the correct word. Other than that, nice video :)
@theturboassslayer7354
@theturboassslayer7354 3 жыл бұрын
The correct word for digits when talking about computers is bits
@bobwillett5663
@bobwillett5663 3 жыл бұрын
@@theturboassslayer7354 no it doesn't, he's still talking about base 10 a bit is base 2
@theturboassslayer7354
@theturboassslayer7354 3 жыл бұрын
@@bobwillett5663 that's why I said when talking about computers
@RedVRCC
@RedVRCC 5 ай бұрын
Fun fact. Some earlier ARM processors completely lacked the ability to perform division at all. The reason why humans can perform this kind of math better is because we have logical reasoning and rationality. We know that 1/3 being 0.33333333333... is impractical, so we may choose instead to represent it as 0.33 or 0.34 depending on whether we want it to be slightly above or slightly below the target whole number. On a computer, 2/3 is represented as 0.666666.... but we may choose to simply do 0.67.
@ImKeviin9
@ImKeviin9 2 жыл бұрын
for comparison there is a good aproach using distance (like epsilon-delta definition of limits) instead of > (0.3 • 3) == 0.9 # error use > abs((0.3 • 3) - 0.9) < EPSILON where EPSILON is a small floating constant (like 0.01). It is not accurate (must to use a smallest DELTA to get accuracy) but works in lot of cases
@ImKeviin9
@ImKeviin9 2 жыл бұрын
*EPSILON
@blacklight683
@blacklight683 2 жыл бұрын
0:10 to be fair a human has a lot of things in his mind his family,bills,taxes,dreams,what will he eat/wear ect. The computer has 1gole and its 1+1=2
@mskiptr
@mskiptr 3 жыл бұрын
floating-point for inaccuracy; fixed-point when appropriate; rationals ftw; cyclotomic from Hackage for being fancy; and exact reals only in theory!
@paulstubbs7678
@paulstubbs7678 Жыл бұрын
This is a right pain, it would be good if they offered alternative math libraries. They are probably out there however for a newbee to say Arduino, this can be a rather awful experience
@pseudonymity0000
@pseudonymity0000 2 жыл бұрын
Mental Outlaw : You cannot add an integer to a string. JS: Hold my beer.
@chessdad182
@chessdad182 2 жыл бұрын
I remember 40 years ago, my boss yelling at the consultant programmer because the code wasn't working properly due to this.
Do This Before Putting Your Files in the Cloud
12:56
Mental Outlaw
Рет қаралды 169 М.
why can’t computers have thousands of cores?
8:08
Low Level
Рет қаралды 717 М.
ЗНАЛИ? ТОЛЬКО ОАЭ 🤫
00:13
Сам себе сушист
Рет қаралды 4 МЛН
When mom gets home, but you're in rollerblades.
00:40
Daniel LaBelle
Рет қаралды 132 МЛН
1, 2, 3, 4, 5, 6, 7, 8, 9 🙈⚽️
00:46
Celine Dept
Рет қаралды 59 МЛН
Why You Shouldn't Nest Your Code
8:30
CodeAesthetic
Рет қаралды 2,8 МЛН
How to Actually Escape the Botnet
32:17
Mental Outlaw
Рет қаралды 532 М.
how floating point works
17:48
jan Misali
Рет қаралды 384 М.
Floating Point Numbers - Computerphile
9:16
Computerphile
Рет қаралды 2,3 МЛН
Naming Things in Code
7:25
CodeAesthetic
Рет қаралды 2,2 МЛН
Bad OPSEC - How The Feds Traced a Monero User
13:55
Mental Outlaw
Рет қаралды 567 М.
How File Compression Works
11:25
Mental Outlaw
Рет қаралды 266 М.
Why YouTube Sponsors Are (Almost) Always Terrible
10:48
Micro
Рет қаралды 138 М.
ЗНАЛИ? ТОЛЬКО ОАЭ 🤫
00:13
Сам себе сушист
Рет қаралды 4 МЛН