* Problem with string.format (Lua)
@ 2018-01-14 12:05 Fabrice Couvreur
2018-01-14 12:13 ` Joseph Canedo
0 siblings, 1 reply; 5+ messages in thread
From: Fabrice Couvreur @ 2018-01-14 12:05 UTC (permalink / raw)
To: mailing list for ConTeXt users
[-- Attachment #1.1: Type: text/plain, Size: 479 bytes --]
Hello,
I start with Lua and I try to display my result with 4 decimal places
without success.
Thank you
Fabrice
\startluacode
function Binomial(n,k,p)
if k > n then return nil end
if k > n/2 then k = n - k end
numer, denom = 1, 1
for i = 1, k do
numer = numer * ( n - i + 1 )
denom = denom * i
end
return (numer/denom) * p^k * (1-p)^(n-k)
end
\stopluacode
\starttext
\ctxlua{print(string.format("%g",Binomial(100,60,0.25)))}
\stoptext
[-- Attachment #1.2: Type: text/html, Size: 821 bytes --]
[-- Attachment #2: Type: text/plain, Size: 492 bytes --]
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Problem with string.format (Lua)
2018-01-14 12:05 Problem with string.format (Lua) Fabrice Couvreur
@ 2018-01-14 12:13 ` Joseph Canedo
2018-01-14 12:58 ` Hans Hagen
0 siblings, 1 reply; 5+ messages in thread
From: Joseph Canedo @ 2018-01-14 12:13 UTC (permalink / raw)
To: mailing list for ConTeXt users
[-- Attachment #1.1: Type: text/plain, Size: 854 bytes --]
Hi Fabrice,
string.format behaves more or less as C printf. You probably need to specify %.4g in 1st argument.
Hope this helps
Best
Joseph
De : Fabrice Couvreur
Envoyé le :dimanche 14 janvier 2018 13:05
À : mailing list for ConTeXt users
Objet :[NTG-context] Problem with string.format (Lua)
Hello,
I start with Lua and I try to display my result with 4 decimal places without success.
Thank you
Fabrice
\startluacode
function Binomial(n,k,p)
if k > n then return nil end
if k > n/2 then k = n - k end
numer, denom = 1, 1
for i = 1, k do
numer = numer * ( n - i + 1 )
denom = denom * i
end
return (numer/denom) * p^k * (1-p)^(n-k)
end
\stopluacode
\starttext
\ctxlua{print(string.format("%g",Binomial(100,60,0.25)))}
\stoptext
[-- Attachment #1.2: Type: text/html, Size: 3664 bytes --]
[-- Attachment #2: Type: text/plain, Size: 492 bytes --]
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Problem with string.format (Lua)
2018-01-14 12:13 ` Joseph Canedo
@ 2018-01-14 12:58 ` Hans Hagen
2018-01-14 20:50 ` Fabrice Couvreur
0 siblings, 1 reply; 5+ messages in thread
From: Hans Hagen @ 2018-01-14 12:58 UTC (permalink / raw)
To: mailing list for ConTeXt users, Joseph Canedo
On 1/14/2018 1:13 PM, Joseph Canedo wrote:
> Hi Fabrice,
>
> string.format behaves more or less as C printf. You probably need to
> specify %.4g in 1st argument.
>
> Hope this helps
>
> Best
>
> Joseph
>
> *De : *Fabrice Couvreur <mailto:fabrice1.couvreur@gmail.com>
> *Envoyé le :*dimanche 14 janvier 2018 13:05
> *À : *mailing list for ConTeXt users <mailto:ntg-context@ntg.nl>
> *Objet :*[NTG-context] Problem with string.format (Lua)
>
> Hello,
> I start with Lua and I try to display my result with 4 decimal places
> without success.
> Thank you
> Fabrice
>
> \startluacode
> function Binomial(n,k,p)
> if k > n then return nil end
> if k > n/2 then k = n - k end
>
> numer, denom = 1, 1
> for i = 1, k do
> numer = numer * ( n - i + 1 )
> denom = denom * i
> end
> return (numer/denom) * p^k * (1-p)^(n-k)
> end
>
> \stopluacode
>
> \starttext
> \ctxlua{print(string.format("%g",Binomial(100,60,0.25)))}
> \stoptext
the % should be \letterpercent
btw, use locals for numer and denom
you can also do this:
\startluacode
function document.Binomial(n,k,p)
if k > n then
return nil
end
if k > n/2 then
k = n - k
end
local numer, denom = 1, 1
for i=1,k do
numer = numer * ( n - i + 1 )
denom = denom * i
end
context("%0.5g",(numer/denom) * p^k * (1-p)^(n-k))
end
\stopluacode
\starttext
\ctxlua{document.Binomial(100,60,0.25)}
\stoptext
in general
context("format",....)
is a bit more powerful than using format
--
-----------------------------------------------------------------
Hans Hagen | PRAGMA ADE
Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Problem with string.format (Lua)
2018-01-14 12:58 ` Hans Hagen
@ 2018-01-14 20:50 ` Fabrice Couvreur
2018-01-14 23:02 ` Hans Hagen
0 siblings, 1 reply; 5+ messages in thread
From: Fabrice Couvreur @ 2018-01-14 20:50 UTC (permalink / raw)
To: mailing list for ConTeXt users
[-- Attachment #1.1: Type: text/plain, Size: 2943 bytes --]
Hi Hans,
Your code runs perfectly, thanks. However, if I replace % with
\letterpercent, my code does not run
\ctxlua{print(string.format("\letterpercent g",Binomial(100,60,0.25)))}
Thank you
Fabrice
2018-01-14 13:58 GMT+01:00 Hans Hagen <pragma@wxs.nl>:
> On 1/14/2018 1:13 PM, Joseph Canedo wrote:
>
>> Hi Fabrice,
>>
>> string.format behaves more or less as C printf. You probably need to
>> specify %.4g in 1st argument.
>>
>> Hope this helps
>>
>> Best
>>
>> Joseph
>>
>> *De : *Fabrice Couvreur <mailto:fabrice1.couvreur@gmail.com>
>> *Envoyé le :*dimanche 14 janvier 2018 13:05
>> *À : *mailing list for ConTeXt users <mailto:ntg-context@ntg.nl>
>> *Objet :*[NTG-context] Problem with string.format (Lua)
>>
>> Hello,
>> I start with Lua and I try to display my result with 4 decimal places
>> without success.
>> Thank you
>> Fabrice
>>
>> \startluacode
>> function Binomial(n,k,p)
>> if k > n then return nil end
>> if k > n/2 then k = n - k end
>>
>> numer, denom = 1, 1
>> for i = 1, k do
>> numer = numer * ( n - i + 1 )
>> denom = denom * i
>> end
>> return (numer/denom) * p^k * (1-p)^(n-k)
>> end
>>
>> \stopluacode
>>
>> \starttext
>> \ctxlua{print(string.format("%g",Binomial(100,60,0.25)))}
>> \stoptext
>>
>
> the % should be \letterpercent
>
> btw, use locals for numer and denom
>
> you can also do this:
>
> \startluacode
> function document.Binomial(n,k,p)
> if k > n then
> return nil
> end
> if k > n/2 then
> k = n - k
> end
> local numer, denom = 1, 1
> for i=1,k do
> numer = numer * ( n - i + 1 )
> denom = denom * i
> end
> context("%0.5g",(numer/denom) * p^k * (1-p)^(n-k))
> end
> \stopluacode
>
> \starttext
> \ctxlua{document.Binomial(100,60,0.25)}
> \stoptext
>
> in general
>
> context("format",....)
>
> is a bit more powerful than using format
>
>
>
>
> --
>
> -----------------------------------------------------------------
> Hans Hagen | PRAGMA ADE
> Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
> tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
> -----------------------------------------------------------------
> ____________________________________________________________
> _______________________
> If your question is of interest to others as well, please add an entry to
> the Wiki!
>
> maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/list
> info/ntg-context
> webpage : http://www.pragma-ade.nl / http://context.aanhet.net
> archive : https://bitbucket.org/phg/context-mirror/commits/
> wiki : http://contextgarden.net
> ____________________________________________________________
> _______________________
[-- Attachment #1.2: Type: text/html, Size: 4815 bytes --]
[-- Attachment #2: Type: text/plain, Size: 492 bytes --]
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Problem with string.format (Lua)
2018-01-14 20:50 ` Fabrice Couvreur
@ 2018-01-14 23:02 ` Hans Hagen
0 siblings, 0 replies; 5+ messages in thread
From: Hans Hagen @ 2018-01-14 23:02 UTC (permalink / raw)
To: mailing list for ConTeXt users, Fabrice Couvreur
On 1/14/2018 9:50 PM, Fabrice Couvreur wrote:
> Hi Hans,
> Your code runs perfectly, thanks. However, if I replace % with
> \letterpercent, my code does not run
>
> \ctxlua{print(string.format("\letterpercent g",Binomial(100,60,0.25)))}
because print prints to the console
> Thank you
> Fabrice
>
> 2018-01-14 13:58 GMT+01:00 Hans Hagen <pragma@wxs.nl
> <mailto:pragma@wxs.nl>>:
>
> On 1/14/2018 1:13 PM, Joseph Canedo wrote:
>
> Hi Fabrice,
>
> string.format behaves more or less as C printf. You probably
> need to specify %.4g in 1st argument.
>
> Hope this helps
>
> Best
>
> Joseph
>
> *De : *Fabrice Couvreur <mailto:fabrice1.couvreur@gmail.com
> <mailto:fabrice1.couvreur@gmail.com>>
> *Envoyé le :*dimanche 14 janvier 2018 13:05
> *À : *mailing list for ConTeXt users <mailto:ntg-context@ntg.nl
> <mailto:ntg-context@ntg.nl>>
> *Objet :*[NTG-context] Problem with string.format (Lua)
>
> Hello,
> I start with Lua and I try to display my result with 4 decimal
> places without success.
> Thank you
> Fabrice
>
> \startluacode
> function Binomial(n,k,p)
> if k > n then return nil end
> if k > n/2 then k = n - k end
>
> numer, denom = 1, 1
> for i = 1, k do
> numer = numer * ( n - i + 1 )
> denom = denom * i
> end
> return (numer/denom) * p^k * (1-p)^(n-k)
> end
>
> \stopluacode
>
> \starttext
> \ctxlua{print(string.format("%g",Binomial(100,60,0.25)))}
> \stoptext
>
>
> the % should be \letterpercent
>
> btw, use locals for numer and denom
>
> you can also do this:
>
> \startluacode
> function document.Binomial(n,k,p)
> if k > n then
> return nil
> end
> if k > n/2 then
> k = n - k
> end
> local numer, denom = 1, 1
> for i=1,k do
> numer = numer * ( n - i + 1 )
> denom = denom * i
> end
> context("%0.5g",(numer/denom) * p^k * (1-p)^(n-k))
> end
> \stopluacode
>
> \starttext
> \ctxlua{document.Binomial(100,60,0.25)}
> \stoptext
>
> in general
>
> context("format",....)
>
> is a bit more powerful than using format
>
>
>
>
> --
>
> -----------------------------------------------------------------
> Hans Hagen | PRAGMA ADE
> Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
> tel: 038 477 53 69 | www.pragma-ade.nl
> <http://www.pragma-ade.nl> | www.pragma-pod.nl
> <http://www.pragma-pod.nl>
> -----------------------------------------------------------------
> ___________________________________________________________________________________
> If your question is of interest to others as well, please add an
> entry to the Wiki!
>
> maillist : ntg-context@ntg.nl <mailto:ntg-context@ntg.nl> /
> http://www.ntg.nl/mailman/listinfo/ntg-context
> <http://www.ntg.nl/mailman/listinfo/ntg-context>
> webpage : http://www.pragma-ade.nl / http://context.aanhet.net
> archive : https://bitbucket.org/phg/context-mirror/commits/
> <https://bitbucket.org/phg/context-mirror/commits/>
> wiki : http://contextgarden.net
> ___________________________________________________________________________________
>
>
>
>
> ___________________________________________________________________________________
> If your question is of interest to others as well, please add an entry to the Wiki!
>
> maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
> webpage : http://www.pragma-ade.nl / http://context.aanhet.net
> archive : https://bitbucket.org/phg/context-mirror/commits/
> wiki : http://contextgarden.net
> ___________________________________________________________________________________
>
--
-----------------------------------------------------------------
Hans Hagen | PRAGMA ADE
Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2018-01-14 23:02 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-01-14 12:05 Problem with string.format (Lua) Fabrice Couvreur
2018-01-14 12:13 ` Joseph Canedo
2018-01-14 12:58 ` Hans Hagen
2018-01-14 20:50 ` Fabrice Couvreur
2018-01-14 23:02 ` Hans Hagen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).