Continued fraction/Arithmetic/Construct from rational number: Difference between revisions

m (→‎{{header|Tcl}}: minor tweaks for readability)
(→‎{{header|Tcl}}: More tweaks)
Line 382:
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
{{trans|Ruby}}
<lang tcl>package require Tcl 8.6
 
proc r2cf {n1 {n2 1}} {
# Convert a decimal fraction (e.g., 1.23) into a form we can handle
if {$n1 != int($n1) && [regexp {\.(\d+)} $n1 -> suffix]} {
set pow [string length $suffix]
Line 390 ⟶ 392:
set n2 [expr {$n2 * 10**$pow}]
}
# Construct the continued fraction as a coroutine that yields the digits in sequence
coroutine cf\#[incr ::cfcounter] apply {{n1 n2} {
yield [info coroutine]
Anonymous user