Rational Approximation
2 posts
• Page 1 of 1
Rational Approximation
Providing you have the following patch installed:
here's function, which uses GCD to find the smallest pair, whose quotient is tolerably equal to the argument:
The shape of the result is the same as that of the (possibly nested) argument but with an additional leading length-2 axis.
See also: http://www.dyalog.com/dfnsdws/n_rational.htm
3745: Logged & Fixed (Versions: 11.0.1) 2008-09-08
Tolerant GCD and LCM for non-integers have been improved.
here's function, which uses GCD to find the smallest pair, whose quotient is tolerably equal to the argument:
- Code: Select all
rational←{⎕ml←0 ⋄ ↑⍵ 1÷⊂1∨⍵}
The shape of the result is the same as that of the (possibly nested) argument but with an additional leading length-2 axis.
- Code: Select all
rational 12.3 0.456 7.89
123 57 789
10 125 100
rational ○1 ⍝ rational within ⎕CT of pi.
5419351 1725033
See also: http://www.dyalog.com/dfnsdws/n_rational.htm
- JohnS|Dyalog
Re: Rational Approximation
We could supply comparison tolerance as an optional left argument to rational thus:
NB: the final example might seem strange in that, with zero tolerance, pi is certainly not equal to any rational number. However ○1 is not exactly pi; it is the best IEEE double floating approximation to pi and all IEEE floating point numbers are rational (Yeah?).
- Code: Select all
rational←{⎕ML←0 ⍝ rational approximation to real ⍵.
⍺←⎕CT ⋄ ⎕CT←⍺ ⍝ default tolerance.
↑⍵ 1÷⊂1∨⍵ ⍝ ⍵≡÷⌿∇⍵
}
rational ○1 ⍝ default tolerance.
5419351 1725033
(16*¯8) rational ○1 ⍝ coarse tolerance.
103993 33102
0 rational ○1 ⍝ zero-tolerance.
245850922 78256779
NB: the final example might seem strange in that, with zero tolerance, pi is certainly not equal to any rational number. However ○1 is not exactly pi; it is the best IEEE double floating approximation to pi and all IEEE floating point numbers are rational (Yeah?).
- JohnS|Dyalog
2 posts
• Page 1 of 1
Return to Functional Programming
Who is online
Users browsing this forum: No registered users and 1 guest
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group