From: "Lawrence Velázquez" <vq@larryv.me>
To: Ray Andrews <rayandrews@eastlink.ca>
Cc: zsh-users@zsh.org
Subject: Re: arithmetic anomaly
Date: Thu, 25 Mar 2021 16:40:34 -0400 [thread overview]
Message-ID: <79FD66A8-76A5-4F76-9F3A-D6481F920862@larryv.me> (raw)
In-Reply-To: <0227b30d-4f09-2c8b-1248-557859d875ce@eastlink.ca>
> On Mar 25, 2021, at 11:52 AM, Ray Andrews <rayandrews@eastlink.ca> wrote:
>
> 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=15 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=
typeset -F term=
typeset -F numerator=
typeset -F denominator=
for ((level=1; level<23; level++)); do
numerator=
for ((term=1; term <= level; term++)); do
(( numerator += (level**(term - 1)) * (level - 1)**(level - term) ))
done
(( denominator = level**level ))
printf 'L%-2d %-25s %.0f\n' $level 'Ray (zsh):' "$(( numerator ))"
python3.9 - $level <<\EOF
import sys
level = int(sys.argv[1])
numerator = 0
for term in range(1, level+1):
numerator += (level**(term - 1)) * (level - 1)**(level - term)
denominator = level**level
print(f"L{level:<2} {'Ray (Python 3.9):':25} {numerator}")
EOF
numerator=1
for ((term=1; term < level; term++)); do
(( numerator = numerator * (level - 1) + level**term ))
done
(( denominator = level**level ))
printf 'L%-2d %-25s %.0f\n' level 'Brett (zsh):' "$(( numerator ))"
python3.9 - $level <<\EOF
import sys
level = int(sys.argv[1])
numerator = 1
for term in range(1, level):
numerator = numerator * (level - 1) + level**term
denominator = 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 = 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
next prev parent reply other threads:[~2021-03-25 20:41 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-03-25 15:52 Ray Andrews
2021-03-25 20:40 ` Lawrence Velázquez [this message]
2021-03-25 22:44 ` Ray Andrews
2021-03-25 22:54 ` Roman Perepelitsa
2021-03-25 23:57 ` Ray Andrews
2021-03-26 0:16 ` Oliver Kiddle
2021-03-26 0:37 ` Ray Andrews
2021-03-26 10:35 ` Roman Perepelitsa
2021-03-26 14:36 ` Ray Andrews
2021-03-26 14:49 ` Roman Perepelitsa
2021-03-26 15:35 ` Ray Andrews
2021-03-26 17:09 ` Lawrence Velázquez
2021-03-26 21:45 ` Ray Andrews
2021-03-26 21:59 ` Lawrence Velázquez
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=79FD66A8-76A5-4F76-9F3A-D6481F920862@larryv.me \
--to=vq@larryv.me \
--cc=rayandrews@eastlink.ca \
--cc=zsh-users@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).