all repos — min @ 3dbe830d6e0fa242953b2a7f80034c0a9669e497

A small but practical concatenative programming language.

lib/min_math.nim

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
import 
  math,
  strformat,
  strutils
import 
  ../core/parser, 
  ../core/value, 
  ../core/interpreter, 
  ../core/utils

  # Math
  
proc math_module*(i: In)=

  let def = i.define()

  def.symbol("floor") do (i: In):
    let vals = i.expect("num")
    i.push vals[0].getFloat.floor.newVal
 
  def.symbol("ceil") do (i: In):
    let vals = i.expect("num")
    i.push vals[0].getFloat.ceil.newVal
 
  def.symbol("trunc") do (i: In):
    let vals = i.expect("num")
    i.push vals[0].getFloat.trunc.newVal
 
  def.symbol("round") do (i: In):
    let vals = i.expect("int", "num")
    let places = vals[0].intVal.int
    let n = vals[1].getFloat
    var res = ""
    formatValue(res, n, "." & $places & "f")
    i.push parseFloat(res).newVal
 
  def.symbol("e") do (i: In):
    i.push E.newVal
  
  def.symbol("pi") do (i: In):
    i.push PI.newVal
  
  def.symbol("tau") do (i: In):
    i.push TAU.newVal
  
  def.symbol("ln") do (i: In):
    let vals = i.expect("num")
    i.push vals[0].getFloat.ln.newVal
 
  def.symbol("log2") do (i: In):
    let vals = i.expect("num")
    i.push vals[0].getFloat.log2.newVal
 
  def.symbol("log10") do (i: In):
    let vals = i.expect("num")
    i.push vals[0].getFloat.log10.newVal
 
  def.symbol("pow") do (i: In):
    let vals = i.expect("num", "num")
    let y = vals[0].getFloat
    let x = vals[1].getFloat
    i.push x.pow(y).newVal
 
  def.symbol("sqrt") do (i: In):
    let vals = i.expect("num")
    i.push vals[0].getFloat.sqrt.newVal
 
  def.symbol("sin") do (i: In):
    let vals = i.expect("num")
    i.push vals[0].getFloat.sin.newVal
 
  def.symbol("cos") do (i: In):
    let vals = i.expect("num")
    i.push vals[0].getFloat.cos.newVal

  def.symbol("tan") do (i: In):
    let vals = i.expect("num")
    i.push vals[0].getFloat.tan.newVal

  def.symbol("sinh") do (i: In):
    let vals = i.expect("num")
    i.push vals[0].getFloat.sinh.newVal

  def.symbol("cosh") do (i: In):
    let vals = i.expect("num")
    i.push vals[0].getFloat.cosh.newVal

  def.symbol("tanh") do (i: In):
    let vals = i.expect("num")
    i.push vals[0].getFloat.tanh.newVal

  def.symbol("asin") do (i: In):
    let vals = i.expect("num")
    i.push vals[0].getFloat.arcsin.newVal

  def.symbol("acos") do (i: In):
    let vals = i.expect("num")
    i.push vals[0].getFloat.arccos.newVal

  def.symbol("atan") do (i: In):
    let vals = i.expect("num")
    i.push vals[0].getFloat.arctan.newVal

  def.symbol("d2r") do (i: In):
    let vals = i.expect("num")
    i.push vals[0].getFloat.degToRad.newVal

  def.symbol("r2g") do (i: In):
    let vals = i.expect("num")
    i.push vals[0].getFloat.radToDeg.newVal

  def.finalize("math")