Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. 10 divided by 3 *multiplied with 3 is not 10 again??

10 divided by 3 *multiplied with 3 is not 10 again??

Scheduled Pinned Locked Moved The Lounge
question
24 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • realJSOPR realJSOP

    Back in the day, I had to write a Pascal function called "AlmostEqual". I passed the values to be checked for "equality", and how many decimal places to check. I converted both values to strings with the appropriate formatting specs, and compared the strings. We needed different precisions depending on where we were in the math calculations. My suggestion is to NOT check doubles for equality without doing something along the same lines.

    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
    -----
    "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #15

    John Simmons / outlaw programmer wrote:

    I passed the values to be checked for "equality", and how many decimal places to check. I converted both values to strings with the appropriate formatting specs, and compared the strings.

    Wow, now that's a hack. Wouldn't it be nicer to do something like this?

    bool AlmostEqual(double a, double b, int decimalPlaces)
    {
    double diff = 0f;
    for(int i=0; i

    Or would this be inaccurate again?

    J 1 Reply Last reply
    0
    • V Vikram A Punathambekar

      Jörgen Sigvardsson wrote:

      The most accurate way to present that number, is to present it as 10/3, or 3 1/3 if you wish.

      I learned that it can be written as 3.3. Was my teacher wrong? :suss:

      Cheers, Vikram.


      "whoever I am, I'm not other people" - Corinna John.

      J Offline
      J Offline
      Jorgen Sigvardsson
      wrote on last edited by
      #16

      No he wasn't wrong, but it's not a real number either, and not very intuitive for further computations. What is 0.36 times 33? Took you a while eh? What is 4/11 times 33? :)

      -- Hey, TiVo! Suggest this!

      A 1 Reply Last reply
      0
      • realJSOPR realJSOP

        Back in the day, I had to write a Pascal function called "AlmostEqual". I passed the values to be checked for "equality", and how many decimal places to check. I converted both values to strings with the appropriate formatting specs, and compared the strings. We needed different precisions depending on where we were in the math calculations. My suggestion is to NOT check doubles for equality without doing something along the same lines.

        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

        J Offline
        J Offline
        Jorgen Sigvardsson
        wrote on last edited by
        #17

        I often write functions like this:

        bool FuzzyEqual(double l, double r, double fuzz) {
        return abs(l - r) < fuzz;
        }

        Then I adjust the "fuzz" in accordance to the needed precision.

        -- Secreted by the Comedy Bee

        1 Reply Last reply
        0
        • E ensger

          Yesterday I questioned, if the possibility of 0 will mean impossible. I dont think so. If you have an infinite number of choices and take one, the possibility is 1 / infinite = 0 but not impossible. All of you said, that 1 / infinite is nearly 0, but not 0. That's astonishing me. So if 10 / 3 = Charly, and Charly * 3 = result, the result is nearly 10, but not 10???

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #18

          0 is not a number so dividing any number by 0 returns "not a number" in mathematical terms(abbr. NaN). Elaine :rose:

          The tigress is here :-D

          J 1 Reply Last reply
          0
          • L Lost User

            John Simmons / outlaw programmer wrote:

            I passed the values to be checked for "equality", and how many decimal places to check. I converted both values to strings with the appropriate formatting specs, and compared the strings.

            Wow, now that's a hack. Wouldn't it be nicer to do something like this?

            bool AlmostEqual(double a, double b, int decimalPlaces)
            {
            double diff = 0f;
            for(int i=0; i

            Or would this be inaccurate again?

            J Offline
            J Offline
            Jorgen Sigvardsson
            wrote on last edited by
            #19

            That's quite inefficient.. :~

            -- Featuring GRATUITOUS ALIEN NUDITY

            1 Reply Last reply
            0
            • L Lost User

              0 is not a number so dividing any number by 0 returns "not a number" in mathematical terms(abbr. NaN). Elaine :rose:

              The tigress is here :-D

              J Offline
              J Offline
              Jorgen Sigvardsson
              wrote on last edited by
              #20

              0 is not a number? I think you need to revisit and revise your post... ;)

              -- For External Use Only

              L 1 Reply Last reply
              0
              • J Jorgen Sigvardsson

                0 is not a number? I think you need to revisit and revise your post... ;)

                -- For External Use Only

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #21

                Technically, 0 is lack of a number. The romans didn't have 0 which limited them mathematically (it was actually invented in India and spread to Europe via Arab traders). Elaine :rose:

                The tigress is here :-D

                J 1 Reply Last reply
                0
                • L Lost User

                  Technically, 0 is lack of a number. The romans didn't have 0 which limited them mathematically (it was actually invented in India and spread to Europe via Arab traders). Elaine :rose:

                  The tigress is here :-D

                  J Offline
                  J Offline
                  Jorgen Sigvardsson
                  wrote on last edited by
                  #22

                  Just because it wasn't discovered until much later, doesn't mean it's not a number[^]. It may not be a natural number per se, but it is a consequence of natural numbers.

                  -- Pictures[^] from my Japan trip.

                  1 Reply Last reply
                  0
                  • J Jorgen Sigvardsson

                    No he wasn't wrong, but it's not a real number either, and not very intuitive for further computations. What is 0.36 times 33? Took you a while eh? What is 4/11 times 33? :)

                    -- Hey, TiVo! Suggest this!

                    A Offline
                    A Offline
                    Andy Brummer
                    wrote on last edited by
                    #23

                    Actually, in mathematics 0.3 is a real number. To a computer with limited precision it cannot be accurately represented, but in general infinitely repeating decimals are definitely real. Even things like pi and e are real numbers even though all their digits cannot be computed. That's what makes the set of real numbers bigger then the set of rational numbers.


                    and of course [they] outsource their technical support to a land where English bears little resemblance to the language I speak - Christopher Duncan

                    1 Reply Last reply
                    0
                    • V Vikram A Punathambekar

                      Jörgen Sigvardsson wrote:

                      The most accurate way to present that number, is to present it as 10/3, or 3 1/3 if you wish.

                      I learned that it can be written as 3.3. Was my teacher wrong? :suss:

                      Cheers, Vikram.


                      "whoever I am, I'm not other people" - Corinna John.

                      A Offline
                      A Offline
                      Andy Brummer
                      wrote on last edited by
                      #24

                      Your teacher was right. All decimals are real even infinitely repeating ones.


                      and of course [they] outsource their technical support to a land where English bears little resemblance to the language I speak - Christopher Duncan

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups