From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.comp.tex.context/99840 Path: news.gmane.org!.POSTED!not-for-mail From: Joseph Canedo Newsgroups: gmane.comp.tex.context Subject: Re: Problem with string.format (Lua) Date: Sun, 14 Jan 2018 13:13:54 +0100 Message-ID: <5a5b4982.39ba500a.ab110.6848@mx.google.com> References: Reply-To: mailing list for ConTeXt users NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============6033448865084182512==" X-Trace: blaine.gmane.org 1515931946 1634 195.159.176.226 (14 Jan 2018 12:12:26 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 14 Jan 2018 12:12:26 +0000 (UTC) To: mailing list for ConTeXt users Original-X-From: ntg-context-bounces@ntg.nl Sun Jan 14 13:12:22 2018 Return-path: Envelope-to: gctc-ntg-context-518@m.gmane.org Original-Received: from zapf.boekplan.nl ([5.39.185.232] helo=zapf.ntg.nl) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eah9A-0008PT-PA for gctc-ntg-context-518@m.gmane.org; Sun, 14 Jan 2018 13:12:20 +0100 Original-Received: from localhost (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id 75E668A0DA0; Sun, 14 Jan 2018 13:14:09 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at zapf.boekplan.nl Original-Received: from zapf.ntg.nl ([127.0.0.1]) by localhost (zapf.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Ngl9ts_0dldP; Sun, 14 Jan 2018 13:14:08 +0100 (CET) Original-Received: from zapf.ntg.nl (localhost [IPv6:::1]) by zapf.ntg.nl (Postfix) with ESMTP id B97B78A1B7C; Sun, 14 Jan 2018 13:14:08 +0100 (CET) Original-Received: from localhost (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id 568998A1B7C for ; Sun, 14 Jan 2018 13:14:06 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at zapf.boekplan.nl Original-Received: from zapf.ntg.nl ([127.0.0.1]) by localhost (zapf.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id lIkB-NYc9c_J for ; Sun, 14 Jan 2018 13:14:05 +0100 (CET) Original-Received: from mail-wm0-f45.google.com (mail-wm0-f45.google.com [74.125.82.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by zapf.ntg.nl (Postfix) with ESMTPS id A1C6A8A0DA0 for ; Sun, 14 Jan 2018 13:13:55 +0100 (CET) Original-Received: by mail-wm0-f45.google.com with SMTP id f3so3680863wmc.1 for ; Sun, 14 Jan 2018 04:13:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:mime-version:to:from:subject:date:importance:in-reply-to :references; bh=lGB2P2/mvTlAVuwSlZUzE9o6Y/RpYXhkibj0uMmCteE=; b=LchpOqKQI1K+F1qh8d0jh0XcTKPCLK9uZYX4N66x3AkIgUyvRWd0X3+/euRIW1hiXb 9Sk+7aC0TaH7SZ+BNhRsnEcoepzPoU5xFA1Ni2qguyWVvoqs5OhCPswhT3WX4oOdjCi3 p1IyODRnnB3n2KPPOe5B8yObo+UZpxgZ8Ih6RKKIcuQloNGysU1pJoUoD1z8mO91WmuF Cc3w7/zgZyRH+KIoJ3s6F60QNa9dcEgatI1AA9yjFfY315onjrqOeuMkBYJhExZ8NX7n trkI/PRriO52YnteP3QOfE5cQMAWpKe3lndyJTQsF8f6fnzJkJ2yfnwEuXZZVdotOH/W SzoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:mime-version:to:from:subject:date :importance:in-reply-to:references; bh=lGB2P2/mvTlAVuwSlZUzE9o6Y/RpYXhkibj0uMmCteE=; b=MVTlyFJc20Ncvsc0iHCmcDDvdC7t/yjhbfgiWMbGBvmDFsYHT9Oh6zCYziiidwHdeI iAcjtz9BYrF4qn1bGjNLSQBxVj35J1QS0jF1Ba2WU3/1KeHArRRhDReNnr/b8hJZcuSX utw7rFNX8wzjjZQWKwh2x+/MbSw2We2yQq6AMaUTqg8Q10P4WgUOcyq9ecUs4cmE+Lnb E5KNIxluzv/Q2OSZcmheID7mggjUX4oMc77757v57YwnQIMt2v9MPc8ugPD7EXcNuzzv avbMqB6omPFmCzN7s5LxBMfzaMLlTdjrheiYV91fKOyNAAOt66mqT530hzRLmX8XYWZT W9Wg== X-Gm-Message-State: AKwxytfo1qZ7g10u1C0SgsZpvt7HYpDkxPVK9HQmqKbX2NkL4jGafgdb dzOY5j5eDI4HApdkkNJE0k4F9RXY X-Google-Smtp-Source: ACJfBovHUSLRFuixD2vC1+1GUJC337z9AcuC4ZQCXCWQOecV80cXs5pVhZ58LW5U+cYpF4+erpjYEw== X-Received: by 10.80.213.218 with SMTP id g26mr7113498edj.101.1515932035134; Sun, 14 Jan 2018 04:13:55 -0800 (PST) Original-Received: from ?IPv6:2a01:cb00:68e:300:b9fb:a194:2e61:ed47? (2a01cb00068e0300b9fba1942e61ed47.ipv6.abo.wanadoo.fr. [2a01:cb00:68e:300:b9fb:a194:2e61:ed47]) by smtp.gmail.com with ESMTPSA id g54sm2691201edc.91.2018.01.14.04.13.54 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 14 Jan 2018 04:13:54 -0800 (PST) Importance: normal X-Priority: 3 In-Reply-To: X-BeenThere: ntg-context@ntg.nl X-Mailman-Version: 2.1.16 Precedence: list List-Id: mailing list for ConTeXt users List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ntg-context-bounces@ntg.nl Original-Sender: "ntg-context" Xref: news.gmane.org gmane.comp.tex.context:99840 Archived-At: --===============6033448865084182512== Content-Type: multipart/alternative; boundary="_8A165818-60BF-47EE-B675-F9324CF7F35D_" --_8A165818-60BF-47EE-B675-F9324CF7F35D_ Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Hi Fabrice, string.format behaves more or less as C printf. You probably need to specif= y %.4g in 1st argument. Hope this helps Best Joseph De=C2=A0: Fabrice Couvreur Envoy=C3=A9 le=C2=A0:dimanche 14 janvier 2018 13:05 =C3=80=C2=A0: mailing list for ConTeXt users Objet=C2=A0:[NTG-context] Problem with string.format (Lua) Hello, I start with Lua and I try to display my result with 4 decimal places witho= ut success. Thank you Fabrice \startluacode function Binomial(n,k,p) =C2=A0=C2=A0=C2=A0 if k > n then return nil end =C2=A0=C2=A0=C2=A0 if k > n/2 then k =3D n - k end=20 =C2=A0 =C2=A0=C2=A0=C2=A0 numer, denom =3D 1, 1 =C2=A0=C2=A0=C2=A0 for i =3D 1, k do =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 numer =3D numer * ( n - i + 1 ) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 denom =3D denom * i =C2=A0=C2=A0=C2=A0 end =C2=A0=C2=A0=C2=A0 return (numer/denom) * p^k * (1-p)^(n-k) end \stopluacode \starttext \ctxlua{print(string.format("%g",Binomial(100,60,0.25)))} \stoptext --_8A165818-60BF-47EE-B675-F9324CF7F35D_ Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset="utf-8"

Hi Fabrice,

 

string.format behaves more or less = as C printf. You probably need to specify %.4g in 1st argument.

Hope this helps

 

<= p class=3DMsoNormal>Best

Joseph

 

De : Fabrice Couvreur
Envoy=C3=A9 le&n= bsp;:dimanche 14 janvier 2018 13:05
=C3=80 : mailing list for ConTeXt users
Objet&n= bsp;:[NTG-context] Problem with string.format (Lua)

 

Hello,
I start with Lua and I try to display my result with 4 decima= l places without success.
Thank youFabrice

\startlua= code
function Binomial(n,k,p)
    if k > n then return nil end
<= br>    if k > n/2 then k =3D n - k e= nd
 
&n= bsp;   numer, denom =3D 1, 1
 = ;   for i =3D 1, k do
  =       numer =3D numer * ( n - i + 1 )
        denom =3D den= om * i

    end
    return (numer/denom) * p^k * (1-p)^(n-k= )
end

\st= opluacode

\starttext
\ctxlua{print(string.format("%g",Binomial(100,60,0.25= )))}
\stoptext

 

= --_8A165818-60BF-47EE-B675-F9324CF7F35D_-- --===============6033448865084182512== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f X19fX19fX19fX19fX19fX19fX19fX19fX18KSWYgeW91ciBxdWVzdGlvbiBpcyBvZiBpbnRlcmVz dCB0byBvdGhlcnMgYXMgd2VsbCwgcGxlYXNlIGFkZCBhbiBlbnRyeSB0byB0aGUgV2lraSEKCm1h aWxsaXN0IDogbnRnLWNvbnRleHRAbnRnLm5sIC8gaHR0cDovL3d3dy5udGcubmwvbWFpbG1hbi9s aXN0aW5mby9udGctY29udGV4dAp3ZWJwYWdlICA6IGh0dHA6Ly93d3cucHJhZ21hLWFkZS5ubCAv IGh0dHA6Ly9jb250ZXh0LmFhbmhldC5uZXQKYXJjaGl2ZSAgOiBodHRwczovL2JpdGJ1Y2tldC5v cmcvcGhnL2NvbnRleHQtbWlycm9yL2NvbW1pdHMvCndpa2kgICAgIDogaHR0cDovL2NvbnRleHRn YXJkZW4ubmV0Cl9fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f --===============6033448865084182512==--