tweak: save some progress.

This commit is contained in:
NunoSempere 2023-05-20 22:24:30 -04:00
parent dc27673887
commit 7e2d2b95a1
4 changed files with 51 additions and 18 deletions

2
wip/nim/makefile Normal file
View File

@ -0,0 +1,2 @@
build: samples.nim
nim c samples.nim

Binary file not shown.

View File

@ -1,32 +1,64 @@
import std/random
import std/math
import std/random
# randomize()
proc pow2(x:float, y:int): float =
return pow(x, float(y))
## Basic math functions
proc factorial(n: int): int =
if n == 0 or n < 0:
return 1
else:
return n * factorial(n - 1)
proc sine(x: float): float =
let n = 100
let n = 8
# ^ Taylor will converge really quickly
# notice that the factorial of 17 is
# already pretty gigantic
var acc = 0.0
for i in 0..n:
let k = 2*n + 1
let taylor = pow2(x, k) / float(k)
var k = 2*i + 1
var taylor = pow(-1, i.float) * pow(x, k.float) / factorial(k).float
acc = acc + taylor
return acc
return acc
# Helpers for calculating the log function
## Arithmetic-geomtric mean
proc ag(x: float, y: float): float =
let n = 100
var a = (x + y)/2.0
var b = sqrt(x * y)
for i in 0..n:
let temp = a
a = (a+b)/2.0
b = sqrt(b*temp)
return a
## Find m such that x * 2^m > 2^100
proc log_slow(x: float): float =
# See: <https://en.wikipedia.org/wiki/Natural_logarithm#High_precision>
var y = x - 1
let n = 100000000
var acc = 0.0
for i in 1..n:
let taylor = pow(-1.0, float(i+1)) * pow(y, i.float) / i.float
acc = acc + taylor
return acc
proc log(x: float): float =
var y = x - 1
let n = 1000
for i in 1..n:
let taylor = pow2(-1.0, n+1) * pow2(y, n) / float(n)
result = result + taylor
return result
return 1
## Test these functions
echo factorial(5)
echo sine(1.0)
echo log(1.0)
echo log(2.0)
## Distribution functions
proc normal(): float =
let u1 = rand(1.0)
let u2 = rand(1.0)
let z = 1
# see https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform#Basic_form
echo sine(0.1)

View File

@ -1,5 +1,4 @@
import std/math
# randomize()
proc factorial(n: int): int =
if n == 0 or n < 0:
@ -19,5 +18,5 @@ proc sine(x: float): float =
acc = acc + taylor
return acc
# echo factorial(17)
echo factorial(17)
echo sine(1.0)