Int overflow (or not) in C++, Ruby, Zig, Rust, & Pony

Sdílet
Vložit
  • čas přidán 6. 07. 2024
  • Code: github.com/contextfreecode/nu...
    0:00 Intro
    0:25 C++ (cpp)
    2:30 Ruby
    3:23 Zig
    5:22 Rust
    6:29 Pony
    8:43 Outro
  • Věda a technologie

Komentáře • 75

  • @jihoonkim9766
    @jihoonkim9766 Před rokem +212

    In Rust, you can also use the `Wrapping` type to get wrapping arithmetic with regular operators like `+`, `-` instead of using explicit method calls.

    • @JannisAdmek
      @JannisAdmek Před 3 měsíci

      that's really cool, didn't know this one!

  • @Pixel__Man
    @Pixel__Man Před rokem +26

    Student here! Currently trying to learn how to create a programming language (maybe not from scratch, but at least with great tooling and stuffs) and your channel is really helping me see how other languages are working, and, if I finally start creating my own, what behavior I would like.
    Thanks again for your work! Subbed!

  • @user-tk2jy8xr8b
    @user-tk2jy8xr8b Před rokem +6

    In C# there are keywords `checked` and `unchecked` to create a code region or wrap you expression with

  • @OpenSourceAnarchist
    @OpenSourceAnarchist Před rokem +12

    I really like the Wolfram Language. I know it's proprietary, but nothing else even comes close for working with data, visualization, complex transformations, etc. Very slow but accurate, powerful, and expressive!

  • @contextfree
    @contextfree  Před rokem +16

    As an aside, while I didn't demo it, you can get specific bit sizes (and more) on your ints in C or C++ using stdint.

    • @contextfree
      @contextfree  Před rokem +3

      Also, here's the link to the C3 blog post shown in the video: c3.handmade.network/blog/p/7656-c3__handling_casts_and_overflows_part_1

    • @Ryan-xq3kl
      @Ryan-xq3kl Před rokem +2

      @@vextechexcept that it isnt actually overflow proof and has caused thousands of exploits due to accidental rounding

  • @inkryption3386
    @inkryption3386 Před rokem +24

    In zig there's also some stdlib alternatives that allow you to do error handling on overflow/underflow in std.math, like add, sub, div, mul, which all return error unions instead of panicking. There's also mulWide, which returns an integer type which has enough bits to hold the largest values of both operands' types.
    Also also, there's "cast", which returns an optional for if the argument can't cast into the specified type without overflowing.

    • @unarei
      @unarei Před rokem +5

      also lossyCast which is a saturating cast, so lossyCast(u8, 300) → 255

    • @jobat_
      @jobat_ Před rokem +4

      there are also the compiler builtins of @addWithOverflow etc which allow you to check if an overflow happened at runtime

    • @inkryption3386
      @inkryption3386 Před rokem +2

      @@jobat_ yeah, the std.math functions are implemented in terms of them as appropriate.

  • @malwaretestingfan
    @malwaretestingfan Před rokem +1

    Very interesting video as usual, Context Free.

  • @raianmr2843
    @raianmr2843 Před rokem +10

    You should do videos on concurrency models. Would love to see a comparison between Go, Elixir, and Clojure in this space.

  • @richtourist
    @richtourist Před rokem +1

    Superb Channel. Thanks!

  • @AdamFraserKruck
    @AdamFraserKruck Před 9 měsíci +1

    Subbed! Great video. I'm currently considering what to do in the fin language. It cares more about safety than speed (controlling vehicles) but also can't just panic. How to achieve and be ergonomic?
    Great info. Didn't know about new c23 features or c3 language. Cheers!

  • @porky1118
    @porky1118 Před rokem +3

    Nice, you also mentioned C3.

  • @letMeSayThatInIrish
    @letMeSayThatInIrish Před rokem +21

    Wow, I like the saturating operator in Zig!

    • @inkryption3386
      @inkryption3386 Před rokem +9

      It's perhaps one of my favorite niche and useful things that I didn't realize I needed

    • @malwaretestingfan
      @malwaretestingfan Před rokem +3

      I agree, it's extremely useful.

    • @carlosmspk
      @carlosmspk Před rokem +1

      I'd say most of the times saturating is also a bad thing. Might even be worse because your program might not be acting as you expect it and it's hard to understand why (while in wrap-around it's usually a bit more obvious)

    • @inkryption3386
      @inkryption3386 Před rokem +1

      @@carlosmspk how can it be a bad thing if it's the behaviour you want. It's not enabled by default, it's explicitly opt-in.

    • @carlosmspk
      @carlosmspk Před rokem

      @@inkryption3386 I know I know, I'm arguing that I don't see any case where you'd want it. You might opt for it with the rationale "well, if things unexpectedly overflow, I want them to at least not be so different that they wrap around", and I'm saying that if that's the mindset, it seems to me like it's worse just because it will make the unexpected outcome's source harder to understand (possibly).

  • @briandublidi4708
    @briandublidi4708 Před 8 měsíci +2

    in BQN the ints would turn into floats automatically

  • @porky1118
    @porky1118 Před rokem +4

    The Pony behavior is so much better than the one in Rust. I think, Rust shouldn't have any functions which panic by default anymore. Everything should return a result or similar. Especially after there is the question mark operator. Maybe in Rust 2 ;)

    • @tjgdddfcn
      @tjgdddfcn Před rokem +1

      Doing this would force you to do unwrap or something else whenever you add two numbers. I think panicing at it only in debug mode is ok since you’re never going to publish in it

    • @porky1118
      @porky1118 Před rokem +3

      @@tjgdddfcn For some time I always compiled in Release mode since the compiliation time is not so much higher, but the runtime speed was a lot lower. So I published a few versions of some program which had this bug.

  • @carlosmspk
    @carlosmspk Před rokem +3

    I wonder the usefulness of having integers with, say, 37 bit size, instead of 64

  • @austinmajeski9427
    @austinmajeski9427 Před rokem +1

    Now what about how every language and calculator handles modulus differently?
    What do you do when you get that secondary, negative remainder?

  • @porky1118
    @porky1118 Před rokem +1

    4:52 That might already be possible using generics. The underlying type won't be i127 anyway, but probably still a i128 in cases like these. Or the generic would use tuples for the inner types.
    Probably not viable in most programming languages. I don't think, the Rust generics are close to being powerful enough without a lot of repetition, maybe C++ templates and for sure one of the Scopes macros.

  • @tylovset
    @tylovset Před rokem +2

    I am trying to use long long (guaranteed at least 64 bits) or int64_t everywhere for computation and parameter passing. The small overhead on 32-bit platforms can mostly be ignored, and you virtually never gonna get wrap-around problems. Its annoying that libraries insists on using unsigned for sizes, as *signed op unsigned* gives *unsigned* as result, which is unintuitive.

    • @contextfree
      @contextfree  Před rokem +1

      Makes sense. I have one project where I purposely using 32-bit ints because I *think* it's never going to be an issue for my use case, and then I get to store half the memory and make better use of cache for large arrays. But I have no idea if it will matter in performance in reality. Maybe if I get far enough, I can switch the typedefs to 64-bit and see if there's a noticeable difference.

    • @contextfree
      @contextfree  Před rokem +1

      And I'm pretty convinced 16-bit is likely too small for my use case, which is why I'm sticking to 32 so far.

    • @itellyouforfree7238
      @itellyouforfree7238 Před rokem +3

      well, people use unsigned for sizes because that's what they are. i still have to see a vector of length -4. this adheres to the principle "make impossible state unrepresentable"

  • @cerulity32k
    @cerulity32k Před rokem +2

    math would never

  • @sopadebronha
    @sopadebronha Před rokem +5

    You know, from my perspective its really funny to watch Software programmers struggling with variables overflow while I, a Firmware programmer, am so used to deal with variables and registers overflow from working with 8 or 16 bits MCUs that this kinda o thing is not even perceived as a problem to me anymore.

  • @professornumbskull5555
    @professornumbskull5555 Před rokem +3

    4:50 that's exactly what dynamic languages like python and JS do... They increment the type if they sense overflow

    • @contextfree
      @contextfree  Před rokem +1

      Thanks for the comment! To clarify, I meant statically sized. As in i3 + i4 has static type i5, and functions could be generic in int size.

    • @SolarLiner
      @SolarLiner Před rokem +6

      That's not true in JS, it uses 64-bit floats even for integer values and operations.

    • @contextfree
      @contextfree  Před rokem

      @@SolarLiner Good point as well. Bigints are a separate type in JS.

    • @contextfree
      @contextfree  Před rokem +1

      Also, in Python 3 and Ruby, I don't think bigints count as a separate type from smaller ints. Just a more efficient handling when they're small. I think Python 2 had a distinct "long" type for bigints, though, even if they added the automatic promotion at some point in the evolution.

  • @aciddev_
    @aciddev_ Před rokem +2

    try jakt

  • @HuntingKingYT
    @HuntingKingYT Před rokem +2

    JS autofloat😎

    • @contextfree
      @contextfree  Před rokem +1

      Yeah, also wouldn't hit the limit so soon because more bits in mantissa.

  • @ahmaddynugroho
    @ahmaddynugroho Před rokem +1

    what the hell is PONY?!

    • @mirabilis
      @mirabilis Před rokem +3

      A small horse.

    • @AdrianBoyko
      @AdrianBoyko Před rokem

      “Pony is an open-source, object-oriented, actor-model, capabilities-secure, high-performance programming language.” I like it a lot.

  • @noideanoidea3068
    @noideanoidea3068 Před rokem +1

    C solo neg diff

  • @JohnWilliams-gy5yc
    @JohnWilliams-gy5yc Před rokem +1

    Languages in a high-school drama...
    C++ : As always, you will never pay a cycle for ubsan and boost mp math when you're not using them even at compile time.
    Rust : I personally choose to buy a Volvo XC and I'm using it. Huh, You won't believe those people's cheap by default "philosophy". Duh!
    C++ : Why are you that angry all the time?
    Rust: I just hate hackers that keep selling gazillion zero-days and get rich quick, not you guys TBH.
    Zero-day: Don't look at me. Blame it on capitalism and free market.
    Communism: You see? That's why the people must be kept in a lockdown forever. They well deserve it.
    Communism Propaganda: Creating youtube news channels while suppressing press freedom under sovereignity are not related and not hypocrite at all. Be logical, guys.

    • @inkryption3386
      @inkryption3386 Před rokem +1

      I don't understand the latter criticism in this comment. That happens in both capitalist and communist societies lol.

    • @JohnWilliams-gy5yc
      @JohnWilliams-gy5yc Před rokem +1

      @@inkryption3386 Exactly how propaganda is supposed to work in the mind. One must find Marx is never worse than the rest ever in every aspect. They are almost flawless to the subject so to speak. It's a sole reason to protect the party at any cost. That's simply logical to the subject.

    • @inkryption3386
      @inkryption3386 Před rokem +5

      @@JohnWilliams-gy5yc I think you're perhaps confusing authoritarianism with Marxism, and Marx with Stalin/Lenin. At any rate, the same shit happens in capitalism, people worship figures of authority, they're just not always state-sanctioned.

    • @JohnWilliams-gy5yc
      @JohnWilliams-gy5yc Před rokem

      @@inkryption3386 Yeah, you're right. Marx can solely exists in reality without any forms of authoritarianism. How come I missed it. I'm sorry that I'm too confused to understand that facts. Your view is completely logical without any artificial influence. I'm impressed. Thanks for correcting me.

    • @_tsu_
      @_tsu_ Před rokem

      CZcams communism?? bro are you 8 years old?

  • @monad_tcp
    @monad_tcp Před rokem +1

    The biggest lie in systems languages like Rust/C++/Zig or anything using LLVM is that they're fast.
    Yes, you go fast by being inexact. Basically you're giving slightly wrong results, that's the magic of 'native' languages.

    • @patryk_49
      @patryk_49 Před rokem +23

      What do you mean by "wrong"? if something does what its documentation/specification says, it's not wrong.

    • @electric26
      @electric26 Před rokem +18

      So the lie isn't that they're fast, it's that they use native processing? Even Python has inaccurate floats.

    • @monad_tcp
      @monad_tcp Před rokem

      @@patryk_49 That's a cope isn't it?
      You're justifying the sacrifice in behavior of the computing environment.
      For example, weak memory models.
      And ironically most Undefined Behaviors are not defined, they're by omission. Some of them are what's called "implementation detail"
      The LLVM IR suffer less from that, but it also has its undefined behaviors like aliasing of memory pointers.
      Documenting a bug doesn't make it less of a bug in design.
      It's a matter of perspectives.

    • @monad_tcp
      @monad_tcp Před rokem

      @@electric26 Because that's an implementation detail of IEE754.
      There are overflows, C++/LLVM IR pretend they don't exist.
      Why on earth would you disable overflow checking?
      It's a failure of abstraction.
      That's the lie.
      One of the lies C++ and native languages tells you.
      They are literally trading off precision for performance. They're not fast.

    • @electric26
      @electric26 Před rokem +9

      @@monad_tcp what's ironic about undefined behaviours being undefined?