product() unexpected result
- Posted by CraigWelch Apr 29, 2021
- 1029 views
The math function product() is giving me an unexpected result when the input is an empty sequence.
I test the function as follows:
object void void = {6,6,4,3} ? void void = product(void) ? void puts(1, EOL) void = {6,0,4,3} ? void void = product(void) ? void puts(1, EOL) void = {0,0,0,0} ? void void = product(void) ? void puts(1,EOL) void = {} ? void void = product(void) ? void puts(1, EOL)
This is the result:
{6,6,4,3} 432 {6,0,4,3} 0 {0,0,0,0} 0 {} 1
The last case is giving what is, in my case, a wrong answer.
I can see why the function does this, and understand why the variable b is set to 1 to make the function work. But it's not the result I expect, or want.
I've modified my own version of the routine as follows:
if atom(a) then return a elsif length(a) = 0 then -- CW add return 0 -- CW add end if
Any other thoughts on this ? As always, what is obvious to me might be misguided, I might be missing some bigger picture.
Craig