tsums.nim 612 B

123456789101112131415161718192021222324
  1. import std/sums
  2. from math import pow
  3. import std/assertions
  4. var epsilon = 1.0
  5. while 1.0 + epsilon != 1.0:
  6. epsilon /= 2.0
  7. let data = @[1.0, epsilon, -epsilon]
  8. doAssert sumKbn(data) == 1.0
  9. # doAssert sumPairs(data) != 1.0 # known to fail in 64 bits
  10. doAssert (1.0 + epsilon) - epsilon != 1.0
  11. var tc1: seq[float]
  12. for n in 1 .. 1000:
  13. tc1.add 1.0 / n.float
  14. doAssert sumKbn(tc1) == 7.485470860550345
  15. doAssert sumPairs(tc1) == 7.485470860550345
  16. var tc2: seq[float]
  17. for n in 1 .. 1000:
  18. tc2.add pow(-1.0, n.float) / n.float
  19. doAssert sumKbn(tc2) == -0.6926474305598203
  20. doAssert sumPairs(tc2) == -0.6926474305598204