我正在尝试使用C's lgamma中的math.h中的Perl6。
我如何将它合并到Perl6中呢?
我试过了
use NativeCall;
sub lgamma(num64 --> num64) is native(Str) {};
say lgamma(3e0);
my $x = 3.14;
say lgamma($x);这对于第一个数字(一个Str)有效,但是对于第二个数字,$x失败,给出了错误:
This type cannot unbox to a native number: P6opaque, Rat
in block <unit> at pvalue.p6 line 8我想简单地这样做,就像在Perl5:use POSIX 'lgamma';,然后是lgamma($x),但是我不知道如何在Perl6中这样做。
发布于 2018-12-27 19:43:10
带有本机值的错误并不总是很清楚。
基本上,它是说老鼠不是一个名词。
3.14是一只老鼠。(理性)
say 3.14.^name; # Rat
say 3.14.nude.join('/'); # 157/50每次你叫它的时候,你都可以强迫它的价值为Num。
lgamma( $x.Num )看上去不太好。
我只想把本地的子包在另一个,强制所有的实数为Num。
(除了复数外,所有数字都是真实的)
sub lgamma ( Num(Real) \n --> Num ){
use NativeCall;
sub lgamma (num64 --> num64) is native {}
lgamma( n )
}
say lgamma(3); # 0.6931471805599453
say lgamma(3.14); # 0.8261387047770286发布于 2018-12-27 13:02:03
您的$x没有类型。如果你对它使用任何类型,比如num64,它会说:
Cannot assign a literal of type Rat (3.14) to a native variable of type num. You can declare the variable to be of type Real, or try to coerce the value with 3.14.Num or Num(3.14)所以你就是这么做的
my num64 $x = 3.14.Num;这将准确地将数字转换为lgamma所需的表示形式。
https://stackoverflow.com/questions/53939570
复制相似问题