June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,36 @@
function _sqrt(a::Bool, n)
if a
return n > 0 ? 2.0 : 1.0
else
return 1.0
end
end
function _napier(a::Bool, n)
if a
return n > 0 ? Float64(n) : 2.0
else
return n > 1 ? n - 1.0 : 1.0
end
end
function _pi(a::Bool, n)
if a
return n > 0 ? 6.0 : 3.0
else
return (2.0 * n - 1.0) ^ 2.0 # exponentiation operator
end
end
function calc(f::Function, expansions::Integer)
a, b = true, false
r = 0.0
for i in expansions:-1:1
r = f(b, i) / (f(a, i) + r)
end
return f(a, 0) + r
end
for (v, f) in (("√2", _sqrt), ("e", _napier), ("π", _pi))
@printf("%3s = %f\n", v, calc(f, 1000))
end

View file

@ -0,0 +1,21 @@
# Project : Continued fraction
# Date : 2018/03/17
# Author : Gal Zsolt [~ CalmoSoft ~]
# Email : <calmosoft@gmail.com>
see "SQR(2) = " + contfrac(1, 1, "2", "1") + nl
see " e = " + contfrac(2, 1, "n", "n") + nl
see " PI = " + contfrac(3, 1, "6", "(2*n+1)^2") + nl
func contfrac(a0, b1, a, b)
expr = ""
n = 0
while len(expr) < (700 - n)
n = n + 1
eval("temp1=" + a)
eval("temp2=" + b)
expr = expr + string(temp1) + char(43) + string(temp2) + "/("
end
str = copy(")",n)
eval("temp3=" + expr + "1" + str)
return a0 + b1 / temp3

View file

@ -0,0 +1,50 @@
#!r6rs
(import (rnrs base (6))
(srfi :41 streams))
(define nats (stream-cons 0 (stream-map (lambda (x) (+ x 1)) nats)))
(define (build-stream fn) (stream-map fn nats))
(define (stream-cycle s . S)
(cond
((stream-null? (car S)) stream-null)
(else (stream-cons (stream-car s)
(apply stream-cycle (append S (list (stream-cdr s))))))))
(define (cf-floor cf) (stream-car cf))
(define (cf-num cf) (stream-car (stream-cdr cf)))
(define (cf-denom cf) (stream-cdr (stream-cdr cf)))
(define (cf-integer? x) (stream-null? (stream-cdr x)))
(define (cf->real x)
(let refine ((x x) (n 65536))
(cond
((= n 0) +inf.0)
((cf-integer? x) (cf-floor x))
(else (+ (cf-floor x)
(/ (cf-num x)
(refine (cf-denom x) (- n 1))))))))
(define (real->cf x)
(let-values (((integer-part fractional-part) (div-and-mod x 1)))
(if (= fractional-part 0.0)
(stream (exact integer-part))
(stream-cons
(exact integer-part)
(stream-cons
1
(real->cf (/ fractional-part)))))))
(define sqrt2 (stream-cons 1 (stream-constant 1 2)))
(define napier
(stream-append (stream 2 1)
(stream-cycle (stream-cdr nats) (stream-cdr nats))))
(define pi
(stream-cons 3
(stream-cycle (build-stream (lambda (n) (expt (- (* 2 (+ n 1)) 1) 2)))
(stream-constant 6))))

View file

@ -0,0 +1,6 @@
> (cf->real sqrt2)
1.4142135623730951
> (cf->real napier)
2.7182818284590455
> (cf->real pi)
3.141592653589794