tuseafterdef.nim 649 B

123456789101112131415161718192021222324252627282930313233
  1. discard """
  2. errormsg: "(k)..(k) not disjoint from (k)..(k)"
  3. line: 23
  4. action: compile
  5. """
  6. # bug #1597
  7. import strutils, math, threadpool
  8. type
  9. BoxedFloat = object
  10. value: float
  11. proc term(k: float): ptr BoxedFloat =
  12. var temp = 4 * math.pow(-1, k) / (2*k + 1)
  13. result = cast[ptr BoxedFloat](allocShared(sizeof(BoxedFloat)))
  14. result.value = temp
  15. proc pi(n: int): float =
  16. var ch = newSeq[ptr BoxedFloat](n+1)
  17. parallel:
  18. for k in 0..ch.high:
  19. ch[k] = (spawn term(float(k)))
  20. assert ch[k] != nil
  21. for k in 0..ch.high:
  22. var temp = ch[k][].value
  23. result += temp
  24. deallocShared(ch[k])
  25. echo formatFloat(pi(5000))