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. How hard would it be to have a Rational Number type, so that there would never be floating point errors?

How hard would it be to have a Rational Number type, so that there would never be floating point errors?

Scheduled Pinned Locked Moved The Lounge
c++question
33 Posts 19 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.
  • S Offline
    S Offline
    swampwiz
    wrote on last edited by
    #1

    It seems to me that there could be a type (implemented in your favorite language has has classes, including operator overloading) that would store a number as a rational number. This type would be a list of links that would have prime factors and their exponents (e.g., in C++, the prime factor would be type "unsigned long", with the exponent being "long"). This would work because any decimal (or hexadecimal) input would be a rational number itself, and then any operations would be exact, so that, e.g., the result of (213.56 - 45.41) would be 168.15 and not 168.14999999, etc.

    OriginalGriffO M G L P 14 Replies Last reply
    0
    • S swampwiz

      It seems to me that there could be a type (implemented in your favorite language has has classes, including operator overloading) that would store a number as a rational number. This type would be a list of links that would have prime factors and their exponents (e.g., in C++, the prime factor would be type "unsigned long", with the exponent being "long"). This would work because any decimal (or hexadecimal) input would be a rational number itself, and then any operations would be exact, so that, e.g., the result of (213.56 - 45.41) would be 168.15 and not 168.14999999, etc.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      And storage of Pi would be?

      Sent from my Amstrad PC 1640 Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      M P P K S 7 Replies Last reply
      0
      • S swampwiz

        It seems to me that there could be a type (implemented in your favorite language has has classes, including operator overloading) that would store a number as a rational number. This type would be a list of links that would have prime factors and their exponents (e.g., in C++, the prime factor would be type "unsigned long", with the exponent being "long"). This would work because any decimal (or hexadecimal) input would be a rational number itself, and then any operations would be exact, so that, e.g., the result of (213.56 - 45.41) would be 168.15 and not 168.14999999, etc.

        M Offline
        M Offline
        megaadam
        wrote on last edited by
        #3

        Most languages do have libraries for such types. But they would not solve irrationals such as pi or sqr(2).

        ... such stuff as dreams are made on

        1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          And storage of Pi would be?

          Sent from my Amstrad PC 1640 Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

          M Offline
          M Offline
          megaadam
          wrote on last edited by
          #4

          Again he beats me to it!

          ... such stuff as dreams are made on

          1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            And storage of Pi would be?

            Sent from my Amstrad PC 1640 Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            My stomach. I like pie.

            This space for rent

            OriginalGriffO 1 Reply Last reply
            0
            • P Pete OHanlon

              My stomach. I like pie.

              This space for rent

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              So do I! I have the last of my homemade Pork Pie for supper tonight with a salad (with ice cream and a fruit coulis to follow).

              Sent from my Amstrad PC 1640 Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              • S swampwiz

                It seems to me that there could be a type (implemented in your favorite language has has classes, including operator overloading) that would store a number as a rational number. This type would be a list of links that would have prime factors and their exponents (e.g., in C++, the prime factor would be type "unsigned long", with the exponent being "long"). This would work because any decimal (or hexadecimal) input would be a rational number itself, and then any operations would be exact, so that, e.g., the result of (213.56 - 45.41) would be 168.15 and not 168.14999999, etc.

                G Offline
                G Offline
                GuyThiebaut
                wrote on last edited by
                #7

                1/3 = ?

                “That which can be asserted without evidence, can be dismissed without evidence.”

                ― Christopher Hitchens

                K 1 Reply Last reply
                0
                • S swampwiz

                  It seems to me that there could be a type (implemented in your favorite language has has classes, including operator overloading) that would store a number as a rational number. This type would be a list of links that would have prime factors and their exponents (e.g., in C++, the prime factor would be type "unsigned long", with the exponent being "long"). This would work because any decimal (or hexadecimal) input would be a rational number itself, and then any operations would be exact, so that, e.g., the result of (213.56 - 45.41) would be 168.15 and not 168.14999999, etc.

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

                  You may also consider a [continued fraction representation](http://www.inwap.com/pdp10/hbaker/hakmem/cf.html) which can also be calculated with, and if you allow them to be "generators" they can even represent some annoying numbers such as e and phi and irrational square roots. But this also has various problems. For example, when subtracting "accidentally equal" numbers (ie two generators that are not the same instance and not binary equivalent, but generate the same list) it takes infinite steps to find out that there is never a difference, and this delays the generation of the "head" of the result generator infinitely long (ie there is no answer). Working with finite CFs necessarily means that some numbers cannot be represented any more since they get truncated at some point. By the way, floating point arithmetic is more exact than many people give it credit for. It doesn't *always* have to round, for example subtracting two numbers with equal sign that are within a factor of 2 of each other results in the *actual* difference between the two numbers (Sterbenz lemma). A large part of that perception is the great Decimal Bias of the modern world (at other times in history it would have been a Sexagesimal Bias), but there is nothing inherent about a base ten system.

                  1 Reply Last reply
                  0
                  • S swampwiz

                    It seems to me that there could be a type (implemented in your favorite language has has classes, including operator overloading) that would store a number as a rational number. This type would be a list of links that would have prime factors and their exponents (e.g., in C++, the prime factor would be type "unsigned long", with the exponent being "long"). This would work because any decimal (or hexadecimal) input would be a rational number itself, and then any operations would be exact, so that, e.g., the result of (213.56 - 45.41) would be 168.15 and not 168.14999999, etc.

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    Ask Python. PEP 239 -- Adding a Rational Type to Python | Python.org[^]

                    K 1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      And storage of Pi would be?

                      Sent from my Amstrad PC 1640 Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #10

                      Three significant digits should be enough for anybody.

                      OriginalGriffO 1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        Three significant digits should be enough for anybody.

                        OriginalGriffO Offline
                        OriginalGriffO Offline
                        OriginalGriff
                        wrote on last edited by
                        #11

                        Quote:

                        640K ought to be enough for anybody

                        O...Kay! :laugh:

                        Sent from my Amstrad PC 1640 Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                        1 Reply Last reply
                        0
                        • OriginalGriffO OriginalGriff

                          And storage of Pi would be?

                          Sent from my Amstrad PC 1640 Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                          K Offline
                          K Offline
                          kalberts
                          wrote on last edited by
                          #12

                          As a rational number? I believe 23/7 comes close, but if you want, you can find even closer rational numbers. pi isn't a rational number. With irrational numbers, there would be roundoff problems, although small ones. Most our everyday problems end up in rational numbers. It isn't difficult to implement a rational number type with modern OO languages.

                          1 Reply Last reply
                          0
                          • G GuyThiebaut

                            1/3 = ?

                            “That which can be asserted without evidence, can be dismissed without evidence.”

                            ― Christopher Hitchens

                            K Offline
                            K Offline
                            kalberts
                            wrote on last edited by
                            #13

                            A perfectly rational number.

                            1 Reply Last reply
                            0
                            • P PIEBALDconsult

                              Ask Python. PEP 239 -- Adding a Rational Type to Python | Python.org[^]

                              K Offline
                              K Offline
                              kalberts
                              wrote on last edited by
                              #14

                              It just gave me a hiss in return. Made no sense, except from making me scared.

                              1 Reply Last reply
                              0
                              • OriginalGriffO OriginalGriff

                                And storage of Pi would be?

                                Sent from my Amstrad PC 1640 Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                                S Offline
                                S Offline
                                swampwiz
                                wrote on last edited by
                                #15

                                I suppose that there could be some setting in this RationalNumber class that would allow for infinite computation for numbers like pi & e, if the client so desired. Pi could use Chudnovsky algorithm. e would be pretty straightforward too. And here's a quickie: 1833616417 / 583658233

                                K 1 Reply Last reply
                                0
                                • S swampwiz

                                  It seems to me that there could be a type (implemented in your favorite language has has classes, including operator overloading) that would store a number as a rational number. This type would be a list of links that would have prime factors and their exponents (e.g., in C++, the prime factor would be type "unsigned long", with the exponent being "long"). This would work because any decimal (or hexadecimal) input would be a rational number itself, and then any operations would be exact, so that, e.g., the result of (213.56 - 45.41) would be 168.15 and not 168.14999999, etc.

                                  S Offline
                                  S Offline
                                  Super Lloyd
                                  wrote on last edited by
                                  #16

                                  Well... you will quickly get overflow after some simple operations ( + - * / )... Not to mention some number can't be approximated by rational number. A large infinity of them!

                                  A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                                  1 Reply Last reply
                                  0
                                  • OriginalGriffO OriginalGriff

                                    And storage of Pi would be?

                                    Sent from my Amstrad PC 1640 Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                                    C Offline
                                    C Offline
                                    CPallini
                                    wrote on last edited by
                                    #17

                                    Don't be irrational, π would be automatically promoted to 'floating point error'.

                                    1 Reply Last reply
                                    0
                                    • S swampwiz

                                      It seems to me that there could be a type (implemented in your favorite language has has classes, including operator overloading) that would store a number as a rational number. This type would be a list of links that would have prime factors and their exponents (e.g., in C++, the prime factor would be type "unsigned long", with the exponent being "long"). This would work because any decimal (or hexadecimal) input would be a rational number itself, and then any operations would be exact, so that, e.g., the result of (213.56 - 45.41) would be 168.15 and not 168.14999999, etc.

                                      J Offline
                                      J Offline
                                      John Stewien
                                      wrote on last edited by
                                      #18

                                      You mean like this BigRational class in C# [SwordfishCollections/BigRational.cs at master · stewienj/SwordfishCollections · GitHub](https://github.com/stewienj/SwordfishCollections/blob/master/Swordfish.NET/General/BigRational.cs)

                                      1 Reply Last reply
                                      0
                                      • S swampwiz

                                        It seems to me that there could be a type (implemented in your favorite language has has classes, including operator overloading) that would store a number as a rational number. This type would be a list of links that would have prime factors and their exponents (e.g., in C++, the prime factor would be type "unsigned long", with the exponent being "long"). This would work because any decimal (or hexadecimal) input would be a rational number itself, and then any operations would be exact, so that, e.g., the result of (213.56 - 45.41) would be 168.15 and not 168.14999999, etc.

                                        G Offline
                                        G Offline
                                        gdemont
                                        wrote on last edited by
                                        #19

                                        Already done : check Rationals @ http://mathpaqs.sourceforge.net/. It's even generic, so you can use whatever type for a,b in the fractions a/b: integers, big integers, polynomials, ...

                                        K S 2 Replies Last reply
                                        0
                                        • S swampwiz

                                          I suppose that there could be some setting in this RationalNumber class that would allow for infinite computation for numbers like pi & e, if the client so desired. Pi could use Chudnovsky algorithm. e would be pretty straightforward too. And here's a quickie: 1833616417 / 583658233

                                          K Offline
                                          K Offline
                                          kalberts
                                          wrote on last edited by
                                          #20

                                          That wouldn't make pi a rational number, though. It would be misleading to provide pi in a class named something like rational_number - unless you called it not pi, but pi_approximated_as_a_rational_number. A (true) story about pi, I believe it is from around 1980 - I was told the story around 1983 by a guy who had participated in the hunt for The True pi: At Bergen University, Norway, one professor teaching numerical methods and error propagation had his students estimate the error expected in some trancendental functions in difficult number ranges, and verify it on the University's new shining IBM 3080 mainframe. The students came back and reported significantly larger errors than their estimates suggested. The surprised professor set out to find the cause of this. It turned out that the IBM 3080 Fortran libraries were carbon copies of the 370 libraries. Which were carbon copies of 360 libraries. The 360 got its libraries (in assembler format, of course, with floating point constants in hexadecimal format) as an adaptation of the old 7090 libraries - machines with a different instruction set and 36 bit word length (rather the 360's 32 bits). Calculating a binary representation of pi anew would have had to be set up as a separate job. They didn't do that; they just chopped 4 bits off the 7090 binary floating point mantissa for the pi value. Ignoring rounding. So the least significant bit, which should have been rounded up to a 1, remained a 0. The professor's theoretical error estimates were based on a properly rounded, not a truncated pi value. This truncated 7090-binary pi value from the end of the 1950s was interited all the way up to the 3080 series, more than 20 years later. When discovered, and the least significant bit rounded up to a 1, the theoretical error estimates matched the observed errors more or less perfectly.

                                          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