我怎样才能让这个rakuMullerRecurrenceOne-Liner工作?
在对我最近的博客文章的回复中,Markus H. 回复了一个非常简洁的代码替代方案,即:
dd $_ for (4, 4.25, 108 – (815 – 1500 / * ) / * … *)[^30].kv
可悲的是,我无法让它“开箱即用”并得到这个错误:
Confused
at /Users/stephenroe/Dropbox/RakuStuff/mullerrec/./mullerrec3.raku:27
------> dd $_ for (4, 4.25, 108? – (815 – 1500 / * ) / * … *)[^30].kv
expecting any of:
infix
infix stopper
statement end
statement modifier
statement modifier loop
我怎样才能解决这个问题?
这是我可以开始工作的代码(是的,我的风格有点不同,但没关系):
sub f(y,z) {
108 - ( (815 - 1500/z ) / y )
}
dd $_ for (4, 4.25, -> z,y {f(y,z)} … ?)[^30].kv;
作为跟进,我想我需要将参数交换为 f,但 Markus 没有这样做,谁是对的?我很想知道这是否是一个问题,然后问题是“我怎样才能颠倒消耗任何星星的顺序”?
或者可能是这样的:
dd $_ for (4, 4.25, f(^z,^y) … ?)[^30].kv; #not legal
回答
TL;DR在非 Unicode 字符集时代的剪切/粘贴令人绝望并且变得越来越糟——但至少它通常是大声的,所以有人得到了 mojibake,很明显事情出了问题。在 Unicode 时代,这是一种新的球赛。你需要s/–/-/。
进行该更改,但也进行了一些其他更改以切换到更有洞察力的显示,并切换到FatRat计算,以便结果100% 准确且精度无限:
say sprintf "%-2s: %-24s %s / %s", .key, .value, |.value.nude
for (4.FatRat, 4.25, 108 - (815 - 1500 / * ) / * … *)[^30].pairs
0 : 4 4 / 1
1 : 4.25 17 / 4
2 : 4.470588 76 / 17
3 : 4.644737 353 / 76
4 : 4.770538 1684 / 353
5 : 4.855701 8177 / 1684
6 : 4.910847 40156 / 8177
7 : 4.945537 198593 / 40156
8 : 4.96696258 986404 / 198593
9 : 4.9800457 4912337 / 986404
10: 4.987979448 24502636 / 4912337
11: 4.9927702881 122336033 / 24502636
12: 4.99565589151 611148724 / 122336033
13: 4.99739126838 3054149297 / 611148724
14: 4.998433943945 15265963516 / 3054149297
15: 4.9990600719709 76315468673 / 15265963516
16: 4.9994359371468 381534296644 / 76315468673
17: 4.99966152410377 1907542343057 / 381534296644
18: 4.999796900713418 9537324294796 / 1907542343057
19: 4.999878135477931 47685459212513 / 9537324294796
20: 4.9999268795045999 238423809278164 / 47685459212513
21: 4.99995612706115774 1192108586037617 / 238423809278164
22: 4.999973676005712445 5960511549128476 / 1192108586037617
23: 4.999984205520272708 29802463602463553 / 5960511549128476
24: 4.9999905232822276594 149012035582781284 / 29802463602463553
25: 4.99999431395855959365 745059330625296977 / 149012035582781284
26: 4.99999658837125602371 3725294111260656556 / 745059330625296977
27: 4.999997953021356907988 18626462930705797793 / 3725294111260656556
28: 4.9999987718123113299994 93132291776736534004 / 18626462930705797793
29: 4.9999992630872057845553 465661390253305305137 / 93132291776736534004
穆勒循环是一个近似值。但是给定迭代的准确度/精度由先前迭代的准确度/精度决定。这些反过来又受数字类型和使用的操作的准确性/精度的影响:
-
使用浮点数/运算在第 12 次左右迭代后会产生无用的结果。
-
只要数字保持在其精度范围内,使用定点数字/运算就会产生准确的结果,然后很快就会产生无用的结果。
-
在我的@Holli 代码变体中,我使用了 Raku 的
FatRat数字类型和操作。这保持了 100% 的准确度和无限的精度。即使是便宜的硬件也可以在不到一秒的时间内轻松地以 100% 的准确度进行 1,000 次迭代。
穆勒循环公式在数学上仅涉及有理数和运算(“有理数”包括整数)。Raku 中的数字运算方便地遵循“感染”规则,如果:
-
每次迭代中至少有一个数字是
FatRat; -
没有一个数字是
Num(浮点数); -
没有操作引入
Num(浮点);
那么每次迭代都会产生另一个 100% 准确的FatRat。
当按照 Raku 中的原样使用 Muller 的循环公式时,这是正确的,前提是其中一个数字被强制转换为FatRat。
- dear raiph - that is very helpful - thanks! (although a bit embarrassing that I cannot cut & paste unicode ... @markus was right about the non reversal ... as future reference, can one reverse whatever stars?
- @p6steve No, you can't reverse stars. You have to use braces. For example `4, 4.25.FatRat, { 108 - (815 - 1500 / $^foo ) / $^bar } … *` will reverse the parameters/arguments because `bar` sorts lexicographically before `foo`.
- I've added a PR today to introduce a `$*RAT-UPGRADE-CLASS` dynamic variable, that allows one to indicate how upgrading from `Rat` can be handled: https://github.com/rakudo/rakudo/pull/4299 . With that, you could leave out the `4.FatRat` when you would add `my $*RAT-UPGRADE-CLASS = FatRat` to your code (which would cause an upgrade to `FatRat` rather than a downgrade to `Num` when the denominator exceeds 64 bits).