fedit.pl
  • Communities
  • Create Post
  • Create Community
  • heart
    Support Lemmy
  • search
    Search
  • Login
  • Sign Up
abhi9u@lemmy.world to Technology@lemmy.worldEnglish · 1 year ago

How Python Compares Floats and Ints: When Equals Isn’t Really Equal

blog.codingconfessions.com

external-link
message-square
15
fedilink
86
external-link

How Python Compares Floats and Ints: When Equals Isn’t Really Equal

blog.codingconfessions.com

abhi9u@lemmy.world to Technology@lemmy.worldEnglish · 1 year ago
message-square
15
fedilink
How Python Compares Floats and Ints: Why It Can Give Surprising Results
blog.codingconfessions.com
external-link
Another Python gotcha and an investigation into its internals to understand why this happens
alert-triangle
You must log in or register to comment.
  • Siegfried@lemmy.world
    link
    fedilink
    English
    arrow-up
    13
    ·
    1 year ago

    If i’m comparing ints with floats, it is my fault in the first place

    • warrenson@lemmy.world
      link
      fedilink
      English
      arrow-up
      1
      ·
      1 year ago

      Exactly, I’d expect a warning, if not an error.

  • Eximius@lemmy.world
    link
    fedilink
    English
    arrow-up
    5
    ·
    1 year ago

    Did nobody read the manual?

    IEEE 754 double precision: The 53-bit significand precision gives from 15 to 17 significant decimal digits precision.

  • 🇦🇺𝕄𝕦𝕟𝕥𝕖𝕕𝕔𝕣𝕠𝕔𝕕𝕚𝕝𝕖@lemm.ee
    link
    fedilink
    English
    arrow-up
    1
    arrow-down
    1
    ·
    1 year ago

    Do we have a js type situation here

    • Valmond@lemmy.world
      link
      fedilink
      English
      arrow-up
      10
      ·
      1 year ago

      Probably more like the old precision problem. It ecists in C/C++ too and it’s just how fliats and ints work.

      • 🇦🇺𝕄𝕦𝕟𝕥𝕖𝕕𝕔𝕣𝕠𝕔𝕕𝕚𝕝𝕖@lemm.ee
        link
        fedilink
        English
        arrow-up
        5
        arrow-down
        5
        ·
        1 year ago

        I dont think comparisons should be doing type conversion if i compare a float to an int i want it to say false cos types are different.

        • Womble@lemmy.world
          link
          fedilink
          English
          arrow-up
          4
          ·
          1 year ago

          But how far should that be taken should 8 == 8 return false because one is an unsigned int and the other is signed? Or 0.0 == 0.0 where they are floats and doubles? You can make a case for those due to a strong type system but it would go against most peoples idea of what equality is.

          • 🇦🇺𝕄𝕦𝕟𝕥𝕖𝕕𝕔𝕣𝕠𝕔𝕕𝕚𝕝𝕖@lemm.ee
            link
            fedilink
            English
            arrow-up
            1
            ·
            1 year ago

            If bits aren’t same then i dont want it to tell me they are the same. And python just has one implementation for int and float.

            I like python cos everything’s an object i dont want different types of objects to evaluate the same they are fundamentally different objects is that not what u would expect?

            • Womble@lemmy.world
              link
              fedilink
              English
              arrow-up
              2
              ·
              1 year ago

              Even in python you can have control of what types of numbers are used under the hood with numpy arrays (and chances are if you are using floats in any quantity you want to be using numpy). I would be very surprised if array([1,2,3], dtype=uint8) == array([1,2,3], dtype=int16) gave [False, False, False]. In general I think == for numbers should give mathematical equivalence, with the understanding that comparing floats is highly likely to give false negatives unless you are extremely careful with what you are comparing.

              • 🇦🇺𝕄𝕦𝕟𝕥𝕖𝕕𝕔𝕣𝕠𝕔𝕕𝕚𝕝𝕖@lemm.ee
                link
                fedilink
                English
                arrow-up
                1
                ·
                1 year ago

                Numpys more or less a math wrapper for c isnt it?

                • Womble@lemmy.world
                  link
                  fedilink
                  English
                  arrow-up
                  2
                  ·
                  edit-2
                  1 year ago

                  More Fortran than C, but its the same for any language doing those sorts of array mathematics, they will be calling compiled versions of blas and lapack. Numpy builds up low level highly optimised compiled functions into a coherant python ecosystem. A numpy array is a C array with some metadata sure, but a python list is also just a C array of pointers to pyobjects.

        • Sylvartas@lemmy.world
          link
          fedilink
          English
          arrow-up
          4
          ·
          1 year ago

          That makes sense, but then you’d just have people converting the int to a float manually and run into the exact same issues.

          • 🇦🇺𝕄𝕦𝕟𝕥𝕖𝕕𝕔𝕣𝕠𝕔𝕕𝕚𝕝𝕖@lemm.ee
            link
            fedilink
            English
            arrow-up
            4
            ·
            1 year ago

            I mean honestly its expected u should check floats similarity not equivalence.

            • Sylvartas@lemmy.world
              link
              fedilink
              English
              arrow-up
              5
              arrow-down
              1
              ·
              1 year ago

              Agreed. But the less experienced programmers I know are surprisingly naive about this.

              • 🇦🇺𝕄𝕦𝕟𝕥𝕖𝕕𝕔𝕣𝕠𝕔𝕕𝕚𝕝𝕖@lemm.ee
                link
                fedilink
                English
                arrow-up
                3
                ·
                1 year ago

                Yeah its gonna be one of those problems chatgpt ain’t gonna help

    • perviouslyiner@lemmy.world
      link
      fedilink
      English
      arrow-up
      1
      ·
      1 year ago

      deleted by creator

Technology@lemmy.world

technology@lemmy.world

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: [email protected]

This is a most excellent place for technology news and articles.


Our Rules


  1. Follow the lemmy.world rules.
  2. Only tech related news or articles.
  3. Be excellent to each other!
  4. Mod approved content bots can post up to 10 articles per day.
  5. Threads asking for personal tech support may be deleted.
  6. Politics threads may be removed.
  7. No memes allowed as posts, OK to post as comments.
  8. Only approved bots from the list below, this includes using AI responses and summaries. To ask if your bot can be added please contact a mod.
  9. Check for duplicates before posting, duplicates may be removed
  10. Accounts 7 days and younger will have their posts automatically removed.

Approved Bots


  • @[email protected]
  • @[email protected]
  • @[email protected]
  • @[email protected]
Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 3.45K users / day
  • 8.72K users / week
  • 17.6K users / month
  • 37.5K users / 6 months
  • 5 local subscribers
  • 69.9K subscribers
  • 7.8K Posts
  • 199K Comments
  • Modlog
  • mods:
  • L3s@lemmy.world
  • enu@lemmy.world
  • Technopagan@lemmy.world
  • L4sBot@lemmy.world
  • L3s@hackingne.ws
  • L4s@hackingne.ws
  • UI: 0.19.8
  • BE: 0.19.9
  • Modlog
  • Instances
  • Docs
  • Code
  • join-lemmy.org