Fraction Problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bluesteel
    New Member
    • Mar 2007
    • 84

    Fraction Problems

    Hi guys, I am currently developing a math program in which i need the program to interpret, for instance, 1/3 like 0.3333... instead of a limited number of numbers after the comma. I thought it could be done by saying it has no "exact" result and storing the two integers of the fraction to get a better accuracy, but i wanted to know whether you had a better idea or maybe you could help doing this. I have another question: if i divide 1 by 3, how many numbers does the computer use to make the division?? (=how many numbers after the comma)
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    Originally posted by bluesteel
    Hi guys, I am currently developing a math program in which i need the program to interpret, for instance, 1/3 like 0.3333... instead of a limited number of numbers after the comma. I thought it could be done by saying it has no "exact" result and storing the two integers of the fraction to get a better accuracy, but i wanted to know whether you had a better idea or maybe you could help doing this. I have another question: if i divide 1 by 3, how many numbers does the computer use to make the division?? (=how many numbers after the comma)
    "better accuracy" is a relative term. What is the difference between .333333333333 and .3333333333333? Virtually nothing, and any calculation you do will be significant to 12 digits. If you want to increase accuracy use a larger data type or you can write a program that estimates a fraction based on a decimal value. The way to estimate a fraction based on a decimal is you take a value like .3333333333 and turn it into 3333333333 / 10000000000 then reduce the fraction.

    In answer to your other question the amount of numbers the computer uses to make the division depends on what your system is set up to be. Most floats are 32 bits, but they can be larger or smaller.

    Comment

    • bluesteel
      New Member
      • Mar 2007
      • 84

      #3
      Originally posted by RedSon
      "better accuracy" is a relative term. What is the difference between .333333333333 and .3333333333333? Virtually nothing, and any calculation you do will be significant to 12 digits. If you want to increase accuracy use a larger data type or you can write a program that estimates a fraction based on a decimal value. The way to estimate a fraction based on a decimal is you take a value like .3333333333 and turn it into 3333333333 / 10000000000 then reduce the fraction.

      In answer to your other question the amount of numbers the computer uses to make the division depends on what your system is set up to be. Most floats are 32 bits, but they can be larger or smaller.

      The thing is that i need the computer to compare numbers and, for instance, if it is 0.9999999999999 99999999999 it will say it's not equal to 1

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        It depends on how accurate your application needs to be. If you are looking for infinite precision then there are libraries available for this. In this library there should be methods for compare and other useful operations.

        If you are looking for something that is only accurate to 20 digits or so then make it so when you do a compare you test the very last digit.

        Comment

        • bluesteel
          New Member
          • Mar 2007
          • 84

          #5
          Originally posted by RedSon
          It depends on how accurate your application needs to be. If you are looking for infinite precision then there are libraries available for this. In this library there should be methods for compare and other useful operations.

          If you are looking for something that is only accurate to 20 digits or so then make it so when you do a compare you test the very last digit.
          Then I'm interested in knowing which those libraries are...!

          Comment

          • RedSon
            Recognized Expert Expert
            • Jan 2007
            • 4980

            #6
            Then google is your best friend. They are not a part of a standard package, you will have to locate a library that will work. You can search on MSDN or on google labs or on sourceforge, those would be my first stops. Also try "infinite precision library"

            Comment

            • AdrianH
              Recognized Expert Top Contributor
              • Feb 2007
              • 1251

              #7
              Originally posted by bluesteel
              The thing is that i need the computer to compare numbers and, for instance, if it is 0.9999999999999 99999999999 it will say it's not equal to 1
              This is because of round off error (well sort of), if you actually have 0.999... on to infinitly small, it actually is equal to 1. When comparing floats, doubles, you should compare within a margin of error:

              target - somePercision < x && x < target + somePercision

              Where x is the value you want to check, target is the value you want to check against and somePercision is the percision that you want it to be accurate to. I'd probably write a function for this.

              Hope this helps.


              Adrian

              Comment

              • RedSon
                Recognized Expert Expert
                • Jan 2007
                • 4980

                #8
                Originally posted by AdrianH
                This is because of round off error (well sort of), if you actually have 0.999... on to infinitly small, it actually is equal to 1. When comparing floats, doubles, you should compare within a margin of error:

                target - somePercision < x && x < target + somePercision

                Where x is the value you want to check, target is the value you want to check against and somePercision is the percision that you want it to be accurate to. I'd probably write a function for this.

                Hope this helps.


                Adrian
                Thats actually true, from a purely mathematical standpoint .9999999... is actually equal to 1. It can be proven and has been. So the truth of the matter is .99999... to infinite precision is == to 1.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  w.r.t. those fractions: if a fraction 0.ddddddd ... contains a finite number of
                  decimals d then the simplified fraction itself can be found as follows:

                  0.ddddd = ddddd/10^n = (ddddd/gcd(ddddd, 10^n))/(10^n/gcd(ddddd, 10^n))
                  where n is the length of the finite decimal expansion and gcd(x, y) is the
                  greatest common divisor of x and y.

                  if the decimal expansion contains a repeating subgroup. e.g. as in
                  0.1428571428571 42857 ... the simplified fraction can be found as
                  ddddd/(10^n-1) where ddddd is the repeating subgroup of length n. The
                  simplification can be found as in step 1 (see above). For the example,
                  the fraction will be 142857/99999 == 1/7

                  As a corollary: 0.99999999 ... is the fraction 9/(10^1-1) == 9/9 == 1

                  kind regards,

                  Jos

                  Comment

                  • bluesteel
                    New Member
                    • Mar 2007
                    • 84

                    #10
                    Originally posted by JosAH
                    w.r.t. those fractions: if a fraction 0.ddddddd ... contains a finite number of
                    decimals d then the simplified fraction itself can be found as follows:

                    0.ddddd = ddddd/10^n = (ddddd/gcd(ddddd, 10^n))/(10^n/gcd(ddddd, 10^n))
                    where n is the length of the finite decimal expansion and gcd(x, y) is the
                    greatest common divisor of x and y.

                    if the decimal expansion contains a repeating subgroup. e.g. as in
                    0.1428571428571 42857 ... the simplified fraction can be found as
                    ddddd/(10^n-1) where ddddd is the repeating subgroup of length n. The
                    simplification can be found as in step 1 (see above). For the example,
                    the fraction will be 142857/99999 == 1/7

                    As a corollary: 0.99999999 ... is the fraction 9/(10^1-1) == 9/9 == 1

                    kind regards,

                    Jos
                    Actually, i thought it could be much easier, but anyway i still have a problem, i might have 0.999999999 but it might be 0.999999999 or it could be a problem and it returned 0.999999999 insted of 1

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by bluesteel
                      Actually, i thought it could be much easier, but anyway i still have a problem, i might have 0.999999999 but it might be 0.999999999 or it could be a problem and it returned 0.999999999 insted of 1
                      Welcome to the wonderful world of floating point numbers using a finite number
                      of bits. Here's a nice link that explains it all: Floating point stuff

                      kind regards,

                      Jos

                      Comment

                      Working...