tsums.nim 590 B

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