From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,PDS_BTC_ID,RCVD_IN_DNSWL_MED, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 14902 invoked from network); 25 Mar 2021 20:41:29 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 25 Mar 2021 20:41:29 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20200801; t=1616704889; b=VAgbETmcM7MC0rW32xuSK0FH4Aik931WOcsLbTQhqqLGwr/kGHfUeb8CO6MkB3ZtCA0uaq6AxR 7wm914i2ipedHZmksZHOcibeBRNev08EXYCfTa/xh3rCTR+Pcb1zAYVw1Ji5Vyrty92e3ZIlNd touHolFle40E7WVIs5OhkvOTAZ3JF27nAiNtfjPeUfi8Z5ESSETmTiV4/w+4O7lVqv4aE+T1zC FHhhLNVKxQIjgMHZZPj1XwVFFOjRGjNA3x5lkvONEqOyKfq+r9NP7hNi5Q3fi8Gzp69QJt5OR8 rgE2ZtUSn/lZ/XVzbTuuoh+jnkCA7/Z8xRWuyOxKNT0Dyg==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (wout1-smtp.messagingengine.com) smtp.remote-ip=64.147.123.24; dkim=pass header.d=larryv.me header.s=fm3 header.a=rsa-sha256; dkim=pass header.d=messagingengine.com header.s=fm2 header.a=rsa-sha256; dmarc=pass header.from=larryv.me; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20200801; t=1616704889; bh=H9x9nLr3BuE8/HHKC2c4uI4e7gQeQYdoKoYNlNeVrVo=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:To:References:Message-ID:Content-Transfer-Encoding:Cc:Date: In-Reply-To:From:Subject:MIME-Version:Content-Type:DKIM-Signature: DKIM-Signature:DKIM-Signature; b=Pw18S6z8Sji3XQyH/jVTZ1kBH4lZ09jZlPaAkjK3BWfHSxig5MrOhaf7tVmU7DjD4EKLYQFPR+ QDVfTOyWuoIqfx8tgoVCKBR+szUS5XBag0ltWX9rJJHXdxACsbsdP9czgoS2MN9fRFSuKgpVjg KFT3ciYjGR/kd33yWQbiW9m/m+gkmgmZchyVdGfg4TquIao1v45YRUyAObCC1orVtte/OZVr6J Nsq7Dc/rKMHMQdsSdwZ+ouKJuE9oOHFtruZarcqXADuSyuOBhOHDKO8NQbX5WFMZ3EoL61vpUI 06JOofQ4XZegxzLYeJvjw6AA1WryLKigDJOmaZ+HDi4r1w==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20200801; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:To:References:Message-Id: Content-Transfer-Encoding:Cc:Date:In-Reply-To:From:Subject:Mime-Version: Content-Type:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=yrVv1Ovdkodm3AE89RMo7HYKAeqgj+25cxLiatqlcE4=; b=DpzxLUfmkIPEUJVZNtBaF+idoC 04hcWKCAMSSeUmn9gkXDRnZZw9cdEkq5sPqawqEbeXqmLDWaQ8JR35HyMSSbi2VIq0MVHCcZDEqOz OxpoVI6vPUBHX+2bAQ56Zu057yswOsnohtQkm+hc4twKJ0FV2YUqqg/0YEXnObuE7OML+BcLvGCCw NvYPsOkTnxteYY0T+AwhniH5fztG5bbATkYDpv+FQTVEhNxy5owUiBeflNBG7Mot4bcgQ6g6CPu/u kDvzpD14wtaZXcGnpzvsDR0WkdZFKrnb2lxjivEMvhLd0CpGullrNv7oTY+fTbgJqnxqferxunjI1 DaSfCO5Q==; Received: from authenticated user by zero.zsh.org with local id 1lPWnH-000EKd-0g; Thu, 25 Mar 2021 20:41:27 +0000 Authentication-Results: zsh.org; iprev=pass (wout1-smtp.messagingengine.com) smtp.remote-ip=64.147.123.24; dkim=pass header.d=larryv.me header.s=fm3 header.a=rsa-sha256; dkim=pass header.d=messagingengine.com header.s=fm2 header.a=rsa-sha256; dmarc=pass header.from=larryv.me; arc=none Received: from wout1-smtp.messagingengine.com ([64.147.123.24]:33437) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_256_GCM_SHA384:256) id 1lPWmW-000Cwf-Vu; Thu, 25 Mar 2021 20:40:42 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.west.internal (Postfix) with ESMTP id 5ACA9195E; Thu, 25 Mar 2021 16:40:37 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Thu, 25 Mar 2021 16:40:37 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=larryv.me; h= content-type:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; s=fm3; bh=y rVv1Ovdkodm3AE89RMo7HYKAeqgj+25cxLiatqlcE4=; b=opx9FY7YkplWz8W3U y5TwD94JIIpt6xomfFVqqG8H6OKntbYZY7nQBiQG4z4xCIiuIJ94OnstUuN8iUmz kBsi8wjvq7cIFwOyAr22bmBcMSEmNivGU5mDUPkZFbNbAH8ys568Q3Ersko1DvBy dSoVrFGrs/mGUPf3rm62NCJJHPu2kgdekX8b+iNmqnJEqPsQEDB2PO0sxysakqjF pQikgFtIWQMlcxjJuMOa0FKjoXAQVuwwIXEg35mfTEtJZHBEynLXshGeq/ZmPEbj puHMxB2ldrDjnZvLpzFLi41849Wf7a5wIEbu7Nzoe4sd/SykUJff/iy0CPBVApH/ JHi8Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=yrVv1Ovdkodm3AE89RMo7HYKAeqgj+25cxLiatqlc E4=; b=ZpEzF4sdsBMl9aDWMvc1YGyaCc+ogtC5e/xDrqCYXhqErJJhiXEdPAhzZ HFKCYTbwc1caqXFNPCaV5eDvms2gqoireCkgQ/OJ7+r6Xe/brN6EAPM5bOJxvWfe F7/wmwYaiD5B2zUHAIJEg5qUclz/l2fbQFEO0HGxJCMBHZ880hdjbwzvwxHentVW l1daSRdkHiIdVBdXUk5xV69erjksjSj04O3UyCMrEdbArqKOQA4M+jn6P42B8MFI vyRTlqHvskObGi5+GrGwjShE/4IZ9zqnueGOUMvGxTl3wsaAMIc1UAg3KoXCqUO1 q2WBtt3wrwgKL+MWzk5j7LMVb0NQQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrudehtddgudegfecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpegtggfuhfgjfffgkfhfvffosehtqhhmtdhhtddvnecuhfhrohhmpefnrgif rhgvnhgtvggpgggvlhojiihquhgviicuoehvqheslhgrrhhrhihvrdhmvgeqnecuggftrf grthhtvghrnhepvedvudeileffueffheetjeeikeehledtfefhkeduuddutdehjedtheef hfelhfelnecuffhomhgrihhnpeifohhlfhhrrghmtghlohhuugdrtghomhenucfkphepud dttddruddvrddujeeirdeljeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhep mhgrihhlfhhrohhmpehvqheslhgrrhhrhihvrdhmvg X-ME-Proxy: Received: from [192.168.1.15] (pool-100-12-176-97.nycmny.fios.verizon.net [100.12.176.97]) by mail.messagingengine.com (Postfix) with ESMTPA id 8A1C01080057; Thu, 25 Mar 2021 16:40:36 -0400 (EDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.17\)) Subject: Re: arithmetic anomaly From: =?utf-8?Q?Lawrence_Vel=C3=A1zquez?= In-Reply-To: <0227b30d-4f09-2c8b-1248-557859d875ce@eastlink.ca> Date: Thu, 25 Mar 2021 16:40:34 -0400 Cc: zsh-users@zsh.org Content-Transfer-Encoding: quoted-printable Message-Id: <79FD66A8-76A5-4F76-9F3A-D6481F920862@larryv.me> References: <0227b30d-4f09-2c8b-1248-557859d875ce@eastlink.ca> To: Ray Andrews X-Mailer: Apple Mail (2.3445.104.17) X-Seq: 26602 Archived-At: X-Loop: zsh-users@zsh.org Errors-To: zsh-users-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-users-request@zsh.org X-no-archive: yes List-Id: List-Help: List-Subscribe: List-Unsubscribe: List-Post: List-Owner: List-Archive: Archived-At: > On Mar 25, 2021, at 11:52 AM, Ray Andrews = wrote: >=20 > It's puzzling that the two are perfect until the numbers get big, but = it's not close to overflow and even then the denominator is bigger and = has no issue. Obviously no bridges are going to fall down for an error = in the 17th digit, but I'm curious. The bad news is that *all* your figures -- your numerators, Brett's numerators, and the denominators -- are actually incorrect for level=3D15 and higher. The good news is that it's not your algorithms per se. Below are the results of interleaving your original script with lookalike Python implementations that produce correct figures. (For evidence of correctness, here's a concordant Wolfram Language implementation: https://www.wolframcloud.com/obj/6281a1a4-1094-4aac-b10b-4139b6444d6e) vq % zsh --version zsh 5.8 (x86_64-apple-darwin18.7.0) % python3.9 -V Python 3.9.2 % cat /tmp/demo.zsh #!/bin/zsh setopt force_float integer level=3D typeset -F term=3D typeset -F numerator=3D typeset -F denominator=3D for ((level=3D1; level<23; level++)); do numerator=3D for ((term=3D1; term <=3D level; term++)); do (( numerator +=3D (level**(term - 1)) * (level - 1)**(level - = term) )) done (( denominator =3D level**level )) printf 'L%-2d %-25s %.0f\n' $level 'Ray (zsh):' "$(( numerator ))" python3.9 - $level <<\EOF import sys level =3D int(sys.argv[1]) numerator =3D 0 for term in range(1, level+1): numerator +=3D (level**(term - 1)) * (level - 1)**(level - term) denominator =3D level**level print(f"L{level:<2} {'Ray (Python 3.9):':25} {numerator}") EOF numerator=3D1 for ((term=3D1; term < level; term++)); do (( numerator =3D numerator * (level - 1) + level**term )) done (( denominator =3D level**level )) printf 'L%-2d %-25s %.0f\n' level 'Brett (zsh):' "$(( numerator ))" python3.9 - $level <<\EOF import sys level =3D int(sys.argv[1]) numerator =3D 1 for term in range(1, level): numerator =3D numerator * (level - 1) + level**term denominator =3D level**level print(f"L{level:<2} {'Brett (Python 3.9):':25} {numerator}") EOF printf 'L%-2d %-25s %.0f\n' \ $level 'denominator (zsh):' "$(( denominator ))" python3.9 - $level <<\EOF import sys level =3D int(sys.argv[1]) print(f"L{level:<2} denominator (Python 3.9): {level**level}") EOF done % zsh /tmp/demo.zsh L1 Ray (zsh): 1 L1 Ray (Python 3.9): 1 L1 Brett (zsh): 1 L1 Brett (Python 3.9): 1 L1 denominator (zsh): 1 L1 denominator (Python 3.9): 1 L2 Ray (zsh): 3 L2 Ray (Python 3.9): 3 L2 Brett (zsh): 3 L2 Brett (Python 3.9): 3 L2 denominator (zsh): 4 L2 denominator (Python 3.9): 4 L3 Ray (zsh): 19 L3 Ray (Python 3.9): 19 L3 Brett (zsh): 19 L3 Brett (Python 3.9): 19 L3 denominator (zsh): 27 L3 denominator (Python 3.9): 27 L4 Ray (zsh): 175 L4 Ray (Python 3.9): 175 L4 Brett (zsh): 175 L4 Brett (Python 3.9): 175 L4 denominator (zsh): 256 L4 denominator (Python 3.9): 256 L5 Ray (zsh): 2101 L5 Ray (Python 3.9): 2101 L5 Brett (zsh): 2101 L5 Brett (Python 3.9): 2101 L5 denominator (zsh): 3125 L5 denominator (Python 3.9): 3125 L6 Ray (zsh): 31031 L6 Ray (Python 3.9): 31031 L6 Brett (zsh): 31031 L6 Brett (Python 3.9): 31031 L6 denominator (zsh): 46656 L6 denominator (Python 3.9): 46656 L7 Ray (zsh): 543607 L7 Ray (Python 3.9): 543607 L7 Brett (zsh): 543607 L7 Brett (Python 3.9): 543607 L7 denominator (zsh): 823543 L7 denominator (Python 3.9): 823543 L8 Ray (zsh): 11012415 L8 Ray (Python 3.9): 11012415 L8 Brett (zsh): 11012415 L8 Brett (Python 3.9): 11012415 L8 denominator (zsh): 16777216 L8 denominator (Python 3.9): 16777216 L9 Ray (zsh): 253202761 L9 Ray (Python 3.9): 253202761 L9 Brett (zsh): 253202761 L9 Brett (Python 3.9): 253202761 L9 denominator (zsh): 387420489 L9 denominator (Python 3.9): 387420489 L10 Ray (zsh): 6513215599 L10 Ray (Python 3.9): 6513215599 L10 Brett (zsh): 6513215599 L10 Brett (Python 3.9): 6513215599 L10 denominator (zsh): 10000000000 L10 denominator (Python 3.9): 10000000000 L11 Ray (zsh): 185311670611 L11 Ray (Python 3.9): 185311670611 L11 Brett (zsh): 185311670611 L11 Brett (Python 3.9): 185311670611 L11 denominator (zsh): 285311670611 L11 denominator (Python 3.9): 285311670611 L12 Ray (zsh): 5777672071535 L12 Ray (Python 3.9): 5777672071535 L12 Brett (zsh): 5777672071535 L12 Brett (Python 3.9): 5777672071535 L12 denominator (zsh): 8916100448256 L12 denominator (Python 3.9): 8916100448256 L13 Ray (zsh): 195881901213181 L13 Ray (Python 3.9): 195881901213181 L13 Brett (zsh): 195881901213181 L13 Brett (Python 3.9): 195881901213181 L13 denominator (zsh): 302875106592253 L13 denominator (Python 3.9): 302875106592253 L14 Ray (zsh): 7174630439858727 L14 Ray (Python 3.9): 7174630439858727 L14 Brett (zsh): 7174630439858727 L14 Brett (Python 3.9): 7174630439858727 L14 denominator (zsh): 11112006825558016 L14 denominator (Python 3.9): 11112006825558016 L15 Ray (zsh): 282325794823047136 L15 Ray (Python 3.9): 282325794823047151 L15 Brett (zsh): 282325794823047136 L15 Brett (Python 3.9): 282325794823047151 L15 denominator (zsh): 437893890380859392 L15 denominator (Python 3.9): 437893890380859375 L16 Ray (zsh): 11878335717996660736 L16 Ray (Python 3.9): 11878335717996660991 L16 Brett (zsh): 11878335717996660736 L16 Brett (Python 3.9): 11878335717996660991 L16 denominator (zsh): 18446744073709551616 L16 denominator (Python 3.9): 18446744073709551616 L17 Ray (zsh): 532092356706984001536 L17 Ray (Python 3.9): 532092356706983938321 L17 Brett (zsh): 532092356706984001536 L17 Brett (Python 3.9): 532092356706983938321 L17 denominator (zsh): 827240261886336827392 L17 denominator (Python 3.9): 827240261886336764177 L18 Ray (zsh): 25283323623228810723328 L18 Ray (Python 3.9): 25283323623228812584415 L18 Brett (zsh): 25283323623228810723328 L18 Brett (Python 3.9): 25283323623228812584415 L18 denominator (zsh): 39346408075296541507584 L18 denominator (Python 3.9): 39346408075296537575424 L19 Ray (zsh): 1270184310304975863414784 L19 Ray (Python 3.9): 1270184310304975912766347 L19 Brett (zsh): 1270184310304975863414784 L19 Brett (Python 3.9): 1270184310304975912766347 L19 denominator (zsh): 1978419655660313627328512 L19 denominator (Python 3.9): 1978419655660313589123979 L20 Ray (zsh): 67267626542454044570419200 L20 Ray (Python 3.9): 67267626542454041806644399 L20 Brett (zsh): 67267626542454044570419200 L20 Brett (Python 3.9): 67267626542454041806644399 L20 denominator (zsh): 104857600000000000000000000 L20 denominator (Python 3.9): 104857600000000000000000000 L21 Ray (zsh): 3745435018385981790358601728 L21 Ray (Python 3.9): 3745435018385982521381124421 L21 Brett (zsh): 3745435018385981790358601728 L21 Brett (Python 3.9): 3745435018385982521381124421 L21 denominator (zsh): 5842587018385982340114415616 L21 denominator (Python 3.9): 5842587018385982521381124421 L22 Ray (zsh): 218733549978113966650274349056 L22 Ray (Python 3.9): 218733549978113924447643110743 L22 Brett (zsh): 218733549978113931465902260224 L22 Brett (Python 3.9): 218733549978113924447643110743 L22 denominator (zsh): 341427877364219559508793360384 L22 denominator (Python 3.9): 341427877364219557396646723584