Skip to content

Instantly share code, notes, and snippets.

@genotrance
Last active April 30, 2018 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save genotrance/97aa4f2417165caa35779a9653b27111 to your computer and use it in GitHub Desktop.
Save genotrance/97aa4f2417165caa35779a9653b27111 to your computer and use it in GitHub Desktop.
import math
import macros
import strutils
macro mechoall(): untyped =
var
lval1 = 50.2345
lval2 = 25.1234
lval = ""
hval1 = 50000000.5678
hval2 = 2500123.9876
hval = ""
done: seq[string] = @[]
filt = @["c_frexp", "classify"]
result = newNimNode(nnkStmtList)
for line in staticRead("mecho.txt").splitLines():
if "float" in line and "*(" in line and "`" notin line:
lval = if "," notin line.split(")")[0]: $lval1 else: $lval1 & ", " & $lval2
hval = if "," notin line.split(")")[0]: $lval1 else: $hval1 & ", " & $hval2
let pname = line.split("*")[0]
if pname notin done and pname notin filt:
done.add(pname)
let stmts = """
echo "assert $$$1($2) == \"" & $$$1($2) & "\""
echo "assert $$$1($3) == \"" & $$$1($3) & "\""
""" % [pname, $lval, $hval]
result.add(parseStmt(stmts))
echo "import math"
mechoall()
binom*(n, k: int): int {.noSideEffect.} =
fac*(n: int): int {.noSideEffect.} =
classify*(x: float): FloatClass =
isPowerOfTwo*(x: int): bool {.noSideEffect.} =
nextPowerOfTwo*(x: int): int {.noSideEffect.} =
countBits32*(n: int32): int {.noSideEffect.} =
sum*[T](x: openArray[T]): T {.noSideEffect.} =
sqrt*(x: float32): float32 {.importc: "sqrtf", header: "<math.h>".}
sqrt*(x: float64): float64 {.importc: "sqrt", header: "<math.h>".}
cbrt*(x: float32): float32 {.importc: "cbrtf", header: "<math.h>".}
cbrt*(x: float64): float64 {.importc: "cbrt", header: "<math.h>".}
ln*(x: float32): float32 {.importc: "logf", header: "<math.h>".}
ln*(x: float64): float64 {.importc: "log", header: "<math.h>".}
log10*(x: float32): float32 {.importc: "log10f", header: "<math.h>".}
log10*(x: float64): float64 {.importc: "log10", header: "<math.h>".}
log2*[T: float32|float64](x: T): T = return ln(x) / ln(2.0)
exp*(x: float32): float32 {.importc: "expf", header: "<math.h>".}
exp*(x: float64): float64 {.importc: "exp", header: "<math.h>".}
arccos*(x: float32): float32 {.importc: "acosf", header: "<math.h>".}
arccos*(x: float64): float64 {.importc: "acos", header: "<math.h>".}
arcsin*(x: float32): float32 {.importc: "asinf", header: "<math.h>".}
arcsin*(x: float64): float64 {.importc: "asin", header: "<math.h>".}
arctan*(x: float32): float32 {.importc: "atanf", header: "<math.h>".}
arctan*(x: float64): float64 {.importc: "atan", header: "<math.h>".}
arctan2*(y, x: float32): float32 {.importc: "atan2f", header: "<math.h>".}
arctan2*(y, x: float64): float64 {.importc: "atan2", header: "<math.h>".}
cos*(x: float32): float32 {.importc: "cosf", header: "<math.h>".}
cos*(x: float64): float64 {.importc: "cos", header: "<math.h>".}
cosh*(x: float32): float32 {.importc: "coshf", header: "<math.h>".}
cosh*(x: float64): float64 {.importc: "cosh", header: "<math.h>".}
hypot*(x, y: float32): float32 {.importc: "hypotf", header: "<math.h>".}
hypot*(x, y: float64): float64 {.importc: "hypot", header: "<math.h>".}
sinh*(x: float32): float32 {.importc: "sinhf", header: "<math.h>".}
sinh*(x: float64): float64 {.importc: "sinh", header: "<math.h>".}
sin*(x: float32): float32 {.importc: "sinf", header: "<math.h>".}
sin*(x: float64): float64 {.importc: "sin", header: "<math.h>".}
tan*(x: float32): float32 {.importc: "tanf", header: "<math.h>".}
tan*(x: float64): float64 {.importc: "tan", header: "<math.h>".}
tanh*(x: float32): float32 {.importc: "tanhf", header: "<math.h>".}
tanh*(x: float64): float64 {.importc: "tanh", header: "<math.h>".}
pow*(x, y: float32): float32 {.importc: "powf", header: "<math.h>".}
pow*(x, y: float64): float64 {.importc: "pow", header: "<math.h>".}
erf*(x: float32): float32 {.importc: "erff", header: "<math.h>".}
erf*(x: float64): float64 {.importc: "erf", header: "<math.h>".}
erfc*(x: float32): float32 {.importc: "erfcf", header: "<math.h>".}
erfc*(x: float64): float64 {.importc: "erfc", header: "<math.h>".}
lgamma*(x: float32): float32 {.importc: "lgammaf", header: "<math.h>".}
lgamma*(x: float64): float64 {.importc: "lgamma", header: "<math.h>".}
tgamma*(x: float32): float32 {.importc: "tgammaf", header: "<math.h>".}
tgamma*(x: float64): float64 {.importc: "tgamma", header: "<math.h>".}
floor*(x: float32): float32 {.importc: "floorf", header: "<math.h>".}
floor*(x: float64): float64 {.importc: "floor", header: "<math.h>".}
ceil*(x: float32): float32 {.importc: "ceilf", header: "<math.h>".}
ceil*(x: float64): float64 {.importc: "ceil", header: "<math.h>".}
truncImpl(f: float64): float64 =
truncImpl(f: float32): float32 =
trunc*(x: float64): float64 =
trunc*(x: float32): float32 =
round0[T: float32|float64](x: T): T =
round0(x: float32): float32 {.importc: "roundf", header: "<math.h>".}
round0(x: float64): float64 {.importc: "round", header: "<math.h>".}
trunc*(x: float32): float32 {.importc: "truncf", header: "<math.h>".}
trunc*(x: float64): float64 {.importc: "trunc", header: "<math.h>".}
fmod*(x, y: float32): float32 {.importc: "fmodf", header: "<math.h>".}
fmod*(x, y: float64): float64 {.importc: "fmod", header: "<math.h>".}
trunc*(x: float32): float32 {.importc: "Math.trunc", nodecl.}
trunc*(x: float64): float64 {.importc: "Math.trunc", nodecl.}
floor*(x: float32): float32 {.importc: "Math.floor", nodecl.}
floor*(x: float64): float64 {.importc: "Math.floor", nodecl.}
ceil*(x: float32): float32 {.importc: "Math.ceil", nodecl.}
ceil*(x: float64): float64 {.importc: "Math.ceil", nodecl.}
sqrt*(x: float32): float32 {.importc: "Math.sqrt", nodecl.}
sqrt*(x: float64): float64 {.importc: "Math.sqrt", nodecl.}
ln*(x: float32): float32 {.importc: "Math.log", nodecl.}
ln*(x: float64): float64 {.importc: "Math.log", nodecl.}
log10*[T: float32|float64](x: T): T = return ln(x) / ln(10.0)
log2*[T: float32|float64](x: T): T = return ln(x) / ln(2.0)
exp*(x: float32): float32 {.importc: "Math.exp", nodecl.}
exp*(x: float64): float64 {.importc: "Math.exp", nodecl.}
round0(x: float): float {.importc: "Math.round", nodecl.}
pow*(x, y: float32): float32 {.importC: "Math.pow", nodecl.}
pow*(x, y: float64): float64 {.importc: "Math.pow", nodecl.}
arccos*(x: float32): float32 {.importc: "Math.acos", nodecl.}
arccos*(x: float64): float64 {.importc: "Math.acos", nodecl.}
arcsin*(x: float32): float32 {.importc: "Math.asin", nodecl.}
arcsin*(x: float64): float64 {.importc: "Math.asin", nodecl.}
arctan*(x: float32): float32 {.importc: "Math.atan", nodecl.}
arctan*(x: float64): float64 {.importc: "Math.atan", nodecl.}
arctan2*(y, x: float32): float32 {.importC: "Math.atan2", nodecl.}
arctan2*(y, x: float64): float64 {.importc: "Math.atan2", nodecl.}
cos*(x: float32): float32 {.importc: "Math.cos", nodecl.}
cos*(x: float64): float64 {.importc: "Math.cos", nodecl.}
cosh*(x: float32): float32 = return (exp(x)+exp(-x))*0.5
cosh*(x: float64): float64 = return (exp(x)+exp(-x))*0.5
hypot*[T: float32|float64](x, y: T): T = return sqrt(x*x + y*y)
sinh*[T: float32|float64](x: T): T = return (exp(x)-exp(-x))*0.5
sin*(x: float32): float32 {.importc: "Math.sin", nodecl.}
sin*(x: float64): float64 {.importc: "Math.sin", nodecl.}
tan*(x: float32): float32 {.importc: "Math.tan", nodecl.}
tan*(x: float64): float64 {.importc: "Math.tan", nodecl.}
tanh*[T: float32|float64](x: T): T =
round*[T: float32|float64](x: T, places: int = 0): T =
c_frexp*(x: float32, exponent: var int32): float32 {.
c_frexp*(x: float64, exponent: var int32): float64 {.
frexp*[T, U](x: T, exponent: var U): T =
frexp*[T: float32|float64](x: T, exponent: var int): T =
splitDecimal*[T: float32|float64](x: T): tuple[intpart: T, floatpart: T] =
degToRad*[T: float32|float64](d: T): T {.inline.} =
radToDeg*[T: float32|float64](d: T): T {.inline.} =
sgn*[T: SomeNumber](x: T): int {.inline.} =
`mod`*[T: float32|float64](x, y: T): T =
`^`*[T](x: T, y: Natural): T =
gcd*[T](x, y: T): T =
lcm*[T](x, y: T): T =
mySqrt(num: float): float {.noSideEffect.} =
`==~`(x, y: float): bool = (abs(x-y) < 1e-9)
import math
import macros
import strutils
macro mecho(pname, lval, hval: string): untyped =
let stmts = """
echo "assert $$$1($2) == \"" & $$$1($2) & "\""
echo "assert $$$1($3) == \"" & $$$1($3) & "\""
""" % [pname, lval, hval]
result = parseStmt(stmts)
macro mechoall(): untyped =
var
lval1 = 50.2345
lval2 = 25.1234
lval = ""
hval1 = 50000000.5678
hval2 = 2500123.9876
hval = ""
done: seq[string] = @[]
filt = @["c_frexp", "classify"]
result = newNimNode(nnkStmtList)
for line in staticRead("mecho.txt").splitLines():
if "float" in line and "*(" in line and "`" notin line:
lval = if "," notin line.split(")")[0]: $lval1 else: $lval1 & ", " & $lval2
hval = if "," notin line.split(")")[0]: $lval1 else: $hval1 & ", " & $hval2
let pname = line.split("*")[0]
if pname notin done and pname notin filt:
done.add(pname)
result.add(mecho(pname, $lval, $hval))
echo "import math"
mechoall()
# mecho.nim(9, 5) Error: type mismatch: got <string, array[0..2, NimNode]>
# but expected one of:
# proc `%`(formatstr: string; a: openArray[string]): string
# first type mismatch at position: 2
# required type: openarray[string]
# but expression '[pname, lval, hval]' is of type: array[0..2, NimNode]
# proc `%`(formatstr, a: string): string
# first type mismatch at position: 2
# required type: string
# but expression '[pname, lval, hval]' is of type: array[0..2, NimNode]
# expression: """echo "assert $$$1($2) == \"" & $$$1($2) & "\""
# echo "assert $$$1($3) == \"" & $$$1($3) & "\""
# """ %
# [pname, lval, hval]
import math
assert $sqrt(50.2345) == "7.087630069353224"
assert $sqrt(50.2345) == "7.087630069353224"
#assert $cbrt(50.2345) == "3.689781887456182"
#assert $cbrt(50.2345) == "3.689781887456182"
assert $ln(50.2345) == "3.916702041644877"
assert $ln(50.2345) == "3.916702041644877"
assert $log10(50.2345) == "1.701002083945571"
assert $log10(50.2345) == "1.701002083945571"
assert $exp(50.2345) == "6.554901216300291e+021"
assert $exp(50.2345) == "6.554901216300291e+021"
assert $arccos(50.2345) == "nan"
assert $arccos(50.2345) == "nan"
assert $arcsin(50.2345) == "nan"
assert $arcsin(50.2345) == "nan"
assert $arctan(50.2345) == "1.550892317797348"
assert $arctan(50.2345) == "1.550892317797348"
assert $arctan2(50.2345, 25.1234) == "1.107050781932043"
assert $arctan2(50000000.5678, 2500123.9876) == "1.520835458071592"
assert $cos(50.2345) == "0.9995200820573771"
assert $cos(50.2345) == "0.9995200820573771"
assert $cosh(50.2345) == "3.277450608150146e+021"
assert $cosh(50.2345) == "3.277450608150146e+021"
#assert $hypot(50.2345, 25.1234) == "56.16662904082815"
#assert $hypot(50000000.5678, 2500123.9876) == "50062467.7451419"
assert $sinh(50.2345) == "3.277450608150146e+021"
assert $sinh(50.2345) == "3.277450608150146e+021"
assert $sin(50.2345) == "-0.03097750093235722"
assert $sin(50.2345) == "-0.03097750093235722"
assert $tan(50.2345) == "-0.0309923747290742"
assert $tan(50.2345) == "-0.0309923747290742"
assert $tanh(50.2345) == "1.0"
assert $tanh(50.2345) == "1.0"
assert $pow(50.2345, 25.1234) == "5.431949901191602e+042"
assert $pow(50000000.5678, 2500123.9876) == "inf"
#assert $erf(50.2345) == "1.0"
#assert $erf(50.2345) == "1.0"
#assert $erfc(50.2345) == "0.0"
#assert $erfc(50.2345) == "0.0"
#assert $lgamma(50.2345) == "145.4813150879709"
#assert $lgamma(50.2345) == "145.4813150879709"
#assert $tgamma(50.2345) == "1.519610769465624e+063"
#assert $tgamma(50.2345) == "1.519610769465624e+063"
assert $floor(50.2345) == "50.0"
assert $floor(50.2345) == "50.0"
assert $ceil(50.2345) == "51.0"
assert $ceil(50.2345) == "51.0"
assert $trunc(50.2345) == "50.0"
assert $trunc(50.2345) == "50.0"
assert $fmod(50.2345, 25.1234) == "25.1111"
assert $fmod(50000000.5678, 2500123.9876) == "2497644.803399998"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment