3

It seems that isprime() is broken in TeXLive 2016 (with the latest packages).

The MWE below is from Given a list of primes, how can I plot π(x)? and works fine with TeXLIve2015 but with TeXLive2016 it fails with:

Package PGF Math Error: Sorry, the operation 'isprime' has not yet been implemented in the floating point unit (in 'isprime(0.0e0)').

However, a simpler test case (see MWE 2 below) works?

Related Question:

Questions:

  • Is one of my pacakges not up to date?
  • Is there a patch and is this still a problem in the beta builds of TeXLive2017?

Filelist

 article.cls    2014/09/29 v1.4h Standard LaTeX document class
  size10.clo    2014/09/29 v1.4h Standard LaTeX file (size option)
pgfplots.sty    2016/08/10 v1.14 Data Visualization (1.14)
graphicx.sty    2014/10/28 v1.0g Enhanced LaTeX Graphics (DPC,SPQR)
  keyval.sty    2014/10/28 v1.15 key=value parser (DPC)
graphics.sty    2016/10/09 v1.0u Standard LaTeX Graphics (DPC,SPQR)
    trig.sty    2016/01/03 v1.10 sin cos tan (DPC)
graphics.cfg    2016/06/04 v1.11 sample graphics configuration
  pdftex.def    2017/01/12 v0.06k Graphics/color for pdfTeX
infwarerr.sty   2016/05/16 v1.4 Providing info/warning/error messages (HO)
 ltxcmds.sty    2016/05/16 v1.23 LaTeX kernel commands for general use (HO)
    tikz.sty    2015/08/07 v3.0.1a (rcs-revision 1.151)
     pgf.sty    2015/08/07 v3.0.1a (rcs-revision 1.15)
  pgfrcs.sty    2015/08/07 v3.0.1a (rcs-revision 1.31)
everyshi.sty    2001/05/15 v3.00 EveryShipout Package (MS)
  pgfrcs.code.tex
 pgfcore.sty    2010/04/11 v3.0.1a (rcs-revision 1.7)
  pgfsys.sty    2014/07/09 v3.0.1a (rcs-revision 1.48)
  pgfsys.code.tex
pgfsyssoftpath.code.tex    2013/09/09  (rcs-revision 1.9)
pgfsysprotocol.code.tex    2006/10/16  (rcs-revision 1.4)
  xcolor.sty    2016/05/11 v2.12 LaTeX color extensions (UK)
   color.cfg    2016/01/02 v1.6 sample color configuration
 pgfcore.code.tex
pgfcomp-version-0-65.sty    2007/07/03 v3.0.1a (rcs-revision 1.7)
pgfcomp-version-1-18.sty    2007/07/23 v3.0.1a (rcs-revision 1.1)
  pgffor.sty        2013/12/13 v3.0.1a (rcs-revision 1.25)
 pgfkeys.sty    
 pgfkeys.code.tex
 pgfmath.sty    
 pgfmath.code.tex
  pgffor.code.tex
    tikz.code.tex
pgfplotstable.sty   2016/08/10 v1.14 Table typesetting and Pretty-printing (1.14)
   array.sty        2016/10/06 v2.4d Tabular extension package (FMi)
supp-pdf.mkii
pdftexcmds.sty      2017/03/19 v0.25 Utility functions of pdfTeX for LuaTeX (HO)
ifluatex.sty        2016/05/16 v1.4 Provides the ifluatex switch (HO)
   ifpdf.sty        2017/03/15 v3.2 Provides the ifpdf switch
epstopdf-base.sty   2016/05/15 v2.6 Base part for package epstopdf
  grfext.sty        2016/05/16 v1.2 Manage graphics extensions (HO)
kvdefinekeys.sty    2016/05/16 v1.4 Define keys (HO)
kvoptions.sty       2016/05/16 v3.12 Key value format for package options (HO)
kvsetkeys.sty       2016/05/16 v1.17 Key value parser (HO)
etexcmds.sty        2016/05/16 v1.6 Avoid name clashes with e-TeX commands (HO)
epstopdf-sys.cfg    2010/07/13 v1.3 Configuration of (r)epstopdf for TeX Live                                         

Code: Fails

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=newest}

\begin{document}%% https://tex.stackexchange.com/a/286911/4301
\pgfplotstablenew[
    create on use/x/.style={
        create col/expr={
            \pgfplotstablerow
        }
    },
    create on use/isprime/.style={
        create col/assign/.code={% Can't use "expr" here because "isint" doesn't work reliably with the FPU engine at the moment
            \pgfmathparse{isprime(\thisrow{x})}%
            \pgfkeyslet{/pgfplots/table/create col/next content}\pgfmathresult%
        }
    },
    create on use/primecount/.style={
        create col/expr={
            \pgfmathaccuma + \thisrow{isprime}
        }
    },
    columns={x, isprime, primecount}
]{25}\loadedtable

\begin{tikzpicture}
\begin{axis}[
  title={The prime counting function: $\pi(x)$},
  xlabel=$x$,
  ylabel=$\pi(x)$,
  ]
\addplot[only marks] table [x=x, y=primecount] {\loadedtable};
\end{axis}
\end{tikzpicture}
\end{document}

Code: Works

\documentclass{article}
\usepackage{tikz}

\newcommand{\IsThisPrime}[1]{%
    \par#1: \pgfmathparse{isprime(#1)}
    \ifnum\pgfmathresult=0
        is not prime.
    \else
        is prime.
    \fi
}%

\begin{document}
\foreach \i in {1, ..., 10} {%
    \IsThisPrime{\i}
}%
\end{document}
4
  • Another occurence of tex.stackexchange.com/questions/328972/… the list is at the end of Christian's answer – percusse May 20 '17 at 8:19
  • @percusse: Thanks for that link. Good to know that Christian is aware of it. Do you know if it is fixed in the TeXLive 2017? If so, I'll wait until then rather than patching. – Peter Grill May 20 '17 at 8:31
  • No I used tl2017 updated this morning (I'd better have a look at what @percusse links to now:-) – David Carlisle May 20 '17 at 8:32
  • @PeterGrill I really don't know. I'm not able to keep up too much with the latest developments unfortunately. But his temp hack while loading the pgfplots should also work for isprime – percusse May 20 '17 at 8:46
5

enter image description here

apparently the error message is correct and it isn't defined.

You could define it (there are more efficient primality tests than this)

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=newest}

\makeatletter
\pgfmathdeclarefunction*{isprime}{1}{%
\dimen@#1\p@
\edef\pgfmathresult{%
\ifdim\dimen@=2pt 1\else
\ifdim\dimen@=3pt 1\else
\ifdim\dimen@=5pt 1\else
\ifdim\dimen@=7pt 1\else
\ifdim\dimen@=11pt 1\else
\ifdim\dimen@=13pt 1\else
\ifdim\dimen@=17pt 1\else
\ifdim\dimen@=19pt 1\else
\ifdim\dimen@=23pt 1\else
0%
\fi\fi\fi\fi\fi\fi\fi\fi\fi
}
}
\makeatother

\begin{document}%% https://tex.stackexchange.com/a/286911/4301
\pgfplotstablenew[
    create on use/x/.style={
        create col/expr={
            \pgfplotstablerow
        }
    },
    create on use/isprime/.style={
        create col/assign/.code={% Can't use "expr" here because "isint" doesn't work reliably with the FPU engine at the moment
            \pgfmathparse{isprime(round(\thisrow{x}))}%
            \pgfkeyslet{/pgfplots/table/create col/next content}\pgfmathresult%
        }
    },
    create on use/primecount/.style={
        create col/expr={
            \pgfmathaccuma + \thisrow{isprime}
        }
    },
    columns={x, isprime, primecount}
]{25}\loadedtable

\begin{tikzpicture}
\begin{axis}[
  title={The prime counting function: $\pi(x)$},
  xlabel=$x$,
  ylabel=$\pi(x)$,
  ]
\addplot[only marks] table [x=x, y=primecount] {\loadedtable};
\end{axis}
\end{tikzpicture}
\end{document}
6
  • Well, that is quite the genius primality test! :-). If isprime is not defined, then how does the second test case (just added) work? – Peter Grill May 20 '17 at 8:59
  • @PeterGrill hey it's unfair to change the question after my brilliant and incisive analysis of the original version:-) my "not defined" comment equates to "/usr/local/texlive/2017/texmf-dist/tex/generic/pgf/libraries/pgflibraryfpu.code.tex" does not have the string "isprime" anywhere but does have the source of other similar things such as iffinite so I defined a function to plug the gap. – David Carlisle May 20 '17 at 9:02
  • but the new example is using the core fixed point arithmetic not the fpu isn't it? – David Carlisle May 20 '17 at 9:05
  • Sorry that is beyond me. Not sure why the failing test case can't just use the same function. – Peter Grill May 20 '17 at 9:10
  • @PeterGrill it's a different data type pgf uses one form to hold non-integer numbers (as dimen in pt, essentially) but that has limited precision, the fpu library changes the internal representation to implement floating point arithmetic closer to the arithmetic of double floating point in conventional programming languages, which means all the functions need to be re-implemented. – David Carlisle May 20 '17 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.