gpt4 book ai didi

perl - 如何设置Raku的sqrt的精确度?

转载 作者:行者123 更新时间:2023-12-03 15:37:32 33 4
gpt4 key购买 nike

使用Perl,可以使用bignum为所有运算符设置精度级别。如:

use bignum ( p => -50 );

print sqrt(20); # 4.47213595499957939281834733746255247088123671922305
在Raku中,我可以使用 Rat/ FatRat,因此在理性方面没有任何问题,但是我不知道如何为sqrt使用更长的精度
say 20.sqrt # 4.47213595499958

最佳答案

如伊丽莎白的回答所述,sqrt返回Num类型,因此精度有限。有关更多详细信息,请参见伊丽莎白的answer
因此,我创建了一个raku类:BigRoot,该类使用牛顿的方法和FatRat类型来计算根。您可以这样使用它:

use BigRoot;

# Can change precision level (Default precision is 30)
BigRoot.precision = 50;

my $root2 = BigRoot.newton's-sqrt: 2;
# 1.41421356237309504880168872420969807856967187537695

say $root2.WHAT;
# (FatRat)

# Can use other root numbers
say BigRoot.newton's-root: root => 3, number => 30;
# 3.10723250595385886687766242752238636285490682906742

# Numbers can be Int, Rational and Num:
say BigRoot.newton's-sqrt: 2.123;
# 1.45705181788431944566113502812562734420538186940001

# Can use other rational roots
say BigRoot.newton's-root: root => FatRat.new(2, 3), number => 30;
# 164.31676725154983403709093484024064018582340849939498

# Results are rounded:

BigRoot.precision = 8;
say BigRoot.newton's-sqrt: 2;
# 1.41421356

BigRoot.precision = 7;
say BigRoot.newton's-sqrt: 2;
# 1.4142136
总的来说,它看起来非常快(至少与Perl的bigfloat相比)
基准测试:
|---------------------------------------|-------------|------------|
| sqrt with 10_000 precision digits | Raku | Perl |
|---------------------------------------|-------------|------------|
| 20000000000 | 0.714 | 3.713 |
|---------------------------------------|-------------|------------|
| 200000.1234 | 1.078 | 4.269 |
|---------------------------------------|-------------|------------|
| π | 0.879 | 3.677 |
|---------------------------------------|-------------|------------|
| 123.9/12.29 | 0.871 | 9.667 |
|---------------------------------------|-------------|------------|
| 999999999999999999999999999999999 | 1.208 | 3.937 |
|---------------------------------------|-------------|------------|
| 302187301.3727 / 123.30219380928137 | 1.528 | 7.587 |
|---------------------------------------|-------------|------------|
| 2 + 999999999999 ** 10 | 2.193 | 3.616 |
|---------------------------------------|-------------|------------|
| 91200937373737999999997301.3727 / π | 1.076 | 7.419 |
|---------------------------------------|-------------|------------|
如果要使用牛顿方法实现自己的 sqrt,请遵循以下基本思想:
sub newtons-sqrt(:$number, :$precision) returns FatRat {
my FatRat $error = FatRat.new: 1, 10 ** ($precision + 1);
my FatRat $guess = (sqrt $number).FatRat;
my FatRat $input = $number.FatRat;
my FatRat $diff = $input;

while $diff > $error {
my FatRat $new-guess = $guess - (($guess ** 2 - $input) / (2 * $guess));
$diff = abs($new-guess - $guess);
$guess = $new-guess;
}

return $guess.round: FatRat.new: 1, 10 ** $precision;
}

关于perl - 如何设置Raku的sqrt的精确度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63484219/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com