* Arithmetic Overflow error in MetaFun
@ 2020-05-16 14:36 Keith McKay
2020-05-16 14:48 ` Keith McKay
2020-05-16 14:50 ` Hans Hagen
0 siblings, 2 replies; 5+ messages in thread
From: Keith McKay @ 2020-05-16 14:36 UTC (permalink / raw)
To: mailing list for ConTeXt users
[-- Attachment #1.1: Type: text/plain, Size: 2154 bytes --]
Colleagues
I'm having problems with an Arithmetic Overflow error when calculating
the x and y values for functions of the form:
y = ax^4 + bx^3 + cx^2 + dx + e
to which I then add a bit of randomness and other embellishments. I have
had a look in the Metafun and Metafont manuals but nothing jumps out at
me. In the minimum working example below, this Arithmetic Overflow
occurs when xord =13.5 but I can get the calculation to work if I
separate out the equation and then add together, since it seems to be
failing on the pow(xord,4) instruction. Is there a switch in MetaFont
that should be set or a better way of getting round this Arithmetic
Overflow error?
Thanks
Best Wishes
Keith McKay
%%%%%%%%%%%%%MWE%%%%%%%%%%%
\setuppapersize [A4,landscape]
\starttext
\startMPpage
StartPage;
width := PaperWidth ; height := PaperHeight ; unit := cm ;
path p;
p := unitsquare scaled .1cm ;
path pat;
pat := (0.0*cm,19.935*cm);
for xord = 0 step 0.1 until 25:
show xord;
%%%%%%%%fails at xord = 13.5%%%%%%%%%
yord := ((-0.000268117) * pow(xord,4)) +(0.0136949 * pow(xord, 3)) +
((-0.16608) * sqr(xord)) + ((-0.771743) * xord) + 19.935;
%%%%%comment out above and uncomment below and it works%%%%
% yord1 := (-0.000268117) * sqr(xord);
% yord1a := yord1 * sqr(xord);
% yord2 := (0.0136949 * pow(xord, 3));
% yord3 := ((-0.16608) * sqr(xord));
% yord4 := ((-0.771743) * xord);
% yord := yord1 + yord1a + yord2 + yord3 + yord4 + 19.935;
xrand := (uniformdeviate(1) - 0.5)*2;
yrand := uniformdeviate(1) - 0.5;
xcoord := xord + xrand;
ycoord := yord + yrand;
if odd xord:
pat := pat ... (xcoord*cm, ycoord*cm);
else:
pat := pat --- (xcoord*cm, ycoord*cm);
fill p shifted (xcoord*cm, ycoord*cm) withcolor
(uniformdeviate(1),uniformdeviate(1),uniformdeviate(1)) ;
fi;
draw pat withpen pencircle scaled (uniformdeviate(0.75)*mm) withcolor
(uniformdeviate(1),uniformdeviate(1),uniformdeviate(1));
%withtransparency (1, .5);
drawdot (xcoord*cm, ycoord*cm) withpen pencircle scaled
(uniformdeviate(2.5)*mm) withcolor
(uniformdeviate(1),uniformdeviate(1),uniformdeviate(1));
endfor;
StopPage;
\stopMPpage
\stoptext
[-- Attachment #1.2: Type: text/html, Size: 6978 bytes --]
[-- Attachment #2: Type: text/plain, Size: 493 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: Arithmetic Overflow error in MetaFun
2020-05-16 14:36 Arithmetic Overflow error in MetaFun Keith McKay
@ 2020-05-16 14:48 ` Keith McKay
2020-05-16 19:46 ` Hans Hagen
2020-05-16 14:50 ` Hans Hagen
1 sibling, 1 reply; 5+ messages in thread
From: Keith McKay @ 2020-05-16 14:48 UTC (permalink / raw)
To: mailing list for ConTeXt users
[-- Attachment #1.1: Type: text/plain, Size: 2575 bytes --]
Answering my own problem here.
I've just searched through the mailing list archives and it seems that
if I add [instance=doublefun] I do not get the Arithmetic Overflow
error. Joy!
Thanks
Best Wishes
Keith McKay
On 16/05/2020 15:36, Keith McKay wrote:
>
> Colleagues
>
> I'm having problems with an Arithmetic Overflow error when calculating
> the x and y values for functions of the form:
>
> y = ax^4 + bx^3 + cx^2 + dx + e
>
> to which I then add a bit of randomness and other embellishments. I
> have had a look in the Metafun and Metafont manuals but nothing jumps
> out at me. In the minimum working example below, this Arithmetic
> Overflow occurs when xord =13.5 but I can get the calculation to work
> if I separate out the equation and then add together, since it seems
> to be failing on the pow(xord,4) instruction. Is there a switch in
> MetaFont that should be set or a better way of getting round this
> Arithmetic Overflow error?
>
> Thanks
>
> Best Wishes
>
> Keith McKay
>
> %%%%%%%%%%%%%MWE%%%%%%%%%%%
>
> \setuppapersize [A4,landscape]
>
> \starttext
>
> \startMPpage
>
> StartPage;
>
> width := PaperWidth ; height := PaperHeight ; unit := cm ;
>
> path p;
>
> p := unitsquare scaled .1cm ;
>
> path pat;
>
> pat := (0.0*cm,19.935*cm);
>
> for xord = 0 step 0.1 until 25:
>
> show xord;
>
> %%%%%%%%fails at xord = 13.5%%%%%%%%%
>
> yord := ((-0.000268117) * pow(xord,4)) +(0.0136949 * pow(xord, 3)) +
> ((-0.16608) * sqr(xord)) + ((-0.771743) * xord) + 19.935;
>
> %%%%%comment out above and uncomment below and it works%%%%
>
> % yord1 := (-0.000268117) * sqr(xord);
>
> % yord1a := yord1 * sqr(xord);
>
> % yord2 := (0.0136949 * pow(xord, 3));
>
> % yord3 := ((-0.16608) * sqr(xord));
>
> % yord4 := ((-0.771743) * xord);
>
> % yord := yord1 + yord1a + yord2 + yord3 + yord4 + 19.935;
>
> xrand := (uniformdeviate(1) - 0.5)*2;
>
> yrand := uniformdeviate(1) - 0.5;
>
> xcoord := xord + xrand;
>
> ycoord := yord + yrand;
>
> if odd xord:
>
> pat := pat ... (xcoord*cm, ycoord*cm);
>
> else:
>
> pat := pat --- (xcoord*cm, ycoord*cm);
>
> fill p shifted (xcoord*cm, ycoord*cm) withcolor
> (uniformdeviate(1),uniformdeviate(1),uniformdeviate(1)) ;
>
> fi;
>
> draw pat withpen pencircle scaled (uniformdeviate(0.75)*mm) withcolor
> (uniformdeviate(1),uniformdeviate(1),uniformdeviate(1));
> %withtransparency (1, .5);
>
> drawdot (xcoord*cm, ycoord*cm) withpen pencircle scaled
> (uniformdeviate(2.5)*mm) withcolor
> (uniformdeviate(1),uniformdeviate(1),uniformdeviate(1));
>
> endfor;
>
> StopPage;
>
> \stopMPpage
>
> \stoptext
>
[-- Attachment #1.2: Type: text/html, Size: 7609 bytes --]
[-- Attachment #2: Type: text/plain, Size: 493 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: Arithmetic Overflow error in MetaFun
2020-05-16 14:48 ` Keith McKay
@ 2020-05-16 19:46 ` Hans Hagen
2020-05-17 8:19 ` Keith McKay
0 siblings, 1 reply; 5+ messages in thread
From: Hans Hagen @ 2020-05-16 19:46 UTC (permalink / raw)
To: Keith McKay, mailing list for ConTeXt users
On 5/16/2020 4:48 PM, Keith McKay wrote:
> Answering my own problem here.
>
> I've just searched through the mailing list archives and it seems that
> if I add [instance=doublefun] I do not get the Arithmetic Overflow
> error. Joy!
fwiw, we have also instance=decimalfun
concerning arithmic:
in metapost we have
scaled : upto 4K bp
double : 64 bit floats
decimal : configurable accuracy
complex : dual doubles (only in luametafun)
in lmtx lua we have:
integer : lua tries to be clever but mixing leads to double
float : lua tries to be clever
decimal : userdata arbitrary precision (only in lmtx)
complex : userdata dual doubles (only in lmtx)
(in lmtx we have an extended math library and these function are also
available in luametafun)
Hans
-----------------------------------------------------------------
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: Arithmetic Overflow error in MetaFun
2020-05-16 19:46 ` Hans Hagen
@ 2020-05-17 8:19 ` Keith McKay
0 siblings, 0 replies; 5+ messages in thread
From: Keith McKay @ 2020-05-17 8:19 UTC (permalink / raw)
To: Hans Hagen; +Cc: mailing list for ConTeXt users
[-- Attachment #1.1: Type: text/plain, Size: 1339 bytes --]
Thanks for your help Hans and the pointer to the extended math library.
Lots more to explore now.
Best Wishes
Keith
On Sat, 16 May 2020, 20:46 Hans Hagen, <j.hagen@xs4all.nl> wrote:
> On 5/16/2020 4:48 PM, Keith McKay wrote:
> > Answering my own problem here.
> >
> > I've just searched through the mailing list archives and it seems that
> > if I add [instance=doublefun] I do not get the Arithmetic Overflow
> > error. Joy!
> fwiw, we have also instance=decimalfun
>
> concerning arithmic:
>
> in metapost we have
>
> scaled : upto 4K bp
> double : 64 bit floats
> decimal : configurable accuracy
> complex : dual doubles (only in luametafun)
>
> in lmtx lua we have:
>
> integer : lua tries to be clever but mixing leads to double
> float : lua tries to be clever
> decimal : userdata arbitrary precision (only in lmtx)
> complex : userdata dual doubles (only in lmtx)
>
> (in lmtx we have an extended math library and these function are also
> available in luametafun)
>
> Hans
>
> -----------------------------------------------------------------
> Hans Hagen | PRAGMA ADE
> Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
> tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
> -----------------------------------------------------------------
>
[-- Attachment #1.2: Type: text/html, Size: 2004 bytes --]
[-- Attachment #2: Type: text/plain, Size: 493 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: Arithmetic Overflow error in MetaFun
2020-05-16 14:36 Arithmetic Overflow error in MetaFun Keith McKay
2020-05-16 14:48 ` Keith McKay
@ 2020-05-16 14:50 ` Hans Hagen
1 sibling, 0 replies; 5+ messages in thread
From: Hans Hagen @ 2020-05-16 14:50 UTC (permalink / raw)
To: mailing list for ConTeXt users, Keith McKay
On 5/16/2020 4:36 PM, Keith McKay wrote:
> Colleagues
>
> I'm having problems with an Arithmetic Overflow error when calculating
> the x and y values for functions of the form:
>
> y = ax^4 + bx^3 + cx^2 + dx + e
>
> to which I then add a bit of randomness and other embellishments. I have
> had a look in the Metafun and Metafont manuals but nothing jumps out at
> me. In the minimum working example below, this Arithmetic Overflow
> occurs when xord =13.5 but I can get the calculation to work if I
> separate out the equation and then add together, since it seems to be
> failing on the pow(xord,4) instruction. Is there a switch in MetaFont
> that should be set or a better way of getting round this Arithmetic
> Overflow error?
\startMPpage[instance=doublefun]
-----------------------------------------------------------------
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:[~2020-05-17 8:19 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-16 14:36 Arithmetic Overflow error in MetaFun Keith McKay
2020-05-16 14:48 ` Keith McKay
2020-05-16 19:46 ` Hans Hagen
2020-05-17 8:19 ` Keith McKay
2020-05-16 14:50 ` 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).