hello everyone, I don't know to do this myself, so I rather use a library, I need to convert decimal numbers to fractions (closest possible fraction in lowest terms), can anyone point me a direction?
thank you!
Most "decimal" numbers would be stored as floating-point doubles, known only to a finite accuracy because of binary representation. So you would have to enter the number as a string.
- Acting on the string representation, separate off the parts before and after the decimal point.
- Take note of, then strip off, the sign.
- Convert the whole number and remainder separately to integers and, from the fractional part (giving the numerator) calculate the denominator as the appropriate power of 10.
- Dealing with the fractional part divide numerator and denominator by their highest common factor.
- Adjust the numerator for any whole number component.
- Put the sign back in the numerator if the original number was negative
"The library does not offer a conversion function from floating point to rational. A number of requests were received for such a conversion, but extensive discussions on the boost list reached the conclusion that there was no "best solution" to the problem. As there is no reason why a user of the library cannot write their own conversion function which suits their particular requirements, the decision was taken not to pick any one algorithm as "standard". "
seems like boost don't manage to do it.
This is a bit "fragile" (don't try too many "ill-formed" strings, cases where numerator or denominator go out of range of an integer, anything involving scientific notation). It would be made much more complicated to have to cover each of those. To do: put in proper error-checking.
I didn't code yet, I try to understand first what I'm doing, so far I don't know really what to do, I just know that if its repeating then I take the repeating numbers and divide by 999... n 9's as the repeating part, and then reduce to lowset terms
For a homework or even simple usage all you need to do is put reasonable limits on the patterns.
Ah, yes.
Boost didn't want to do it because there are extremely obscure technical issues that arise in important corner cases, and the way it is done has an impact on its usability in any given application. Hence, Boost would rather you write the code to satisfy your requirements.
/shrug for practical purposes, cant you just craft something/pow(10, something)?
eg even a repeating thing like
1.234234234234 is 1234234234234 /1000000000000 (if I counted the zeros right, not checking it but you get the point). then just reduce the above with GCD calls or something if you need it reduced. Is that good enough for what you wanted?
I mean its provable in math (see, irrational numbers) that not everything can be written exactly as a fraction. So knowing that going into this, you have to make some decisions on how much trouble you want to go into.... the above will get you a long, long way for homework type problems.
you don't have to do anything for repeating decimals. Just run the precision as far as you want, or as far as double supports, and use this approximate value. Repeating numbers are not irrational, but finding the exact fraction is .... extremely difficult. Finding an approximate fraction should be good enough.
Floating point numbers are an approximation themselves, so the question comes down to, how much effort do you want to put into precision?
Even if you are computing parallax to stars you can get away with six or seven decimal places.
The ratios that will give you the most grief are those involving primes or values very close to primes. But again, this is just part of the precision issue you have to accept as a reality of rational numbers systems.