Incorporate C library function into Perl6 with NativeCall

Multi tool use
I am attempting to use lgamma
from C's math.h
in Perl6.
How can I incorporate this into Perl6?
I have tried
use NativeCall;
sub lgamma(num64 --> num64) is native(Str) {};
say lgamma(3e0);
my $x = 3.14;
say lgamma($x);
This works for the first number (a Str
) but fails for the second, $x
, giving the error:
This type cannot unbox to a native number: P6opaque, Rat
in block <unit> at pvalue.p6 line 8
I want to do this very simply, like in Perl5: use POSIX 'lgamma';
and then lgamma($x)
but I don't see how to do that in Perl6.
perl6 nativecall
add a comment |
I am attempting to use lgamma
from C's math.h
in Perl6.
How can I incorporate this into Perl6?
I have tried
use NativeCall;
sub lgamma(num64 --> num64) is native(Str) {};
say lgamma(3e0);
my $x = 3.14;
say lgamma($x);
This works for the first number (a Str
) but fails for the second, $x
, giving the error:
This type cannot unbox to a native number: P6opaque, Rat
in block <unit> at pvalue.p6 line 8
I want to do this very simply, like in Perl5: use POSIX 'lgamma';
and then lgamma($x)
but I don't see how to do that in Perl6.
perl6 nativecall
1
Can you please post the whole error?
– jjmerelo
6 hours ago
1
@jjmerelo I've updated the post to more completely show the error
– con
2 hours ago
1
Try using thenum64
native type when declaring$x
:my num64 $x = 3.14.Num
. It also seems to work without thenum64
declarator:my $x = 3.14.Num
– Håkon Hægland
2 hours ago
add a comment |
I am attempting to use lgamma
from C's math.h
in Perl6.
How can I incorporate this into Perl6?
I have tried
use NativeCall;
sub lgamma(num64 --> num64) is native(Str) {};
say lgamma(3e0);
my $x = 3.14;
say lgamma($x);
This works for the first number (a Str
) but fails for the second, $x
, giving the error:
This type cannot unbox to a native number: P6opaque, Rat
in block <unit> at pvalue.p6 line 8
I want to do this very simply, like in Perl5: use POSIX 'lgamma';
and then lgamma($x)
but I don't see how to do that in Perl6.
perl6 nativecall
I am attempting to use lgamma
from C's math.h
in Perl6.
How can I incorporate this into Perl6?
I have tried
use NativeCall;
sub lgamma(num64 --> num64) is native(Str) {};
say lgamma(3e0);
my $x = 3.14;
say lgamma($x);
This works for the first number (a Str
) but fails for the second, $x
, giving the error:
This type cannot unbox to a native number: P6opaque, Rat
in block <unit> at pvalue.p6 line 8
I want to do this very simply, like in Perl5: use POSIX 'lgamma';
and then lgamma($x)
but I don't see how to do that in Perl6.
perl6 nativecall
perl6 nativecall
edited 2 hours ago
asked 11 hours ago
con
318210
318210
1
Can you please post the whole error?
– jjmerelo
6 hours ago
1
@jjmerelo I've updated the post to more completely show the error
– con
2 hours ago
1
Try using thenum64
native type when declaring$x
:my num64 $x = 3.14.Num
. It also seems to work without thenum64
declarator:my $x = 3.14.Num
– Håkon Hægland
2 hours ago
add a comment |
1
Can you please post the whole error?
– jjmerelo
6 hours ago
1
@jjmerelo I've updated the post to more completely show the error
– con
2 hours ago
1
Try using thenum64
native type when declaring$x
:my num64 $x = 3.14.Num
. It also seems to work without thenum64
declarator:my $x = 3.14.Num
– Håkon Hægland
2 hours ago
1
1
Can you please post the whole error?
– jjmerelo
6 hours ago
Can you please post the whole error?
– jjmerelo
6 hours ago
1
1
@jjmerelo I've updated the post to more completely show the error
– con
2 hours ago
@jjmerelo I've updated the post to more completely show the error
– con
2 hours ago
1
1
Try using the
num64
native type when declaring $x
: my num64 $x = 3.14.Num
. It also seems to work without the num64
declarator: my $x = 3.14.Num
– Håkon Hægland
2 hours ago
Try using the
num64
native type when declaring $x
: my num64 $x = 3.14.Num
. It also seems to work without the num64
declarator: my $x = 3.14.Num
– Håkon Hægland
2 hours ago
add a comment |
1 Answer
1
active
oldest
votes
Your $x
has no type. If you use any type for it, say num64
, it will say:
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)
So you do exactly that:
my num64 $x = 3.14.Num;
This converts the number exactly to the representation that is required by lgamma
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53939570%2fincorporate-c-library-function-into-perl6-with-nativecall%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your $x
has no type. If you use any type for it, say num64
, it will say:
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)
So you do exactly that:
my num64 $x = 3.14.Num;
This converts the number exactly to the representation that is required by lgamma
add a comment |
Your $x
has no type. If you use any type for it, say num64
, it will say:
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)
So you do exactly that:
my num64 $x = 3.14.Num;
This converts the number exactly to the representation that is required by lgamma
add a comment |
Your $x
has no type. If you use any type for it, say num64
, it will say:
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)
So you do exactly that:
my num64 $x = 3.14.Num;
This converts the number exactly to the representation that is required by lgamma
Your $x
has no type. If you use any type for it, say num64
, it will say:
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)
So you do exactly that:
my num64 $x = 3.14.Num;
This converts the number exactly to the representation that is required by lgamma
answered 2 hours ago
jjmerelo
5,02531645
5,02531645
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53939570%2fincorporate-c-library-function-into-perl6-with-nativecall%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
pVcuP4OD5Z9,J DIx,Qwxss0SVcszuxI19nHlUZoqm3 8pAdugW4DPoz62iAKhIyMD9vAcg6
1
Can you please post the whole error?
– jjmerelo
6 hours ago
1
@jjmerelo I've updated the post to more completely show the error
– con
2 hours ago
1
Try using the
num64
native type when declaring$x
:my num64 $x = 3.14.Num
. It also seems to work without thenum64
declarator:my $x = 3.14.Num
– Håkon Hægland
2 hours ago