# Divide integers and get a floating point number (for a pstricks coordinate)

I'm making a picture with pstricks and would like to plot points whose coordinates are fractions a/c, b/c, where a, b, and c run over certain ranges. I tried using \numexpr but it rounds down. I wouldn't use \dimexpr, as I assume that a dimension (ending with pt or suchlike) isn't welcome as a pstricks coordinate. I tried using pgf but I can't get the syntax to work:

\documentclass{article}
\usepackage{amsmath}
\usepackage{pstricks,pst-node,multido}
\RequirePackage{pgf}%
\begin{document}

\psset{xunit=5cm,yunit=5cm}
\begin{pspicture}(0,0)(1,1)
\multido{\iC=1+1}{5}{%
\multido{\iA=0+1}{\iC}{%
\multido{\iB=0+1}{\iC}{%
\pscircle*(\pgfmathparse{\iA/\iC},\pgfmathparse{\iB/\iC}){0.02}
}
}
}
\end{pspicture}
\end{document}


Desired output (produced using TikZ, which works for this 5x5 example but runs out TeX's memory on the larger scales I want to use).

• I forget the pstricks syntax but can't you use postscript arithmetic here \iA\space\iC\space div ? – David Carlisle Feb 19 '18 at 15:38

\documentclass{article}
\usepackage{pstricks,multido}
\begin{document}

\psset{xunit=5cm,yunit=5cm}
\begin{pspicture}(0,0)(1,1)
\multido{\iC=1+1}{5}{%
\multido{\iA=0+1}{\iC}{%
\multido{\iB=0+1}{\iC}{%
\psdot(!\iA\space \iC\space div \iB\space \iC\space div)
}%
}%
}%
\end{pspicture}
\end{document}


or

\documentclass{article}
\usepackage{pstricks,multido,pst-calculate}
\begin{document}

\psset{unit=8}
\begin{pspicture}(2,2)
\multido{\iC=1+1}{20}{%
\multido{\iA=0+1}{\numexpr\iC+1}{%
\multido{\iB=0+1}{\numexpr\iC+1}{%
\psdot[dotscale=0.5](\pscalculate{\iA/\iC},\pscalculate{\iB/\iC})
}%
}%
}%
\end{pspicture}
\end{document}


You forgot to invoke \pgfmathresult.

\documentclass{article}
\usepackage{amsmath}
\usepackage{pstricks,pst-node,multido}
\RequirePackage{pgf}%
\begin{document}

\psset{xunit=5cm,yunit=5cm}
\begin{pspicture}(0,0)(1,1)
\multido{\iC=1+1}{5}{%
\multido{\iA=0+1}{\iC}{%
\multido{\iB=0+1}{\iC}{%
\pgfmathparse{\iA/\iC}\xdef\myx{\pgfmathresult}
\pgfmathparse{\iB/\iC}\xdef\myy{\pgfmathresult}
\pscircle*(\myx,\myy){0.02}
}
}
}
\end{pspicture}
\end{document}


UPDATE: I do not get any problems when doing this with TikZ, where the code is shorter, no packages are required and the compilation is more convenient (even though I really like PSTricks).

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=10]
\foreach \iC in {1,...,15}{%
\foreach \iA in {0,...,\iC}{%
\foreach \iB in {0, ...,\iC}{%
\draw[fill=black] ({\iA/\iC},{\iB/\iC}) circle (0.002);
}
}
}
\end{tikzpicture}
\end{document}


• May I ask why \pgfmathparse{...}\pgfmathresult cannot be put inside \pscircle, in particular as \pscircle*(\pgfmathparse{\iA/\iC}\pgfmathresult,\pgfmathparse{\iB/\iC}\pgfmathresult){0.02}, and you had to define \myx and \myy? I do not need \myx and \myy anywhere else. – Hotschke Oct 13 '18 at 8:38
• @Hotschke Well, the parser may just not be OK with that. – user121799 Oct 13 '18 at 8:56
• Yes, I noticed that. However, it is not obvious to me why. I was hoping to get an answer here why the parser is not OK with it. – Hotschke Oct 13 '18 at 9:15
• @Hotschke I can only guess. It may just not be OK with two macros at a position in which it only expects a number or macro. I have switched almost completely to TikZ and forgot much of what I knew about pstricks, but most likely I never understood how the parser works there anyway. Note, however, that in TikZ \draw[fill=black] (\pgfmathparse{\iA/\iC}\pgfmathresult,{\iB/\iC}) circle (0.002); does not work for that reason. There, of course, you can do \draw[fill=black] ({\iA/\iC},{\iB/\iC}) circle (0.002); and the problem does not arise in the first place. – user121799 Oct 13 '18 at 9:21
• I agree that this is an XY problem. In pstricks one naturally uses postscript to do calculations and in tikz \pgfmathparse is run anyway. Both are more compact and do not need \myx and \myy. – Hotschke Oct 13 '18 at 9:26

Also available in Metapost, here wrapped up in luamplib so compile with lualatex or work out how to adapt it for GMP, or plain MP.

I've added a bit of color too.

\documentclass[border=5mm]{standalone}
\usepackage{luatex85, luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
n = 25;
for i = 1 upto n:
for j = 0 upto i:
for k = 0 upto i:
fill fullcircle
scaled 3
shifted ((j/i, k/i) scaled (n*cm))
withcolor 3/4(j/i, k/i, i/n);
endfor
endfor
endfor
endfig;
\end{mplibcode}
\end{document}