Not even simple rational numbers like ⅓ which can neither be precisely represented in decimal nor binary. Unless the denominator's prime factors are 2ⁱ × 5ⁿ and no other factors, you will be faced with an infinite regression. I presume it is clear to readers why prime factors 2 and 5 are all right?
Paul Clapham wrote:. . . most decimal numbers don't correspond neatly to a simple fraction. . . .
Randall Twede wrote:going from fraction to decimal is easy, you just divide. to go from decimal to fraction is a bit harder.
the algorithm i came up with for that is you multiply and if the result is an integer, that is the numerator and what you were dividing by is the denominator.
you loop through integers to find the denominator.
for example 1/4 = .25
you multiply .25 X 2 = .5 = no
.25 X 3 = .75 = no
.25 X 4 = 1 = yes
so .25 = 1/4
this works except for repeating numbers like .333333333333333333333
is there a way to make my algorithm work?
Randall Twede wrote:going from fraction to decimal is easy, you just divide
Stephan van Hulst wrote:Think about how difficult converting a fractional notation to a decimal notation would be if you were only allowed to use integer division and not real division.
Piet Souris wrote:But here is a link to a Dutch page, but I'm sure you recognize the 'staartdelingen'. The method has changed since I went to primary, though....
staartdelingen - see examples 4 and further
Well, they do say that your schooldays are the happiest days of your life
Piet Souris wrote:. . . getting emotional about it... sniff . . .
Ryan McGuire wrote:
If you need to report out a fraction at the end of a calculation, could you just do the whole calculation in fractions instead of using doubles at all?
We called that cancelling out when I was at school.
Ryan McGuire wrote:. . .factor out any powers of 2 and/or 5 (up to N). . . .
You can easily tell the numerator and denominator are co‑prime because the denominator has 2 and 5 as its prime factors, well actually 2¹² and 5¹¹. Since the numerator doesn't end in an even digit nor 5, you can tell it has been reduced so it doesn't have 2 or 5 as a factor.
147619047619/200000000000 Final answer. . . .
(1/14 + 2/3) * 5/32 = 0.738095238095