Skip to content

High precision math with decimal datatype

rollynoel edited this page Jun 13, 2013 · 2 revisions

This example

shows how to generate the "golden ratio" to 28 digits of precision using the decimal datatype and a Fibonacci Sequence generator function

.

// shows use of infinite fibonacci sequence with decimal datatype to generate golden ratio to 28 digits

def fib(): a as decimal = 0; b as decimal = 1 while true: yield b a, b = b, a + b

i = 0 diff as decimal = 10.0**-28 lastf as decimal = 1 ratio as decimal = 0 for f in fib(): // print "$i $f $(f/lastf) $ratio $(f/lastf - ratio)" break if ++i > 2 and System.Math.Abs(f/lastf - ratio) < diff ratio, lastf = f/lastf, f

print "error factor less than $diff on $(i)th iteration" print "golden ratio = $ratio"

Output:

error factor less than 0.0000000000000000000000000001 on 72th iteration golden ratio = 1.6180339887498948482045868344

Clone this wiki locally