Тёмный
No video :(

Why Computers Screw up Floating Point Math 

Mental Outlaw
Подписаться 658 тыс.
Просмотров 337 тыс.
50% 1

₿💰💵💲Help Support the Channel by Donating Crypto💲💵💰₿
Monero
45F2bNHVcRzXVBsvZ5giyvKGAgm6LFhMsjUUVPTEtdgJJ5SNyxzSNUmFSBR5qCCWLpjiUjYMkmZoX9b3cChNjvxR7kvh436
Bitcoin
3MMKHXPQrGHEsmdHaAGD59FWhKFGeUsAxV
Ethereum
0xeA4DA3F9BAb091Eb86921CA6E41712438f4E5079
Litecoin
MBfrxLJMuw26hbVi2MjCVDFkkExz8rYvUF
Dash
Xh9PXPEy5RoLJgFDGYCDjrbXdjshMaYerz
Zcash
t1aWtU5SBpxuUWBSwDKy4gTkT2T1ZwtFvrr
Chainlink
0x0f7f21D267d2C9dbae17fd8c20012eFEA3678F14
Bitcoin Cash
qz2st00dtu9e79zrq5wshsgaxsjw299n7c69th8ryp
Etherum Classic
0xeA641e59913960f578ad39A6B4d02051A5556BfC
USD Coin
0x0B045f743A693b225630862a3464B52fefE79FdB
Subscribe to my RU-vid channel goo.gl/9U10Wz
and be sure to click that notification bell so you know when new videos are released.

Опубликовано:

 

27 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 859   
@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
@DoggyBingBong
@DoggyBingBong 3 года назад
floating point errors: cool for minecraft world gen HORRIBLE for banking
@charautreal
@charautreal 2 года назад
Welp, not for positions in bedrock tho
@DoggyBingBong
@DoggyBingBong 2 года назад
@@charautreal bedrock is shit
@charautreal
@charautreal 2 года назад
@@DoggyBingBong Yeah, a lot of things were crap in 2018
@glitchgaming766
@glitchgaming766 2 года назад
@@charautreal +now
@kiyu3229
@kiyu3229 2 года назад
@@charautreal they are shit now too
@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.
@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...
@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
@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.
@user-ir2fu4cx6p
@user-ir2fu4cx6p 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
@user-ir2fu4cx6p
@user-ir2fu4cx6p 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)
@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.
@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 ;)
@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.
@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?
@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.
@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.
@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.
@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.
@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.
@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.
@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 2 года назад
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 11 месяцев назад
Base-12 and the imperial system
@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")
@andricode
@andricode 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.
@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
@thecoolnewsguy
@thecoolnewsguy Год назад
Easy, use Rust
@ghostedyoutuber263
@ghostedyoutuber263 3 года назад
round off error is a bitch... especially when dealing with chemistry and computers.
@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.
@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.
@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.
@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"
@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.
@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.
@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.
@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).
@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?
@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 /
@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
@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
@honeybeeanie
@honeybeeanie 3 года назад
This mans CPU is pinned at 100% when using python interpreter..5:33
@marioschroers7318
@marioschroers7318 3 года назад
I love how this man is able to explain stuff. Natural born teacher. Excellent!
@KnightlyFYI
@KnightlyFYI 3 года назад
Subbed for privacy tips, yet perpetually confused by vids with coding and math.
@hhhyyy4375
@hhhyyy4375 3 года назад
Inevitable on a tech channel.
@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
@radarpixel1304
@radarpixel1304 2 года назад
The amount of information that you cover on your channel, is staggering. I'm a happy subscriber.
@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.
@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.
@randysterling4700
@randysterling4700 2 года назад
Total beginner here, Thanks for such a clear concise explanation on the subject made it really easy to understand!
@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?
@mbahmarijan789
@mbahmarijan789 3 года назад
free class in the morning... nice 👍👍
@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.
@asdasddas100
@asdasddas100 3 года назад
More programming videos please :)
@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.
@RedVRCC
@RedVRCC 2 месяца назад
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.
@gfuentes8449
@gfuentes8449 2 года назад
One of the most powerful ways to teach is by metaphor/comparison and this brings it home really well
@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
@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
@aussiekai
@aussiekai 2 года назад
1:46, i love it how u say, probably because "most" people have ten fingers.
@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.
@km5405
@km5405 2 года назад
fixed point integers is another workaround for this that also sometimes can be more performant depending on the application/processor.
@hhhyyy4375
@hhhyyy4375 3 года назад
There are workarounds. This is a nice video to introduce an idiosyncrasy most scripters have seen.
@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
@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. 🤣🤣🤣🤣
@materialknight
@materialknight 2 года назад
1:55 I think it's better to say that each position to the left represents the next POWER of 10
@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.
@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.
@alice20001
@alice20001 Год назад
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).
@TheRealBeady
@TheRealBeady 2 года назад
"Bitcoins about 40000 it might have dipped a little bit today" Man that did not age well
@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
@disco.jellyfish
@disco.jellyfish 3 года назад
8:12 Although this result isnt exactly incorrect. 0.8999999999... = 0.9. Yes it would be a wired way to write it down, but it does work out. 8:48 If we were talking about infinite 9s, actually it does as I stated before.
@mikechappell4156
@mikechappell4156 2 года назад
No 0.89999999999999999999... does not equal 0.9. The repeating decimal approaches 0.9 as a limit, but if you subtract the repeating decimal from 0.9 you will always get 0.00000000000000000000...1. I think some chips might round numbers at an arbitrary precision or test for fuzzy equality, but mathematically they differ, so a test for equality should fail.
@disco.jellyfish
@disco.jellyfish 2 года назад
@@mikechappell4156 1/inf converges to 0. So 1/inf = 0. A nice way to illustrate 1/inf=0 is a line and points on that line. How much Volume does a square have? None - a square is a two dimensioanl plane, Volume is a property, only avaliable in three or higher dimensional space. How much area, does a line have? None, since area is a property of two or higher dimensional space. You can guess how long a point is then, right? It obviously has not volume, area or length. Thats what makes it a point. A point can be inside any dimensional space, but does not extend in it in any way - not even length. If you now ask yourself how many points you could fit on a line with a length of 1, you would come to the conclusion that you can fit any amount of points on that line. And its the same for a line, with half the length. This must mean that inf*0 is 1, right? But it could also be 2. So does 1 = 2? No, of course it doesnt. In reality 0*inf is undefined. You gotta be very careful, when messing with 0 and infinity. But keep in mind that you can fit any amount of points on any line, any area, any room, any hyper room and so on. Which leads us back to 1/inf = 0.
@mikechappell4156
@mikechappell4156 2 года назад
@@disco.jellyfish Limits convey approximation, possibly infinitely precise, not equality. Good enough for government work does not work for logical comparisons.
@fdntrinity
@fdntrinity 2 года назад
@@mikechappell4156 Incorrect, it has been proven that 0.89999... (continuing) = 0.9. Its not approximately but exactly 0.9. Mathematically, those two expressions are the same number! Your example with the difference does not hold up, because you are treating 0.89999... as a finite number. Which it is not. Computers simply can't deal with infinity - this is where the float error occurs.
@tostupidforname
@tostupidforname 2 года назад
Thanks for reminding me i completely forgot this somehow
@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.
@turwaith
@turwaith 2 года назад
I learned this when my boss in my internship jumped at me screaming why I was using == to compare two doubles.
@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
@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
@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).
@kekistanifreedomfighter4197
@kekistanifreedomfighter4197 3 года назад
I had always wondered why this was & why a lot of high end computational or professional grade hardware, tout double floating point precision.
@AshnSilvercorp
@AshnSilvercorp 3 года назад
it's interesting, a ton of AAA games right now clearly use floats in their damage calculations when they could have just ran it as an integer and converted it to decimal for human reading later. The one's I know personally are Monster Hunter World and Smash Bros Ultimate (there was one screen where someone showed a move in training registering different in total and in individual hit values. When I saw the point difference, I knew it had to be caused by floats in some way.) It's not a bad practice for physics processes as they're already really complex anyways, but damage calculations probably waste by using anything but integers.
@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.
@ejonesss
@ejonesss 2 года назад
i see now how it is 45 instead of 60. the math is first taking 7*6 witch is 42 then adding 3 to make 45. i thought it would take 3+7 to make 10 then 10*6 making 60. you could calculate the coin to a more precise number so the differences is only 1 cent
@bushlshd
@bushlshd 3 года назад
A computer is just a beefed up calculator
@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
@shadowqueen2134
@shadowqueen2134 2 года назад
Literally knew nothing about floats or comp sci but u explained it well without me feeling dumb
@PatchCornAdams723
@PatchCornAdams723 2 года назад
The way you talk kinda reminds me of Styxhexenhammer, lol. Love your vids man, I'm binging them tonight.
@dendritedigital2430
@dendritedigital2430 2 года назад
I don't know why computers can't sort this out on a basic level? Either leave it in factional form or use a number system that has the factors you are using. Behind the scene you could have a base like 16 * 9 * 49 * 11 * 13 = 1009008 and get an integer value that is exactly right. It would be close to the metric system for computers ( 1024 ). Example: 200 / 3 = 200 * 1009008 / 3 = 67267200 / 1009008 = 66.666666666666...
@bettercalldelta
@bettercalldelta Год назад
Correction (maybe I just misunderstood him but anyway): 1/3 is a real number. It's just that it's impossible to represent it finitely in base-10. Same with 2/3, 1/9, etc.
@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
@twoshedsjackson6478
@twoshedsjackson6478 3 года назад
I was taught about this in Computer Studies in 1973. I'm surprised it's still a thing. IIRC, 20 years ago VB6 had a "CURRENCY" data type. Incidentally, simple maths is why we used the imperial measurement system, all the major fractions of a foot are whole numbers in inches and thus are easier for mental arithmetic, particularly for builders.
@Megalomaniakaal
@Megalomaniakaal 2 года назад
US Customary*
@Evan490BC
@Evan490BC 2 года назад
It will always be a thing. There is no way around it, as you are trying to represent an uncountably infinite set of numbers into a digital machine. The only thing you can do is be very careful and control error. There are some arbitrary-precision arithmetic libraries, but there is no free lunch.
@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
@matthewfala
@matthewfala 2 года назад
‘We all “float” down here’
@Ignatiusussy
@Ignatiusussy 8 месяцев назад
Fun fact: 0.89999... DOES equal 0.9 (in most cases) since for a number to be different from another there has to be something else between them on the number line, and in this case there can't be. Same for 0.999... = 1
@l3alamiya
@l3alamiya 2 года назад
Most people have 10 fingers! A famous singer in Morocco got 6 and we call him stati which means the sixthier
@russotragik
@russotragik 8 месяцев назад
In M68K(Sega Genesis) I had to mimic the way we do math, since the M68K doesn't have an FPU, and its Fix16 float representation is quite broken. So I made my own "Fix16", first 16 bits it's the integer part, last 16 bits is broken into 4 nibbles as positional integer part of the decimal and for most of the part, I only used 3 decimal precision, going from the leftmost to the rightmost, and when the rightmost reached a value of 10 or more, it would subq #10 from it and it would addq #1 to the integer part, this wasn't fast as standard Fix16, but was way to precise for 1.4142 😂😂😂
@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
@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...
@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).
@pseudonymity0000
@pseudonymity0000 2 года назад
Mental Outlaw : You cannot add an integer to a string. JS: Hold my beer.
@edgardosantamaria5399
@edgardosantamaria5399 2 года назад
Thank you man, for doing this videos
@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.
@acf2802
@acf2802 2 года назад
Computers don't screw up floating point math. They are perfect at it (excluding a few rare Intel chips from the 90s.) FP is literally just binary numbers in scientific notation, which is something you should have already learned about and understand. Rounding errors are a fundamental limitation of numerical computation, not a limitation of FP. People make this subject 1000x more complicated than it needs to be due to their own ignorance and assuming it's complicated.
@eugenb9017
@eugenb9017 2 года назад
Always remember: a computer will do what you *instruct* him what to do, not you *want*/expect him to do.
@ryansullivan3085
@ryansullivan3085 2 года назад
The most mind-blowing part of this was the realization that we probably use base 10 because we have 10 fingers. What the fuck? How have I never realized this?
@venn7257
@venn7257 2 года назад
as a newbie, found this recently when trying to make a simple mock calculator. Solution was to round the decimal
@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 8 месяцев назад
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
@JorgeEscobarMX
@JorgeEscobarMX 2 года назад
I see, youtube recommended this on the anniversary of the video.
Далее
Floating Point Numbers - Computerphile
9:16
Просмотров 2,3 млн
Why Slower Computers Were Faster
10:57
Просмотров 245 тыс.
Do This Before Putting Your Files in the Cloud
12:56
Просмотров 162 тыс.
Naming Things in Code
7:25
Просмотров 2,1 млн
Fast Inverse Square Root - A Quake III Algorithm
20:08
These Keys Shouldn't Exist | Nostalgia Nerd
19:32
Просмотров 657 тыс.
I tried using AI. It scared me.
15:49
Просмотров 7 млн
Feds Couldn't Crack Signal, So They Banned It
8:50
Просмотров 479 тыс.
Search Engine Review
9:43
Просмотров 323 тыс.
Harder Drive: Hard drives we didn't want or need
36:47
UEFI Malware - The Low Level Threat To Millions of PCs
8:03
CONCURRENCY IS NOT WHAT YOU THINK
16:59
Просмотров 95 тыс.