Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
arithmetic anomaly
- X-seq: zsh-users 26601
- From: Ray Andrews <rayandrews@xxxxxxxxxxx>
- To: Zsh Users <zsh-users@xxxxxxx>
- Subject: arithmetic anomaly
- Date: Thu, 25 Mar 2021 08:52:32 -0700
- Archived-at: <https://zsh.org/users/26601>
- Archived-at: <http://www.zsh.org/sympa/arcsearch_id/zsh-users/2021-03/0227b30d-4f09-2c8b-1248-557859d875ce%40eastlink.ca>
- List-id: <zsh-users.zsh.org>
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.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author