DavidK (3) [Avatar] Offline
#1
I'm not sure whether this is a good example.
Summing up 1..10^n always yields a number like 5050 or 500500. Your Example of a tail recursive sumlist function returns 705082704 for 1..100000 which is wrong due a IntegerOverflow. 5000050000 would be the correct result.

In my opinion I'd be useful to mention this problem or change the example if you can imagine a better one. (I'd know none, except changing the whole example to bigint which would add additional complexity)

Looking forward for the last chapters, great book so far! smilie

With best regards
David Klein
DavidK (3) [Avatar] Offline
#2
Re: P255 - Listing 10.2: Integer Overflow
Same issue concerns Listing 10.16 (Summing up the imbalanced tree) which does essentially the same calculation.

With best regards
David
Tomas Petricek (160) [Avatar] Offline
#3
Re: P255 - Listing 10.2: Integer Overflow
Hi David,
thanks for the feedback - I agree that this isn't the best example. It demonstrates the idea, but it should be something that doesn't rely on integer overflow...
I'll think about it in more detail, but a quick idea would be to replace "sum" with "average". That should work for both of the examples.

(And I'm glad you liked the book so far!)

Thanks!
Tomas

Message was edited by:
tomas.petricek
Tomas Petricek (160) [Avatar] Offline
#4
Re: P255 - Listing 10.2: Integer Overflow
Thinking of this again - "average" won't really work because it needs to sum the values anyway. Looks like I'll have to think of something else smilie. Anyway, thanks for pointing this out!