From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Tim Meehan Newsgroups: gmane.lisp.guile.user Subject: Re: C extensions Date: Sun, 21 Feb 2021 16:18:38 -0600 Message-ID: References: <87zgzx5s3d.fsf@clara> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="1228"; mail-complaints-to="usenet@ciao.gmane.io" Cc: guile-user Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sun Feb 21 23:19:02 2021 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lDx4A-0000CM-Kz for guile-user@m.gmane-mx.org; Sun, 21 Feb 2021 23:19:02 +0100 Original-Received: from localhost ([::1]:57300 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lDx49-0004mx-Kw for guile-user@m.gmane-mx.org; Sun, 21 Feb 2021 17:19:01 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:59418) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lDx40-0004mk-6A for guile-user@gnu.org; Sun, 21 Feb 2021 17:18:52 -0500 Original-Received: from mail-qt1-x836.google.com ([2607:f8b0:4864:20::836]:33499) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lDx3y-0000oH-0X for guile-user@gnu.org; Sun, 21 Feb 2021 17:18:51 -0500 Original-Received: by mail-qt1-x836.google.com with SMTP id z6so7484976qts.0 for ; Sun, 21 Feb 2021 14:18:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:cc; bh=VJ/rXI94nCBmQuZ4dfflf8lxiTYBIxTz2zMgO279enU=; b=GuRQYNGzPQEIsjhAICK8C9MGdvhIt2A9RdWEn5HCCtolgeRUbmPrP4N3BsIom3rSzh MNddncj5rMXIehUFzCl8UOFmNP72zMAZomylPtKjchpKZ5DYTSKZhPRlydEVfbOuK9Zr fcutseUyuQBzvV+5isBZzGYnepXvfkWpILmseyIZI5zEbsojszEpzupiJcDakhW43hQG 0EdZ3Tx945PoYUglbNqwnBjiioeZxfAJ7bHMUufE+x3OwSsMdcrm9FgZfDNXGmtPqh6T Wnwz3jI2wcc0tnPXoUGzkR7dGlGzJuX25PLdT2OmemsxJ5i+CwMx0dey5k9NKS7bnQIH 5BWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:cc; bh=VJ/rXI94nCBmQuZ4dfflf8lxiTYBIxTz2zMgO279enU=; b=ok2XGs3FLPFDLlPTTE+uDzF5XxHRQ8mSaSbIJYDh+AxDUiWywQcFyqYcoVM4FQjFCI ori1YnVomwzsOFZrsA4WOygjw20zcbs3HNeqLJkSRQUxmXqBgfdu4GhhdFgRqiG1gXpP QTA++4MfZgoULcmyGDkV7Hf1IElu/nXixV60zKgvGJixAzsq9bS4O5lsCFxdLdhgnJmR W4hFUz6WvERoH6xv7+QQLSYv5sEAWcTdWxayriqt6LrvmyrHoiNENWrQvUMngPj2TrEt BcF9ICNVX00dPWwELObazNc6c4aDlNv/aMXirGOR+7+S7AFLbq8a407Pzn2N5SW6Ckut YaKA== X-Gm-Message-State: AOAM530eyBMeH+yxJfKxvj3o6DvohSMnXICk1QJYXsG2iGFKJJH/86s1 rqGz6TCRpyaRxeIzd8JCkeREZqRuo71KFXkXrLA2AmjJaGw= X-Google-Smtp-Source: ABdhPJyS5nY2t6DIPIqWgPujEBx7oFV7fE1XAtbGCN2KjvdJ7WR/iQZYAAFItSDPysqtRgoSDagWIp+A8OSUP1xF8jk= X-Received: by 2002:ac8:7b4b:: with SMTP id m11mr18015425qtu.208.1613945928714; Sun, 21 Feb 2021 14:18:48 -0800 (PST) In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::836; envelope-from=btmeehan@gmail.com; helo=mail-qt1-x836.google.com X-Spam_score_int: 4 X-Spam_score: 0.4 X-Spam_bar: / X-Spam_report: (0.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, MALFORMED_FREEMAIL=1.49, MISSING_HEADERS=1.021, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-Content-Filtered-By: Mailman/MimeDel 2.1.23 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:17299 Archived-At: I suppose that I could use something like "scm_wrong_type_arg_msg" ... which gives better looking feedback than "#f": scheme@(guile-user)> (random:lognormal 0 -1) ice-9/boot-9.scm:1669:16: In procedure raise-exception: In procedure random:lognormal: Wrong type argument in position 2 (expecting standard deviation): -1 On Sun, Feb 21, 2021 at 3:58 PM Tim Meehan wrote: > Hello Olivier, thanks! > Those functions helped, and now I know where in the source of Guile to > look. > > Is "inexact" the same thing as "floating point number" in Guile-speak? > > Here is one of the functions in my extension - it returns a lognormal > variate. There are a bunch of things like this that could be added to > "random.c" - but I'm imagining that the only user of stuff like that might > be ... me ... anyhow: > > Right now, it doesn't give really helpful errors when you give it a > negative standard deviation: > scheme@(guile-user)> (random:lognormal 0 -1) > $1 = #f > > SCM_DEFINE (scm_random_lognormal, "random:lognormal", 2, 1, 0, > (SCM _mu, SCM _sigma, SCM state), > "Return an inexact real in a Log Normal distribution.\n" > "\n" > "@var{mu} - mean of the underlying normal distribution.\n" > "@var{sigma} - standard deviation of the same.\n" > "Optionally, a random @var{state} can be provided.\n" > "See @code{seed->random-state}.") > #define FUNC_NAME s_scm_random_lognormal > { > if (SCM_UNBNDP (state)) > state = SCM_VARIABLE_REF (scm_var_random_state); > SCM_VALIDATE_RSTATE (1, state); > > if (scm_is_false (scm_positive_p (_sigma))) > return SCM_BOOL_F; // I'd like to return some meaningful error here ... > > double mu = scm_to_double (_mu); > double sigma = scm_to_double (_sigma); > return scm_from_double (exp (mu + sigma*scm_c_normal01 > (SCM_RSTATE(state)))); > } > #undef FUNC_NAME > > On Sun, Feb 21, 2021 at 9:59 AM Olivier Dion > wrote: > >> On Sat, 20 Feb 2021, Tim Meehan wrote: >> > I'm trying my hand a writing C extensions. >> > I've done this for stuff like Matlab before, and was wondering how you >> do >> > the usual checking of the arguments that are passed in from Guile. >> > >> > In the manual, 6.13.13.1 "C Support" has a few functions. >> > libguile/numbers.h has a bunch more ... >> > >> > What I have is an extension function, sort of like the bessel function >> in >> > the tutorial: >> > >> https://www.gnu.org/software/guile/manual/html_node/A-Sample-Guile-Extension.html >> >> Usually I do this in 2 or 3 steps. >> >> 1. Define the primitive in C with raw arguments. >> >> 2. Make a wrapper of this primitive for Guile to use. Optionnaly do >> the arguments checking here or go to step 3. >> >> 3. Make a second wrapper in Scheme and check the arguments there. >> This wrapper will call wrapper in made in 2. >> >> Here's a full example: >> ---------------------------------------------------------------------- >> float c_sin(double x) >> { >> return sin(x); >> } >> >> SCM_DEFINE(c_sin_wrapper, "sin", 1, 0, 0, >> (SCM x), >> "My sine") >> { >> if (scm_is_number(x)) { >> return c_sin(scm_to_double(x)); >> } >> >> return SCM_BOOL_F; >> } >> ---------------------------------------------------------------------- >> >> > What I would like to do is verify that the first argument is an inexact >> > number, larger than 0. How would I go about that? Perhaps some of it >> > could >> >> For your example, use the following predicates: >> ---------------------------------------------------------------------- >> scm_is_inexact(x) && scm_is_true(scm_positive_p(x)) >> ---------------------------------------------------------------------- >> >> > be: >> > SCM_REALP >> > scm_misc_error >> > >> > Any tips? >> -- >> Olivier Dion >> PolyMtl >> >