tuseafterdef.nim 631 B

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