Why Computers Screw up Floating Point Math

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 พ.ย. 2024

ความคิดเห็น • 849

  • @teamrabbitalec
    @teamrabbitalec 3 ปีที่แล้ว +3990

    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 ปีที่แล้ว +242

      always use AT LEAST decimals.

    • @trolololo720
      @trolololo720 3 ปีที่แล้ว +208

      @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 ปีที่แล้ว +58

      Either doubles or decimals

    • @askhowiknow5527
      @askhowiknow5527 3 ปีที่แล้ว +159

      @Anon double is short for “double-precision floating point”
      By decimal they mean fixed precision or integers

    • @saschadenzler848
      @saschadenzler848 3 ปีที่แล้ว +20

      @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 ปีที่แล้ว +2006

    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 ปีที่แล้ว +157

      Split proton in a perfect half

    • @no-better-name
      @no-better-name 3 ปีที่แล้ว +321

      "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 ปีที่แล้ว +207

      @@groszak1 problem: proton is made of 3 quarks, and those are indivisible

    • @censoredterminalautism4073
      @censoredterminalautism4073 3 ปีที่แล้ว +77

      @@no-better-name Again, REEE. The universe is broken. We must fix it.

    • @no-better-name
      @no-better-name 3 ปีที่แล้ว +99

      @@censoredterminalautism4073 god is yandere dev confirmed, who else would mess up this badly? xd

  • @DoggyBingBong
    @DoggyBingBong 3 ปีที่แล้ว +1673

    floating point errors:
    cool for minecraft world gen
    HORRIBLE for banking

    • @charautreal
      @charautreal 3 ปีที่แล้ว +47

      Welp, not for positions in bedrock tho

    • @DoggyBingBong
      @DoggyBingBong 3 ปีที่แล้ว +65

      @@charautreal bedrock is shit

    • @charautreal
      @charautreal 3 ปีที่แล้ว +26

      @@DoggyBingBong Yeah, a lot of things were crap in 2018

    • @glitchgaming766
      @glitchgaming766 2 ปีที่แล้ว +17

      @@charautreal +now

    • @kiyu3229
      @kiyu3229 2 ปีที่แล้ว +9

      @@charautreal they are shit now too

  • @gavinridley5727
    @gavinridley5727 3 ปีที่แล้ว +367

    Gets way worse when you're adding lots of floats, too.

    • @WandererOfWorlds0
      @WandererOfWorlds0 3 ปีที่แล้ว +26

      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 ปีที่แล้ว +11

      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 ปีที่แล้ว +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 ปีที่แล้ว +442

    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 ปีที่แล้ว +4

      Thanks, you're a life saver

    • @khhnator
      @khhnator 2 ปีที่แล้ว +36

      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 ปีที่แล้ว +1

      @@khhnator example of those?
      I see people say be careful but none explaining what's careful

    • @khhnator
      @khhnator 2 ปีที่แล้ว +6

      @@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 ปีที่แล้ว +3

      @@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.

  • @whig3982
    @whig3982 3 ปีที่แล้ว +861

    >Calling "bits" "decimal places" repeatedly
    Unsubbed and flagged for racism.

    • @onetwobits7558
      @onetwobits7558 3 ปีที่แล้ว +44

      1 decimal place != 1 bit

    • @GrEEnEyE089
      @GrEEnEyE089 3 ปีที่แล้ว +51

      in this context it should be binary places

    • @hossamdash
      @hossamdash 3 ปีที่แล้ว +15

      Should've just used digits

    • @sodiboo
      @sodiboo 3 ปีที่แล้ว +4

      @@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 ปีที่แล้ว +38

      @@sodiboo lol no a digit is the general name for any base number system. BIT is literally short for Binary digIT.

  • @wolverine9632
    @wolverine9632 2 ปีที่แล้ว +232

    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 ปีที่แล้ว +29

      yup, perfectly alignes with how the mantissa and the exponent works in floats

  • @jonathanrash4146
    @jonathanrash4146 3 ปีที่แล้ว +129

    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?

  • @PortocaliusMaximus
    @PortocaliusMaximus 3 ปีที่แล้ว +154

    >that constant 99% CPU usage

    • @sharoyveduchi
      @sharoyveduchi 3 ปีที่แล้ว +18

      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 ปีที่แล้ว +28

      @@sharoyveduchi OBS uses ffmpeg

    • @sharoyveduchi
      @sharoyveduchi 3 ปีที่แล้ว +4

      @@notuxnobux and? Pulse audio uses Alsa but nobody actually uses Alsa by itself.

    • @notuxnobux
      @notuxnobux 3 ปีที่แล้ว +12

      @@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 ปีที่แล้ว +22

      @@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.

  • @laharl2k
    @laharl2k 3 ปีที่แล้ว +278

    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 ปีที่แล้ว +77

      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 ปีที่แล้ว +40

      That's called fixed point computation, not floating point

    • @joey199412
      @joey199412 3 ปีที่แล้ว +29

      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 ปีที่แล้ว +2

      ​@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 ปีที่แล้ว +1

      @@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 ปีที่แล้ว +272

    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 ปีที่แล้ว +9

      why are the character speeds set to 250 u/s?

    • @skaruts
      @skaruts 2 ปีที่แล้ว +54

      @@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 ปีที่แล้ว +4

      Who ever says that? People who only work with double precision?

    • @tankfire20
      @tankfire20 2 ปีที่แล้ว +1

      @@skaruts Makes sense

    • @Pokornz
      @Pokornz 2 ปีที่แล้ว +7

      180 units tall... Sounds like centimeters ;)

  • @ercole1488
    @ercole1488 3 ปีที่แล้ว +147

    Can't spell Chad without C

    • @purrplaysLE
      @purrplaysLE 3 ปีที่แล้ว +9

      Without capital C specifically? Ok, chad

    • @binarycat1237
      @binarycat1237 3 ปีที่แล้ว +5

      This sounds hilariously obvious out of context.

    • @AlizerLeHaxor
      @AlizerLeHaxor 3 ปีที่แล้ว +3

      ok random guy who cant code

    • @eter9nitysslut131
      @eter9nitysslut131 3 ปีที่แล้ว +3

      @@AlizerLeHaxor how do you know he can't code? lmao

    • @reinhold1616
      @reinhold1616 3 ปีที่แล้ว +5

      @@eter9nitysslut131 just people assuming and acting suprerior.

  • @tomaspecl1082
    @tomaspecl1082 3 ปีที่แล้ว +75

    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 ปีที่แล้ว +4

      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 ปีที่แล้ว +3

      Was it in python? That way you get something that runs at the speed of snail^2

    • @yoavmor9002
      @yoavmor9002 2 ปีที่แล้ว +6

      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 ปีที่แล้ว +3

      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 ปีที่แล้ว +4

      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.

  • @MrC0MPUT3R
    @MrC0MPUT3R 2 ปีที่แล้ว +15

    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.

  • @WandererOfWorlds0
    @WandererOfWorlds0 3 ปีที่แล้ว +378

    NEVER EVER use floating point numbers in logical statements.

    • @WandererOfWorlds0
      @WandererOfWorlds0 3 ปีที่แล้ว +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 ปีที่แล้ว +33

      @linlinö önilnil The classic is 0.1+0.2 == 0.3 and it returns false, but yeah, something like that.

    • @miguelangelmartinezcasado8935
      @miguelangelmartinezcasado8935 3 ปีที่แล้ว +23

      @@WandererOfWorlds0 In those cases you just have to be more cautious, like:
      If(Abs(0.1+0.2 - 0.3)

    • @0xCAFEF00D
      @0xCAFEF00D 3 ปีที่แล้ว +16

      Terrible suggestion.
      Learn floating point.

    • @Howtheheckarehandleswit
      @Howtheheckarehandleswit 3 ปีที่แล้ว +22

      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.

  • @Binary_Bloom
    @Binary_Bloom 3 ปีที่แล้ว +23

    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.

  • @plasmatch8
    @plasmatch8 3 ปีที่แล้ว +77

    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 ปีที่แล้ว +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 ปีที่แล้ว +4

      But very few cpu's support the decimal type natively.

    • @diablo.the.cheater
      @diablo.the.cheater 3 ปีที่แล้ว +2

      @@alexvitkov No, you add those 5 layers of abstraction at the API with the DB. Computing is all about abstraction anyway

    • @nonenull1490
      @nonenull1490 3 ปีที่แล้ว +2

      @@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 ปีที่แล้ว +2

      @@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.

  • @ShadowHunter120
    @ShadowHunter120 3 ปีที่แล้ว +24

    The 4 hidden beneath layers of 0s, truly terrifying.

  • @ghostedyoutuber263
    @ghostedyoutuber263 3 ปีที่แล้ว +12

    round off error is a bitch... especially when dealing with chemistry and computers.

  • @monstertrucks9357
    @monstertrucks9357 3 ปีที่แล้ว +113

    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 ปีที่แล้ว +17

      What exactly are they used to? I'm curious. I always turn everything into int.

    • @MarceloDezem
      @MarceloDezem 2 ปีที่แล้ว +5

      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

  • @YaBoiKuma
    @YaBoiKuma 3 ปีที่แล้ว +27

    From the title I expected an explanation on the binary representation of floating point numbers. Most of the video was examples of screwups.

  • @joansalazar5884
    @joansalazar5884 3 ปีที่แล้ว +55

    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.

  • @egg5474
    @egg5474 3 ปีที่แล้ว +49

    "And if you turn to page 2, you'll see homotopy type theory, which is left as a migraine for the student"

  • @MrLittlePredator
    @MrLittlePredator 3 ปีที่แล้ว +119

    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 ปีที่แล้ว +1

      Bruh

    • @unknownguywholovespizza
      @unknownguywholovespizza ปีที่แล้ว

      Easy, use Rust

  • @Calajese
    @Calajese 3 ปีที่แล้ว +141

    8:13 The moustache man inside the bunker be like

    • @test-cp8uz
      @test-cp8uz 3 ปีที่แล้ว +2

      hi

    • @test-cp8uz
      @test-cp8uz 3 ปีที่แล้ว +1

      @Nicefisher bruh

    • @ClifffSVK
      @ClifffSVK 3 ปีที่แล้ว +1

      @@test-cp8uz bruh moment

    • @test-cp8uz
      @test-cp8uz 3 ปีที่แล้ว +1

      @@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.

  • @sephondranzer
    @sephondranzer 3 ปีที่แล้ว +13

    3:15 in the morning and I finally understand why my excel trackers kept adding .00000001 increments to the results!!
    ...
    Going to sleep now

  • @Jankoekepannekoek
    @Jankoekepannekoek 3 ปีที่แล้ว +11

    Douglas Crockford defined a number type called DEC64 to deal with this issue. It's not widely adopted in programming languages unfortunately.

  • @ChimeraX0401
    @ChimeraX0401 3 ปีที่แล้ว +26

    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 ปีที่แล้ว +1

      Intel has FMA

    • @SebiBubble
      @SebiBubble 2 ปีที่แล้ว +1

      @@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.

  • @ThankYouESM
    @ThankYouESM 3 ปีที่แล้ว +7

    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.

  • @entcraft44
    @entcraft44 2 ปีที่แล้ว +6

    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.

  • @marioschroers7318
    @marioschroers7318 3 ปีที่แล้ว +7

    I love how this man is able to explain stuff. Natural born teacher. Excellent!

  • @carsonfball4
    @carsonfball4 3 ปีที่แล้ว +27

    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 ปีที่แล้ว +1

    Total beginner here, Thanks for such a clear concise explanation on the subject made it really easy to understand!

  • @lool8421
    @lool8421 3 ปีที่แล้ว +17

    some random guy who doesn't know anything about computers: so why don't computers use base-10?

    • @trogdorstrngbd
      @trogdorstrngbd 3 ปีที่แล้ว +13

      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 ปีที่แล้ว +4

      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.

  • @todayonthebench
    @todayonthebench 2 ปีที่แล้ว +3

    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.

  • @mbahmarijan789
    @mbahmarijan789 3 ปีที่แล้ว +7

    free class in the morning... nice 👍👍

  • @codechamp27
    @codechamp27 2 ปีที่แล้ว +4

    "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

  • @CoolJosh3k
    @CoolJosh3k 3 ปีที่แล้ว +7

    Should have mentioned the use of an epsilon value and approximation so that 0.3*3==0.9 can still return true.

  • @tutacat
    @tutacat ปีที่แล้ว +1

    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

  • @radarpixel1304
    @radarpixel1304 3 ปีที่แล้ว

    The amount of information that you cover on your channel, is staggering. I'm a happy subscriber.

  • @p0t4toePotato
    @p0t4toePotato 3 ปีที่แล้ว +16

    who the hell stores monetary value with floating point?

    • @Jamiered18
      @Jamiered18 3 ปีที่แล้ว +1

      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

  • @SashaFYI
    @SashaFYI 3 ปีที่แล้ว +14

    Subbed for privacy tips, yet perpetually confused by vids with coding and math.

    • @hhhyyy4375
      @hhhyyy4375 3 ปีที่แล้ว

      Inevitable on a tech channel.

  • @stumbling
    @stumbling 3 ปีที่แล้ว +4

    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 /

  • @Adam_Lyskawa
    @Adam_Lyskawa 3 ปีที่แล้ว +2

    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.

  • @gfuentes8449
    @gfuentes8449 2 ปีที่แล้ว

    One of the most powerful ways to teach is by metaphor/comparison and this brings it home really well

  • @honeybeeanie
    @honeybeeanie 3 ปีที่แล้ว +6

    This mans CPU is pinned at 100% when using python interpreter..5:33

  • @ConernicusRex
    @ConernicusRex 2 ปีที่แล้ว +1

    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. 🤣🤣🤣🤣

  • @hhhyyy4375
    @hhhyyy4375 3 ปีที่แล้ว +1

    There are workarounds. This is a nice video to introduce an idiosyncrasy most scripters have seen.

  • @salsamancer
    @salsamancer 2 ปีที่แล้ว +1

    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

  • @km5405
    @km5405 2 ปีที่แล้ว +2

    fixed point integers is another workaround for this that also sometimes can be more performant depending on the application/processor.

  • @shadowqueen2134
    @shadowqueen2134 2 ปีที่แล้ว

    Literally knew nothing about floats or comp sci but u explained it well without me feeling dumb

  • @asdasddas100
    @asdasddas100 3 ปีที่แล้ว +13

    More programming videos please :)

  • @blacklight683
    @blacklight683 2 ปีที่แล้ว +1

    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

  • @Tubeytime
    @Tubeytime 2 ปีที่แล้ว +4

    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.

  • @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.

  • @disco.jellyfish
    @disco.jellyfish 3 ปีที่แล้ว +5

    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.

  • @PatchCornAdams723
    @PatchCornAdams723 2 ปีที่แล้ว

    The way you talk kinda reminds me of Styxhexenhammer, lol. Love your vids man, I'm binging them tonight.

  • @aussiekai
    @aussiekai 2 ปีที่แล้ว

    1:46, i love it how u say, probably because "most" people have ten fingers.

  • @franspai7415
    @franspai7415 3 ปีที่แล้ว

    This one 10 min video taught me more than 6h of cs classes at uni.

  • @camiloospinaa
    @camiloospinaa 3 ปีที่แล้ว +1

    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 3 ปีที่แล้ว

      Good catch! 0.011011... is actually 3/7, not 1/3

  • @LordOfNihil
    @LordOfNihil 3 ปีที่แล้ว +1

    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.

  • @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

  • @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 ปีที่แล้ว +1

      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.

  • @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).

  • @TheRealBeady
    @TheRealBeady 2 ปีที่แล้ว +1

    "Bitcoins about 40000 it might have dipped a little bit today"
    Man that did not age well

  • @turwaith
    @turwaith 2 ปีที่แล้ว

    I learned this when my boss in my internship jumped at me screaming why I was using == to compare two doubles.

  • @TricoliciSerghei
    @TricoliciSerghei 6 หลายเดือนก่อน

    Best explanation, thank you very much!!

  • @edgardosantamaria5399
    @edgardosantamaria5399 2 ปีที่แล้ว

    Thank you man, for doing this videos

  • @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.

  • @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.

  • @skilz8098
    @skilz8098 3 ปีที่แล้ว +1

    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

  • @devids51
    @devids51 2 ปีที่แล้ว

    Really enjoy the computer logic/coding lessons

  • @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.

  • @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.

  • @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).

  • @rex_melynas
    @rex_melynas 3 ปีที่แล้ว +5

    *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 ปีที่แล้ว +1

      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.

  • @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!

  • @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.

  • @venn7257
    @venn7257 2 ปีที่แล้ว

    as a newbie, found this recently when trying to make a simple mock calculator. Solution was to round the decimal

  • @unsigned_long_long
    @unsigned_long_long 2 ปีที่แล้ว

    you would make a great professor

  • @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

  • @bushlshd
    @bushlshd 3 ปีที่แล้ว +8

    A computer is just a beefed up calculator

  • @emmata98
    @emmata98 2 ปีที่แล้ว +1

    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

  • @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 11 หลายเดือนก่อน

      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

  • @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.

  • @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 ปีที่แล้ว +1

      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.

  • @eugenb9017
    @eugenb9017 2 ปีที่แล้ว

    Always remember: a computer will do what you *instruct* him what to do, not you *want*/expect him to do.

  • @crispyrice
    @crispyrice 3 ปีที่แล้ว +4

    I feel like it would've been nicer if you called binary decimal places, just binary places, since decimal literally means "of base ten"

  • @blacklight683
    @blacklight683 2 ปีที่แล้ว +1

    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

  • @ff-jt8un
    @ff-jt8un 3 ปีที่แล้ว +134

    >python is the language of choice for mathematicians and physicists
    >can't even do math properly
    Check mate python normies

    • @hanteroes
      @hanteroes 3 ปีที่แล้ว +30

      Language that magically changes computer architecture when?

    • @laharl2k
      @laharl2k 3 ปีที่แล้ว +39

      Python isnt doing the math, the cpu is. Youd get the same result be it in C or in visual basic.

    • @____-gy5mq
      @____-gy5mq 3 ปีที่แล้ว +6

      @@laharl2k I'll create a GUI user interface in visual basic, see if I can track the floating point calculations.

    • @corejake
      @corejake 3 ปีที่แล้ว +8

      @@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 ปีที่แล้ว +9

      @@corejake You know Tensorflow exists?

  • @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).

  • @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?

  • @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

  • @materialknight
    @materialknight 2 ปีที่แล้ว

    1:55 I think it's better to say that each position to the left represents the next POWER of 10

  • @censoredterminalautism4073
    @censoredterminalautism4073 3 ปีที่แล้ว +2

    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.

  • @russotragik
    @russotragik 11 หลายเดือนก่อน

    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 😂😂😂

  • @ejonesss
    @ejonesss 2 ปีที่แล้ว +1

    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

  • @sanduhr32
    @sanduhr32 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))