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)
Fraction Problems
Collapse
X
-
Originally posted by bluesteelHi 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)
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. -
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 1Comment
-
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
-
Originally posted by RedSonIt 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
-
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
-
Originally posted by bluesteelThe 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
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.
AdrianComment
-
Originally posted by AdrianHThis 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.
AdrianComment
-
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,
JosComment
-
Originally posted by JosAHw.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,
JosComment
-
Originally posted by bluesteelActually, 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
of bits. Here's a nice link that explains it all: Floating point stuff
kind regards,
JosComment
Comment