Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ccMJh-00056L-5g for pgsql-hackers@arkaria.postgresql.org; Sat, 11 Feb 2017 01:17:33 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1ccMJg-0004QZ-P1 for pgsql-hackers@arkaria.postgresql.org; Sat, 11 Feb 2017 01:17:32 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ccMJg-0004QR-6T for pgsql-hackers@postgresql.org; Sat, 11 Feb 2017 01:17:32 +0000 Received: from mail-wr0-x22e.google.com ([2a00:1450:400c:c0c::22e]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1ccMJd-0006SN-EO for pgsql-hackers@postgresql.org; Sat, 11 Feb 2017 01:17:30 +0000 Received: by mail-wr0-x22e.google.com with SMTP id i10so119721606wrb.0 for ; Fri, 10 Feb 2017 17:17:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:cc:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding; bh=anFSRLgge5YOX0XMIuMaQjb3dMSYfeJzAYmGBySA7EQ=; b=zS08sXashNIaG48S9T/9kr8cP37zoKrcu7XtxwYHlvHVbQGhIoNfeu1QKiNLR0GWU5 Aj1906wILuTsZmc6fgsQrQbGzIDjthv3rB1/oXEqGtdbF2RYhpoBgmAQ9izuUI1unTDc L7qZmjgdpE1E8/NdWnYv6ZjyH2mLdpR5d9Ven5DOGdcPbrgAKSMFP+fhT3AtMbXHn8pd AQDPP2tpVvuDtMThJeegEcJpzOJZeZVVFyblFLiyMwQ6x0CqXZyriNEjKdwtahoW0V+M RpEAqpL9tMFsBpR7crC7tqBQrCAqhaaMZW7FSC5TNqcBEReM+G9LEe2ml4J+Wav0XMzW MWzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:cc:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=anFSRLgge5YOX0XMIuMaQjb3dMSYfeJzAYmGBySA7EQ=; b=YC62MZY1o3i3tV0/Qt9KUPBus46DaR5DYDVLZxJ/T4Yxi/bRqcIJhAj4U0g/dg2LEE /3dbtrP8V+vupOnr3aefpiteCUuW/4p6SIPyLj5484fZCg7Mw/pD41h2ZPxzjkPq7BzF u99wAsq6vEbrIDKJxwUNPMDeeehW+qwnRxu+GXGrUwCoUYYkMtMSulYNzE5tnQR3nWuj FRHbh95E9IsxMi24lRSN7831refCjhFE8f68vBpfP0wtedsmVBl2+vb54Awnrm/xF7vY pn8XX0kV1SX4U9WrOsFWAl/X6Oh2OHiLA1DJ4ypoWJZgDLaL7tc0MKYqsVxRhZ1Ay1zR Fm1A== X-Gm-Message-State: AMke39lQFhltg3tby0yTKsXb/bosKVVjSssh3fT8fWHAsgKhADoGoj+uNoO4aRtNe/n3YkTBhhOqZSoEV2pxPotgnfpPsy8W3f8BW32eWnYNykMCpCXxBKrhMqFCLkbnP4cp/VEuNGR7tYQDkWZRexj8hEesHCl/JwYzxSNvpzENdyLpygdEvxr6rURINUZ0aAPyNVoi5q5a X-Received: by 10.223.136.155 with SMTP id f27mr9704198wrf.98.1486775847942; Fri, 10 Feb 2017 17:17:27 -0800 (PST) Received: from [10.137.2.17] (ip-89-177-46-65.net.upcbroadband.cz. [89.177.46.65]) by smtp.gmail.com with ESMTPSA id u184sm1629880wmb.29.2017.02.10.17.17.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 10 Feb 2017 17:17:27 -0800 (PST) Subject: Re: multivariate statistics (v19) To: Dean Rasheed , David Fetter References: <90fd1100-1886-cb4b-10ee-c556ac6c3d12@2ndquadrant.com> <20170206212616.itctsg6x7u6atbtp@alvherre.pgsql> <20170208160908.GC8118@fetter.org> Cc: Alvaro Herrera , PostgreSQL Hackers From: Tomas Vondra Message-ID: Date: Sat, 11 Feb 2017 02:17:23 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org On 02/08/2017 07:40 PM, Dean Rasheed wrote: > On 8 February 2017 at 16:09, David Fetter wrote: >> Combinations are n!/(k! * (n-k)!), so computing those is more >> along the lines of: >> >> unsigned long long >> choose(unsigned long long n, unsigned long long k) { >> if (k > n) { >> return 0; >> } >> unsigned long long r = 1; >> for (unsigned long long d = 1; d <= k; ++d) { >> r *= n--; >> r /= d; >> } >> return r; >> } >> >> which greatly reduces the chance of overflow. >> > > Hmm, but that doesn't actually prevent overflows, since it can > overflow in the multiplication step, and there is no protection > against that. > > In the algorithm I presented, the inputs and the intermediate result > are kept below INT_MAX, so the multiplication step cannot overflow the > 64-bit integer, and it will only raise an overflow error if the actual > result won't fit in a 32-bit int. Actually a crucial part of that, > which I failed to mention previously, is the first step replacing k > with min(k, n-k). This is necessary for inputs like (100,99), which > should return 100, and which must be computed as 100 choose 1, not 100 > choose 99, otherwise it will overflow internally before getting to the > final result. > Thanks for the feedback, I'll fix this. I've allowed myself to be a bit sloppy because the number of attributes in the statistics is currently limited to 8, so the overflows are currently not an issue. But it doesn't hurt to make it future-proof, in case we change that mostly artificial limit sometime in the future. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers