ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Keith McKay <mckaymeister@gmail.com>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Arithmetic Overflow error in MetaFun
Date: Sat, 16 May 2020 15:36:10 +0100	[thread overview]
Message-ID: <4830f0a0-ece9-53ce-2768-af9110f3962a@gmail.com> (raw)


[-- 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
___________________________________________________________________________________

             reply	other threads:[~2020-05-16 14:36 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-16 14:36 Keith McKay [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4830f0a0-ece9-53ce-2768-af9110f3962a@gmail.com \
    --to=mckaymeister@gmail.com \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).