hi there,
Is there an external that converts decimal numbers to fractions, like 1.5 => 3 / 2 ?
I bet it's complicated to do it as a vanilla patch, right?
thanks alex
i had a go at it. This one converts up to 5 decimal places (you could probably mod it to do more, but then you get pretty close to the limits of float-based arithmetic, i think).
i was getting glitches, because of the known issue where 58/1000 comes out as 57.99999.. , so i have just added a tiny amount to each number to stop this.
also, the one i have done here splits a number into whole and fractional parts, but it would be trivial to multiply the whole by the denominator to express the number as a vulgar fraction (3/2, etc)
On Fri, Dec 16, 2011 at 11:52 AM, Alexandre Torres Porres porres@gmail.comwrote:
hi there,
Is there an external that converts decimal numbers to fractions, like 1.5 => 3 / 2 ?
I bet it's complicated to do it as a vanilla patch, right?
thanks alex
Pd-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list
by the way, here is the method i used:
first, convert the decimal part to a fraction in the form of n/100000 next, find the highest common factor of n and 100000 (using the 'division method' like this: http://easycalculation.com/what-is-hcf.php )
then just divide n and 100000 by that factor.
actually, that means it's accurate to 6 decimal places, i guess. well...whatever :D
On 16/12/11 06:51, i go bananas wrote:
by the way, here is the method i used:
first, convert the decimal part to a fraction in the form of n/100000 next, find the highest common factor of n and 100000 (using the 'division method' like this: http://easycalculation.com/what-is-hcf.php )
then just divide n and 100000 by that factor.
I don't think that method will give happy results for most simple fractions. Plus it's useful to get approximations that are simpler or more accurate, like 3 or 22/7 or 355/113 for pi..
Your patch doesn't work very well for me:
input: 1/7 fraction: 2857/20000 input: 8/9 fraction: 11111/12500 input: 7/11 fraction: 15909/25000 input: 11/17 fraction: 4313.67/6666.67
(input is "$1 $2"--[/], so as accurate as floating point is...)
actually, that means it's accurate to 6 decimal places, i guess.
There's a way to get a "simple" fraction like 1/7 instead of 143/1000 or whatever, could be possible to implement in Pd? (I've not tried.)
[0] http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/Data-Ra...
[1] http://en.wikipedia.org/wiki/Continued_fraction#Best_rational_approximations
well...whatever :D
Claude
yeah, my patch only works for rational numbers.
will have a look at the article / method you posted, claude.
On Fri, Dec 16, 2011 at 7:49 PM, Claude Heiland-Allen claude@goto10.orgwrote:
On 16/12/11 06:51, i go bananas wrote:
by the way, here is the method i used:
first, convert the decimal part to a fraction in the form of n/100000 next, find the highest common factor of n and 100000 (using the 'division method' like this: http://easycalculation.com/**what-is-hcf.phphttp://easycalculation.com/what-is-hcf.php)
then just divide n and 100000 by that factor.
I don't think that method will give happy results for most simple fractions. Plus it's useful to get approximations that are simpler or more accurate, like 3 or 22/7 or 355/113 for pi..
Your patch doesn't work very well for me:
input: 1/7 fraction: 2857/20000 input: 8/9 fraction: 11111/12500 input: 7/11 fraction: 15909/25000 input: 11/17 fraction: 4313.67/6666.67
(input is "$1 $2"--[/], so as accurate as floating point is...)
actually, that means it's accurate to 6 decimal places, i guess.
There's a way to get a "simple" fraction like 1/7 instead of 143/1000 or whatever, could be possible to implement in Pd? (I've not tried.)
[0] http://hackage.haskell.org/**packages/archive/base/latest/** doc/html/src/Data-Ratio.html#**approxRationalhttp://hackage.haskell.org/packages/archive/base/latest/doc/html/src/Data-Ratio.html#approxRational
[1] http://en.wikipedia.org/wiki/**Continued_fraction#Best_** rational_approximationshttp://en.wikipedia.org/wiki/Continued_fraction#Best_rational_approximations
well...whatever :D
Claude
______________________________**_________________ Pd-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/** listinfo/pd-list http://lists.puredata.info/listinfo/pd-list
i had a go at it
thanks, I kinda had to go too, but no time... :(
yeah, my patch only works for rational numbers.
you know what, I think I asked this before on this list,
deja'vu
will have a look at the article / method you posted, claude.
are you going at it too? :)
by the way, I meant something like 1.75 becomes 7/4 and not 3/4, but that is easy to adapt on your patch
thanks
cheers
2011/12/16 i go bananas hard.off@gmail.com
by the way, here is the method i used:
first, convert the decimal part to a fraction in the form of n/100000 next, find the highest common factor of n and 100000 (using the 'division method' like this: http://easycalculation.com/what-is-hcf.php )
then just divide n and 100000 by that factor.
actually, that means it's accurate to 6 decimal places, i guess. well...whatever :D
actually, i'm not going to do anything more on this.
i had a look at the articles claude posted, and they went a bit far over my head.
my patch will still work for basic things like 1/4 and 7/8, but i wouldn't depend on it working for a serious application. As you first suggested, it's not so simple, and if you read claude's articles, you will see that it isn't.
it's not brain science though, so maybe someone with a bit more number understanding can tackle it.
On Sat, Dec 17, 2011 at 12:51 AM, Alexandre Torres Porres porres@gmail.comwrote:
i had a go at it
thanks, I kinda had to go too, but no time... :(
yeah, my patch only works for rational numbers.
you know what, I think I asked this before on this list,
deja'vu
will have a look at the article / method you posted, claude.
are you going at it too? :)
by the way, I meant something like 1.75 becomes 7/4 and not 3/4, but that is easy to adapt on your patch
thanks
cheers
2011/12/16 i go bananas hard.off@gmail.com
by the way, here is the method i used:
first, convert the decimal part to a fraction in the form of n/100000 next, find the highest common factor of n and 100000 (using the 'division method' like this: http://easycalculation.com/what-is-hcf.php )
then just divide n and 100000 by that factor.
actually, that means it's accurate to 6 decimal places, i guess. well...whatever :D
looks like a job for an external
2011/12/16 i go bananas hard.off@gmail.com
actually, i'm not going to do anything more on this.
i had a look at the articles claude posted, and they went a bit far over my head.
my patch will still work for basic things like 1/4 and 7/8, but i wouldn't depend on it working for a serious application. As you first suggested, it's not so simple, and if you read claude's articles, you will see that it isn't.
it's not brain science though, so maybe someone with a bit more number understanding can tackle it.
On Sat, Dec 17, 2011 at 12:51 AM, Alexandre Torres Porres < porres@gmail.com> wrote:
i had a go at it
thanks, I kinda had to go too, but no time... :(
yeah, my patch only works for rational numbers.
you know what, I think I asked this before on this list,
deja'vu
will have a look at the article / method you posted, claude.
are you going at it too? :)
by the way, I meant something like 1.75 becomes 7/4 and not 3/4, but that is easy to adapt on your patch
thanks
cheers
2011/12/16 i go bananas hard.off@gmail.com
by the way, here is the method i used:
first, convert the decimal part to a fraction in the form of n/100000 next, find the highest common factor of n and 100000 (using the 'division method' like this: http://easycalculation.com/what-is-hcf.php )
then just divide n and 100000 by that factor.
actually, that means it's accurate to 6 decimal places, i guess. well...whatever :D
it's possible in vanilla, but the mathematics goes over my head
On Sat, Dec 17, 2011 at 1:05 AM, Alexandre Torres Porres porres@gmail.comwrote:
looks like a job for an external
2011/12/16 i go bananas hard.off@gmail.com
actually, i'm not going to do anything more on this.
i had a look at the articles claude posted, and they went a bit far over my head.
my patch will still work for basic things like 1/4 and 7/8, but i wouldn't depend on it working for a serious application. As you first suggested, it's not so simple, and if you read claude's articles, you will see that it isn't.
it's not brain science though, so maybe someone with a bit more number understanding can tackle it.
On Sat, Dec 17, 2011 at 12:51 AM, Alexandre Torres Porres < porres@gmail.com> wrote:
i had a go at it
thanks, I kinda had to go too, but no time... :(
yeah, my patch only works for rational numbers.
you know what, I think I asked this before on this list,
deja'vu
will have a look at the article / method you posted, claude.
are you going at it too? :)
by the way, I meant something like 1.75 becomes 7/4 and not 3/4, but that is easy to adapt on your patch
thanks
cheers
2011/12/16 i go bananas hard.off@gmail.com
by the way, here is the method i used:
first, convert the decimal part to a fraction in the form of n/100000 next, find the highest common factor of n and 100000 (using the 'division method' like this: http://easycalculation.com/what-is-hcf.php )
then just divide n and 100000 by that factor.
actually, that means it's accurate to 6 decimal places, i guess. well...whatever :D
online calculator http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/cfCALC.html
yes, but it's difficult to embed that in a pd patch, yeah?
On Sat, Dec 17, 2011 at 4:05 AM, Alexandre Torres Porres porres@gmail.comwrote:
online calculator http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/cfCALC.html
On 16/12/11 16:05, Alexandre Torres Porres wrote:
looks like a job for an external
Not really answering the OP question but something could be done in Python:
def find_frac(num): f = float(num) last_error = 1000 best = (0,0) for i in xrange(1,1001): for j in xrange(1,i+1): divide = (float(i) / float (j)) if divide == f: return ((i,j),0) err = abs(divide - f) if err < last_error: best = (i,j) last_error = err return (best,last_error)
This would try to find the exact fraction or the one with the smallest error (trying up to 1000/1000). It would return (numerator, denominator, error). Guess it would work well at least up to 100 but only for positive numbers... and... not for numbers < 1.. and surely it's not optimised etc. etc. :)
find_frac(2)
((2, 1), 0)
find_frac(1.5)
((3, 2), 0)
find_frac(1.333333333333333333333333333)
((4, 3), 0)
find_frac(2.4)
((12, 5), 0)
find_frac(2.8)
((14, 5), 0)
find_frac(2.987654321)
((242, 81), 1.234568003383174e-11)
find_frac(50.32)
((956, 19), 0.004210526315787888)
find_frac(50.322)
((956, 19), 0.006210526315790332)
find_frac(50.4)
((252, 5), 0)
find_frac(10.33)
((971, 94), 0.00021276595744623705)
find_frac(10.33333333333333333333333333)
((31, 3), 0)
Lorenzo.
2011/12/16 i go bananas <hard.off@gmail.com mailto:hard.off@gmail.com>
actually, i'm not going to do anything more on this. i had a look at the articles claude posted, and they went a bit far over my head. my patch will still work for basic things like 1/4 and 7/8, but i wouldn't depend on it working for a serious application. As you first suggested, it's not so simple, and if you read claude's articles, you will see that it isn't. it's not brain science though, so maybe someone with a bit more number understanding can tackle it. On Sat, Dec 17, 2011 at 12:51 AM, Alexandre Torres Porres <porres@gmail.com <mailto:porres@gmail.com>> wrote: > i had a go at it thanks, I kinda had to go too, but no time... :( > yeah, my patch only works for rational numbers. you know what, I think I asked this before on this list, deja'vu > will have a look at the article / method you posted, claude. are you going at it too? :) by the way, I meant something like 1.75 becomes 7/4 and not 3/4, but that is easy to adapt on your patch thanks cheers 2011/12/16 i go bananas <hard.off@gmail.com <mailto:hard.off@gmail.com>> by the way, here is the method i used: first, convert the decimal part to a fraction in the form of n/100000 next, find the highest common factor of n and 100000 (using the 'division method' like this: http://easycalculation.com/what-is-hcf.php ) then just divide n and 100000 by that factor. actually, that means it's accurate to 6 decimal places, i guess. well...whatever :D
Pd-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list
If you guys 'd done your math, you'd know there is an ancient algorithm for approximating numbers by fractions and its called continued fractions.
On 16 December 2011 18:38, Lorenzo Sutton lorenzofsutton@gmail.com wrote:
On 16/12/11 16:05, Alexandre Torres Porres wrote:
looks like a job for an external
Not really answering the OP question but something could be done in Python:
def find_frac(num): f = float(num) last_error = 1000 best = (0,0) for i in xrange(1,1001): for j in xrange(1,i+1): divide = (float(i) / float (j)) if divide == f: return ((i,j),0) err = abs(divide - f) if err < last_error: best = (i,j) last_error = err return (best,last_error)
This would try to find the exact fraction or the one with the smallest error (trying up to 1000/1000). It would return (numerator, denominator, error). Guess it would work well at least up to 100 but only for positive numbers... and... not for numbers < 1.. and surely it's not optimised etc. etc. :)
find_frac(2)
((2, 1), 0)
find_frac(1.5)
((3, 2), 0)
find_frac(1.**333333333333333333333333333)
((4, 3), 0)
find_frac(2.4)
((12, 5), 0)
find_frac(2.8)
((14, 5), 0)
find_frac(2.987654321)
((242, 81), 1.234568003383174e-11)
find_frac(50.32)
((956, 19), 0.004210526315787888)
find_frac(50.322)
((956, 19), 0.006210526315790332)
find_frac(50.4)
((252, 5), 0)
find_frac(10.33)
((971, 94), 0.00021276595744623705)
find_frac(10.**33333333333333333333333333)
((31, 3), 0)
Lorenzo.
2011/12/16 i go bananas <hard.off@gmail.com mailto:hard.off@gmail.com>
actually, i'm not going to do anything more on this.
i had a look at the articles claude posted, and they went a bit far over my head.
my patch will still work for basic things like 1/4 and 7/8, but i wouldn't depend on it working for a serious application. As you first suggested, it's not so simple, and if you read claude's articles, you will see that it isn't.
it's not brain science though, so maybe someone with a bit more number understanding can tackle it.
On Sat, Dec 17, 2011 at 12:51 AM, Alexandre Torres Porres <porres@gmail.com mailto:porres@gmail.com> wrote:
> i had a go at it thanks, I kinda had to go too, but no time... :( > yeah, my patch only works for rational numbers. you know what, I think I asked this before on this list, deja'vu > will have a look at the article / method you posted, claude. are you going at it too? :) by the way, I meant something like 1.75 becomes 7/4 and not 3/4, but that is easy to adapt on your patch thanks cheers 2011/12/16 i go bananas <hard.off@gmail.com <mailto:hard.off@gmail.com>> by the way, here is the method i used: first, convert the decimal part to a fraction in the form of n/100000 next, find the highest common factor of n and 100000 (using the 'division method' like this: http://easycalculation.com/**what-is-hcf.php<http://easycalculation.com/what-is-hcf.php>) then just divide n and 100000 by that factor. actually, that means it's accurate to 6 decimal places, i guess. well...whatever :D
______________________________**_________________ Pd-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/** listinfo/pd-list http://lists.puredata.info/listinfo/pd-list
______________________________**_________________ Pd-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/** listinfo/pd-list http://lists.puredata.info/listinfo/pd-list
if you had read the thread, you would have seen that claude posted a link to that technique.
now go and make a PD patch that does it, mr smart guy.
On Sat, Dec 17, 2011 at 3:00 AM, Ludwig Maes ludwig.maes@gmail.com wrote:
If you guys 'd done your math, you'd know there is an ancient algorithm for approximating numbers by fractions and its called continued fractions.
On 16 December 2011 18:38, Lorenzo Sutton lorenzofsutton@gmail.comwrote:
On 16/12/11 16:05, Alexandre Torres Porres wrote:
looks like a job for an external
Not really answering the OP question but something could be done in Python:
def find_frac(num): f = float(num) last_error = 1000 best = (0,0) for i in xrange(1,1001): for j in xrange(1,i+1): divide = (float(i) / float (j)) if divide == f: return ((i,j),0) err = abs(divide - f) if err < last_error: best = (i,j) last_error = err return (best,last_error)
This would try to find the exact fraction or the one with the smallest error (trying up to 1000/1000). It would return (numerator, denominator, error). Guess it would work well at least up to 100 but only for positive numbers... and... not for numbers < 1.. and surely it's not optimised etc. etc. :)
find_frac(2)
((2, 1), 0)
find_frac(1.5)
((3, 2), 0)
find_frac(1.**333333333333333333333333333)
((4, 3), 0)
find_frac(2.4)
((12, 5), 0)
find_frac(2.8)
((14, 5), 0)
find_frac(2.987654321)
((242, 81), 1.234568003383174e-11)
find_frac(50.32)
((956, 19), 0.004210526315787888)
find_frac(50.322)
((956, 19), 0.006210526315790332)
find_frac(50.4)
((252, 5), 0)
find_frac(10.33)
((971, 94), 0.00021276595744623705)
find_frac(10.**33333333333333333333333333)
((31, 3), 0)
Lorenzo.
2011/12/16 i go bananas <hard.off@gmail.com mailto:hard.off@gmail.com>
actually, i'm not going to do anything more on this.
i had a look at the articles claude posted, and they went a bit far over my head.
my patch will still work for basic things like 1/4 and 7/8, but i wouldn't depend on it working for a serious application. As you first suggested, it's not so simple, and if you read claude's articles, you will see that it isn't.
it's not brain science though, so maybe someone with a bit more number understanding can tackle it.
On Sat, Dec 17, 2011 at 12:51 AM, Alexandre Torres Porres <porres@gmail.com mailto:porres@gmail.com> wrote:
> i had a go at it thanks, I kinda had to go too, but no time... :( > yeah, my patch only works for rational numbers. you know what, I think I asked this before on this list, deja'vu > will have a look at the article / method you posted, claude. are you going at it too? :) by the way, I meant something like 1.75 becomes 7/4 and not 3/4, but that is easy to adapt on your patch thanks cheers 2011/12/16 i go bananas <hard.off@gmail.com <mailto:hard.off@gmail.com>> by the way, here is the method i used: first, convert the decimal part to a fraction in the form of n/100000 next, find the highest common factor of n and 100000 (using the 'division method' like this: http://easycalculation.com/**what-is-hcf.php<http://easycalculation.com/what-is-hcf.php>) then just divide n and 100000 by that factor. actually, that means it's accurate to 6 decimal places, i guess. well...whatever :D
______________________________**_________________ Pd-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/** listinfo/pd-list http://lists.puredata.info/listinfo/pd-list
______________________________**_________________ Pd-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/** listinfo/pd-list http://lists.puredata.info/listinfo/pd-list
Pd-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list
how do you get from a continued fraction in the form like this:
[0;1,5,2,2]
to a fraction in the form like this:
27/32
this patch gets as far as that [0; 1,5,2,2] form. but i'm still not sure how to get further
Le 2011-12-17 à 03:44:00, i go bananas a écrit :
how do you get from a continued fraction in the form like this: [0;1,5,2,2] to a fraction in the form like this: 27/32 this patch gets as far as that [0; 1,5,2,2] form. but i'm still not sure how to get further
keep track of the last two fractions and feed that to something like expr.
e.g. $f1 = element of the list $f2 = last numerator $f3 = last denominator $f4 = next-to-last numerator $f5 = next-to-last denominator
the number before the semicolon should go with the values 1 0 0 1 to start the algorithm (yes, you have to pretend that a denominator is zero, but don't worry)
then it would be something like :
[expr $f1*$f2+$f4; $f1*$f3+$f5; $f2; $f3]
and then you take the four outputs and feed them back into expr together with the next number in the continued fraction...
the advantage of this, is that you can go left-to-right.
With the more obvious method, you have to go right-to-left, because the innermost terms (inside many parentheses) are at the right.
The left-to-right formula is something I learned in Number Theory course in fév.2003, but naturally, I had to look it up in Wikipédia in order to remember, as I don't use this very often...
Let's try the formula with [0;1,5,2,2] : 0 1 0 0 1 gives 0 1 1 0 1 0 1 1 0 gives 1 1 0 1 5 1 1 0 1 gives 5 6 1 1 2 5 6 1 1 gives 11 13 5 6 2 11 13 5 6 gives 27 32 11 13
looks like it works ! 27 32 appear in the 2nd and 1st outlets.
| Mathieu BOUCHARD ----- téléphone : +1.514.383.3801 ----- Montréal, QC
From: i go bananas hard.off@gmail.com To: Ludwig Maes ludwig.maes@gmail.com Cc: pd-list@iem.at Sent: Friday, December 16, 2011 1:16 PM Subject: Re: [PD] number to fractions external?
if you had read the thread, you would have seen that claude posted a link to that technique.
now go and make a PD patch that does it, mr smart guy.
Wow, how much cpu does that take in Python? I tried this approach in the form
of an abstraction, with a nested until, and worst case it can take as much as a quarter of
a second to compute with the constants provided below.
(Pentium Dual-core 2.6gHz in WinXP with 0.43 nightly build)
-Jonathan
On Sat, Dec 17, 2011 at 3:00 AM, Ludwig Maes ludwig.maes@gmail.com wrote:
If you guys 'd done your math, you'd know there is an ancient algorithm for approximating numbers by fractions and its called continued fractions.
On 16 December 2011 18:38, Lorenzo Sutton lorenzofsutton@gmail.com wrote:
On 16/12/11 16:05, Alexandre Torres Porres wrote:
looks like a job for an external
Not really answering the OP question but something could be done in Python:
def find_frac(num): f = float(num) last_error = 1000 best = (0,0) for i in xrange(1,1001): for j in xrange(1,i+1): divide = (float(i) / float (j)) if divide == f: return ((i,j),0) err = abs(divide - f) if err < last_error: best = (i,j) last_error = err return (best,last_error)
This would try to find the exact fraction or the one with the smallest error (trying up to 1000/1000). It would return (numerator, denominator, error). Guess it would work well at least up to 100 but only for positive numbers... and... not for numbers < 1.. and surely it's not optimised etc. etc. :)
find_frac(2)
((2, 1), 0)
find_frac(1.5)
((3, 2), 0)
find_frac(1.333333333333333333333333333)
((4, 3), 0)
find_frac(2.4)
((12, 5), 0)
find_frac(2.8)
((14, 5), 0)
find_frac(2.987654321)
((242, 81), 1.234568003383174e-11)
find_frac(50.32)
((956, 19), 0.004210526315787888)
find_frac(50.322)
((956, 19), 0.006210526315790332)
find_frac(50.4)
((252, 5), 0)
find_frac(10.33)
((971, 94), 0.00021276595744623705)
find_frac(10.33333333333333333333333333)
((31, 3), 0)
Lorenzo.
2011/12/16 i go bananas <hard.off@gmail.com mailto:hard.off@gmail.com>
actually, i'm not going to do anything more on this.
i had a look at the articles claude posted, and they went a bit far over my head.
my patch will still work for basic things like 1/4 and 7/8, but i wouldn't depend on it working for a serious application. As you first suggested, it's not so simple, and if you read claude's articles, you will see that it isn't.
it's not brain science though, so maybe someone with a bit more number understanding can tackle it.
On Sat, Dec 17, 2011 at 12:51 AM, Alexandre Torres Porres
<porres@gmail.com mailto:porres@gmail.com> wrote:
> i had a go at it
thanks, I kinda had to go too, but no time... :(
> yeah, my patch only works for rational numbers.
you know what, I think I asked this before on this list,
deja'vu
> will have a look at the article / method you posted, claude.
are you going at it too? :)
by the way, I meant something like 1.75 becomes 7/4 and not 3/4, but that is easy to adapt on your patch
thanks
cheers
2011/12/16 i go bananas <hard.off@gmail.com
by the way, here is the method i used:
first, convert the decimal part to a fraction in the form of n/100000 next, find the highest common factor of n and 100000 (using the 'division method' like this: http://easycalculation.com/what-is-hcf.php )
then just divide n and 100000 by that factor.
actually, that means it's accurate to 6 decimal places, i guess. well...whatever :D
Pd-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list
Pd-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list
Pd-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list
Pd-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list
I just gave this a go, and here's what I have so far based on the Wikipedia link Claude gave. Send the decimal through the left inlet, and it outputs the numerator and denominator as a list. The argument is the maximum value the denominator is allowed to be, which keeps it from going crazy trying to figure out irrational numbers and also seems to make up for some floating-points errors. You can change the max with the right inlet as well. Right now it defaults to 100, but that may be too low. Higher values=more accurate, but potentially more computation.
I haven't implemented the rules for decrementing the last value of the continuous fractions, so it's not perfect. But it does give 355/113 for pi. :-)
.mmb
On Fri, Dec 16, 2011 at 2:46 PM, Jonathan Wilkes jancsika@yahoo.com wrote:
From: i go bananas hard.off@gmail.com To: Ludwig Maes ludwig.maes@gmail.com Cc: pd-list@iem.at Sent: Friday, December 16, 2011 1:16 PM Subject: Re: [PD] number to fractions external?
if you had read the thread, you would have seen that claude posted a link to that technique.
now go and make a PD patch that does it, mr smart guy.
Wow, how much cpu does that take in Python? I tried this approach in the form
of an abstraction, with a nested until, and worst case it can take as much as a quarter of
a second to compute with the constants provided below.
(Pentium Dual-core 2.6gHz in WinXP with 0.43 nightly build)
-Jonathan
On Sat, Dec 17, 2011 at 3:00 AM, Ludwig Maes ludwig.maes@gmail.com wrote:
If you guys 'd done your math, you'd know there is an ancient algorithm for approximating numbers by fractions and its called continued fractions.
On 16 December 2011 18:38, Lorenzo Sutton lorenzofsutton@gmail.com wrote:
On 16/12/11 16:05, Alexandre Torres Porres wrote:
looks like a job for an external
Not really answering the OP question but something could be done in Python:
def find_frac(num): f = float(num) last_error = 1000 best = (0,0) for i in xrange(1,1001): for j in xrange(1,i+1): divide = (float(i) / float (j)) if divide == f: return ((i,j),0) err = abs(divide - f) if err < last_error: best = (i,j) last_error = err return (best,last_error)
This would try to find the exact fraction or the one with the smallest error (trying up to 1000/1000). It would return (numerator, denominator, error). Guess it would work well at least up to 100 but only for positive numbers... and... not for numbers < 1.. and surely it's not optimised etc. etc. :)
> find_frac(2)
((2, 1), 0)
> find_frac(1.5)
((3, 2), 0)
> find_frac(1.333333333333333333333333333)
((4, 3), 0)
> find_frac(2.4)
((12, 5), 0)
> find_frac(2.8)
((14, 5), 0)
> find_frac(2.987654321)
((242, 81), 1.234568003383174e-11)
> find_frac(50.32)
((956, 19), 0.004210526315787888)
> find_frac(50.322)
((956, 19), 0.006210526315790332)
> find_frac(50.4)
((252, 5), 0)
> find_frac(10.33)
((971, 94), 0.00021276595744623705)
> find_frac(10.33333333333333333333333333)
((31, 3), 0)
Lorenzo.
2011/12/16 i go bananas <hard.off@gmail.com mailto:hard.off@gmail.com>
actually, i'm not going to do anything more on this.
i had a look at the articles claude posted, and they went a bit far over my head.
my patch will still work for basic things like 1/4 and 7/8, but i wouldn't depend on it working for a serious application. As you first suggested, it's not so simple, and if you read claude's articles, you will see that it isn't.
it's not brain science though, so maybe someone with a bit more number understanding can tackle it.
On Sat, Dec 17, 2011 at 12:51 AM, Alexandre Torres Porres
<porres@gmail.com mailto:porres@gmail.com> wrote:
> i had a go at it
thanks, I kinda had to go too, but no time... :(
> yeah, my patch only works for rational numbers.
you know what, I think I asked this before on this list,
deja'vu
> will have a look at the article / method you posted, claude.
are you going at it too? :)
by the way, I meant something like 1.75 becomes 7/4 and not 3/4, but that is easy to adapt on your patch
thanks
cheers
2011/12/16 i go bananas <hard.off@gmail.com
by the way, here is the method i used:
first, convert the decimal part to a fraction in the form of n/100000 next, find the highest common factor of n and 100000 (using the 'division method' like this: http://easycalculation.com/what-is-hcf.php )
then just divide n and 100000 by that factor.
actually, that means it's accurate to 6 decimal places, i guess. well...whatever :D
Pd-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list
Pd-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list
Pd-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list
Pd-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list
Pd-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list
The one I posted earlier apparently can run into stack overflows. I think this version fixes it.
.mmb
On Fri, Dec 16, 2011 at 9:30 PM, Mike Moser-Booth mmoserbooth@gmail.com wrote:
I just gave this a go, and here's what I have so far based on the Wikipedia link Claude gave. Send the decimal through the left inlet, and it outputs the numerator and denominator as a list. The argument is the maximum value the denominator is allowed to be, which keeps it from going crazy trying to figure out irrational numbers and also seems to make up for some floating-points errors. You can change the max with the right inlet as well. Right now it defaults to 100, but that may be too low. Higher values=more accurate, but potentially more computation.
I haven't implemented the rules for decrementing the last value of the continuous fractions, so it's not perfect. But it does give 355/113 for pi. :-)
.mmb
On Fri, Dec 16, 2011 at 2:46 PM, Jonathan Wilkes jancsika@yahoo.com wrote:
From: i go bananas hard.off@gmail.com To: Ludwig Maes ludwig.maes@gmail.com Cc: pd-list@iem.at Sent: Friday, December 16, 2011 1:16 PM Subject: Re: [PD] number to fractions external?
if you had read the thread, you would have seen that claude posted a link to that technique.
now go and make a PD patch that does it, mr smart guy.
Wow, how much cpu does that take in Python? I tried this approach in the form
of an abstraction, with a nested until, and worst case it can take as much as a quarter of
a second to compute with the constants provided below.
(Pentium Dual-core 2.6gHz in WinXP with 0.43 nightly build)
-Jonathan
On Sat, Dec 17, 2011 at 3:00 AM, Ludwig Maes ludwig.maes@gmail.com wrote:
If you guys 'd done your math, you'd know there is an ancient algorithm for approximating numbers by fractions and its called continued fractions.
On 16 December 2011 18:38, Lorenzo Sutton lorenzofsutton@gmail.com wrote:
On 16/12/11 16:05, Alexandre Torres Porres wrote:
looks like a job for an external
Not really answering the OP question but something could be done in Python:
def find_frac(num): f = float(num) last_error = 1000 best = (0,0) for i in xrange(1,1001): for j in xrange(1,i+1): divide = (float(i) / float (j)) if divide == f: return ((i,j),0) err = abs(divide - f) if err < last_error: best = (i,j) last_error = err return (best,last_error)
This would try to find the exact fraction or the one with the smallest error (trying up to 1000/1000). It would return (numerator, denominator, error). Guess it would work well at least up to 100 but only for positive numbers... and... not for numbers < 1.. and surely it's not optimised etc. etc. :)
>> find_frac(2)
((2, 1), 0)
>> find_frac(1.5)
((3, 2), 0)
>> find_frac(1.333333333333333333333333333)
((4, 3), 0)
>> find_frac(2.4)
((12, 5), 0)
>> find_frac(2.8)
((14, 5), 0)
>> find_frac(2.987654321)
((242, 81), 1.234568003383174e-11)
>> find_frac(50.32)
((956, 19), 0.004210526315787888)
>> find_frac(50.322)
((956, 19), 0.006210526315790332)
>> find_frac(50.4)
((252, 5), 0)
>> find_frac(10.33)
((971, 94), 0.00021276595744623705)
>> find_frac(10.33333333333333333333333333)
((31, 3), 0)
Lorenzo.
2011/12/16 i go bananas <hard.off@gmail.com mailto:hard.off@gmail.com>
actually, i'm not going to do anything more on this.
i had a look at the articles claude posted, and they went a bit far over my head.
my patch will still work for basic things like 1/4 and 7/8, but i wouldn't depend on it working for a serious application. As you first suggested, it's not so simple, and if you read claude's articles, you will see that it isn't.
it's not brain science though, so maybe someone with a bit more number understanding can tackle it.
On Sat, Dec 17, 2011 at 12:51 AM, Alexandre Torres Porres
<porres@gmail.com mailto:porres@gmail.com> wrote:
> i had a go at it
thanks, I kinda had to go too, but no time... :(
> yeah, my patch only works for rational numbers.
you know what, I think I asked this before on this list,
deja'vu
> will have a look at the article / method you posted, claude.
are you going at it too? :)
by the way, I meant something like 1.75 becomes 7/4 and not 3/4, but that is easy to adapt on your patch
thanks
cheers
2011/12/16 i go bananas <hard.off@gmail.com
by the way, here is the method i used:
first, convert the decimal part to a fraction in the form of n/100000 next, find the highest common factor of n and 100000 (using the 'division method' like this: http://easycalculation.com/what-is-hcf.php )
then just divide n and 100000 by that factor.
actually, that means it's accurate to 6 decimal places, i guess. well...whatever :D
Pd-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list
Pd-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list
Pd-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list
Pd-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list
Pd-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list
-- Mike Moser-Booth mmoserbooth@gmail.com
Le 2011-12-16 à 15:51:00, i go bananas a écrit :
(using the 'division method' like this: http://easycalculation.com/what-is-hcf.php )
The division method is much more known as Euclid's Algorithm...
And I never saw it named HCF before. Always PGCD in French and GCD in English.
By the way, GridFlow has an operator for computing one or many GCD at once : [# gcd].
Here's a visualisation of the GCD of all combinations of numbers from 0 to 255 :
http://gridflow.ca/gallery/gcd.gif
I made it in fév.2003... it's one of the oldest GridFlow patches ever.
then just divide n and 100000 by that factor. actually, that means it's accurate to 6 decimal places, i guess. well...whatever :D
100000 means 5 decimal places. A million is 6 decimal places. You can see that by looking at how 10, 100, 1000, etc. multiply with each other, and also, logarithms in base 10.
| Mathieu BOUCHARD ----- téléphone : +1.514.383.3801 ----- Montréal, QC
Le 2011-12-16 à 00:52:00, Alexandre Torres Porres a écrit :
Is there an external that converts decimal numbers to fractions, like 1.5 => 3 / 2 ? I bet it's complicated to do it as a vanilla patch, right?
Take the number, subtract its whole part to get something less than 1, then invert it to get something bigger than 1, and repeat.
You will get a list of characteristic numbers that can be used to make good approximations real quick by doing a+1/(b+1/(c+1/(d+1/(e+etc...))))
But unlike what this latter formula looks like, you don't even need to keep a list containing values a b c d e etc... because there's a simple shortcut so that you don't have to start with the innermost parentheses.
http://pt.wikipedia.org/wiki/Fra%C3%A7%C3%A3o_cont%C3%ADnua
| Mathieu BOUCHARD ----- téléphone : +1.514.383.3801 ----- Montréal, QC