forum-msg-id-136129-edit

Original date:2021-04-29 07:50:08 Edited by: petelomax Subject: Re: product() unexpected result

Can you provide me with a practical example of where product({}) returning 0 would actually be useful? [For me docs]

(you can ignore this now:)
See also https://en.wikipedia.org/wiki/Empty_product
In a quick search I found that Haskell and Python are quite adamant the answer should be 1.

Update: I've just found a still-commented-out function I wrote in mpfr.e:

function mpz_vecprod(sequence s, object zls=1) 
-- 
-- Fast vector multiplication. 
-- Multiplying the vector elements in pairs is much faster for essentially  
--  much the same reason that merge sort is faster than insertion sort. 
--  Improved rosettacode/Primorial_numbers from 6 minutes to 6 seconds!!!! 
-- NB: Input sequence s (must all be mpz) is damaged. Returns an mpz. 
--     Obviously zls allows you to specify the result when {} is passed, 
--     since I imagine there'll be cases where you'd rather get a zero, 
--     and just like mpz_init(), zls can be an integer, atom, or string. 
-- 

So maybe the answer is for product() to have an optional second parameter, as above defaulting to 1.

(See https://rosettacode.org/wiki/Primorial_numbers#Phix for the precursor to that, without the zls parameter)
(Note that mpz_vecproc() is/will be obliged to return an mpz, so zls must be "numeric", but that's not necessarily true for product(), though probably still a good idea?)

Not Categorized, Please Help

Search



Quick Links

User menu

Not signed in.

Misc Menu