```zsh-users
help / color / mirror / code / Atom feed```
```From: Ray Andrews <rayandrews@eastlink.ca>
To: Zsh Users <zsh-users@zsh.org>
Subject: arithmetic anomaly
Date: Thu, 25 Mar 2021 08:52:32 -0700	[thread overview]

So my nephew and I came up with different algorithms for working out a
probability problem.  Here's both of them in a script and the output:

#!/bin/zsh

setopt force_float
integer level=
typeset -F term=
typeset -F numerator=
typeset -F denominator=

for ((level=1; level<23; level++)); do

# Ray:
numerator=
for ((term=1; term <= level; term++)); do
(( numerator += (level**(term - 1)) * (level - 1)**(level -
term) ))
done
(( denominator = level**level ))
printf "%.0f / %.0f \n" "\$(( numerator ))" "\$(( denominator ))"

# Brett:
numerator=1
for ((term=1; term < level; term++)); do
(( numerator = numerator * (level - 1) + level**term ))
done
(( denominator = level**level ))
printf "%.0f / %.0f \n" "\$(( numerator ))" "\$(( denominator ))"

done

=========================================

1 / 1
1 / 1
3 / 4
3 / 4
19 / 27
19 / 27
175 / 256
175 / 256
2101 / 3125
2101 / 3125
31031 / 46656
31031 / 46656
543607 / 823543
543607 / 823543
11012415 / 16777216
11012415 / 16777216
253202761 / 387420489
253202761 / 387420489
6513215599 / 10000000000
6513215599 / 10000000000
185311670611 / 285311670611
185311670611 / 285311670611
5777672071535 / 8916100448256
5777672071535 / 8916100448256
195881901213181 / 302875106592253
195881901213181 / 302875106592253
7174630439858727 / 11112006825558016
7174630439858727 / 11112006825558016
282325794823047136 / 437893890380859392
282325794823047136 / 437893890380859392
11878335717996660736 / 18446744073709551616
11878335717996660736 / 18446744073709551616
532092356706984001536 / 827240261886336827392
532092356706984001536 / 827240261886336827392
25283323623228810723328 / 39346408075296541507584
25283323623228810723328 / 39346408075296541507584
1270184310304975863414784 / 1978419655660313627328512
1270184310304975863414784 / 1978419655660313627328512
67267626542454044570419200 / 104857600000000000000000000
67267626542454044570419200 / 104857600000000000000000000
3745435018385981790358601728 / 5842587018385982340114415616
3745435018385981790358601728 / 5842587018385982340114415616
218733549978113966650274349056 / 341427877364219559508793360384
218733549978113931465902260224 / 341427877364219559508793360384
#               ^  Ooops!

... both work.  But look at the last pair. The two diverge in the
numerator, why is that?  If there was some rounding of fractions or
something I'd understand that an error would eventually pop up, but
there's no divisions, only whole number integer arithmetic. 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.

```

```next             reply	other threads:[~2021-03-25 15:53 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-25 15:52 Ray Andrews [this message]
2021-03-25 20:40 ` Lawrence Velázquez
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,

Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

switches of git-send-email(1):

git send-email \
--cc=zsh-users@zsh.org \

https://kernel.org/pub/software/scm/git/docs/git-send-email.html

```Code repositories for project(s) associated with this public inbox