473,320 Members | 1,988 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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)
Mar 26 '07 #1
10 1730
RedSon
5,000 Expert 4TB
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.
Mar 26 '07 #2
"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.999999999999999999999999 it will say it's not equal to 1
Mar 26 '07 #3
RedSon
5,000 Expert 4TB
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.
Mar 26 '07 #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.
Then I'm interested in knowing which those libraries are...!
Mar 26 '07 #5
RedSon
5,000 Expert 4TB
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"
Mar 26 '07 #6
AdrianH
1,251 Expert 1GB
The thing is that i need the computer to compare numbers and, for instance, if it is 0.999999999999999999999999 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
Mar 27 '07 #7
RedSon
5,000 Expert 4TB
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.
Mar 27 '07 #8
JosAH
11,448 Expert 8TB
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.142857142857142857 ... 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
Mar 28 '07 #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.142857142857142857 ... 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
Mar 28 '07 #10
JosAH
11,448 Expert 8TB
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
Mar 28 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Steve | last post by:
I am having trouble finding the answer to this question, I'm thinking the solution must be blindingly obvious, so therefore of course I cannot see it. I wish to take a fraction in DEC say .4 and...
0
by: amarok | last post by:
Hello all. I'm a Software Engineering student, and I'm attempting to write a program in Java that does as follows: UML for the class: Fraction() Fraction(numerator: int) ...
6
evilmonkey
by: evilmonkey | last post by:
I am very new to programming as well as Java and this is my first post so please forgive me if this is not quite posted correctly. My Problem is that I have only been using scanner to get user input...
7
by: d0ugg | last post by:
Hi, Iam writing a code for my class and I cant figure out how to solve it. I will be glad if somebody can teach me how to do something to correct it. The program is divided it 3 parts. ...
1
by: d0ugg | last post by:
Hi, I'm did a fraction program for one of my programming classes and it did compile, however when I'm running the program it crashes for some reason that I do not know. // fraction.cpp ...
2
by: d0ugg | last post by:
Hi, I'm doing a FRACTION program for one of my Programming classes and I'm getting some errors that I can't figure it out. Here is the Assignment: 1. Convert the fraction structure into a...
4
by: d0ugg | last post by:
Hello everyone, I'm creating a program that it is suppose to add, subtract, multiply and also divide fractions and after that, the result has to be reduced to the lowest terms. However, I'm not...
4
by: Semajthewise | last post by:
Hi All For those of you that helped me with the questions on this... Thank you! I believe I have solved all the bugs in the code and wanted to post the final product. This is a set of code to solve...
135
by: robinsiebler | last post by:
I've never had any call to use floating point numbers and now that I want to, I can't! *** Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) on win32. *** 0.29999999999999999 0.29999999999999999
3
by: Myxamatosis | last post by:
we're given an implementation file to base our class of Fraction off of. Input and output are in the format x/y, with x and y being ints, with the "/" character in the middle. so to create an...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.