3

Is there a way to use "negative" numbers with multido and fp? In the code below, it works with \Nx=4+0.5 but not with \Nx=4+-0.5.

Additional question: is there a way to do this without using package fp? I think it is possible with calculating in PostScript but I don't know how to do this.

Thanks.

\documentclass[a4paper,10pt]{article}

\usepackage{multido}
\usepackage{fp}
\usepackage{pstricks}

\begin{document}

\begin{pspicture}(-5,5)(-5,5)
\multido{\Nx=4+-0.5}{12}{
  \FPeval{\moncalcul}{\Nx^2/4+1} 
  \psdots(\Nx,\moncalcul)
  }
\end{pspicture}

\end{document}
2

The raising of a negative number to a power seems to be the problem. Using abs around \Nx for the square works:

\documentclass[a4paper,10pt]{article}

\usepackage{multido}
\usepackage{fp}
\usepackage{pstricks}

\begin{document}

\begin{pspicture}(-5,5)(-5,5)
\multido{\Nx=4+-0.5}{12}{
  \FPeval{\moncalcul}{abs(\Nx)^2/4+1}
  \psdots(\Nx,\moncalcul)
  }
\end{pspicture}

\end{document}

Result

Other working variants without abs

  • Function pow instead of infix operator ^:

    \FPeval{\moncalcul}{pow(\Nx, 2)/4+1}
    
  • Using a multiplication:

    \FPeval{\moncalcul}{(\Nx*\Nx)/4 + 1}
    
  • Reverse Polish notation:

    \FPupn\moncalcul{\Nx{} 2 pow 4 div 1 add}
    

Calculation in PostScript without package fp

\psdots(! \Nx\space dup 2 exp 4 div 1 add)

or simple multiplication:

\psdots(! \Nx\space dup dup mul 4 div 1 add)

There must be two values on the stack for the x and y coordinates. Since TeX skips spaces after command names, \space inserts the needed space to separate the number from the following operator. The name for the pow function is exp in PostScript.

Full example:

\documentclass[a4paper,10pt]{article}
\usepackage{multido}
\usepackage{pstricks}

\begin{document}
  \begin{pspicture}(-5,5)(-5,5)
    \multido{\Nx=4+-0.5}{12}{
      \psdots(! \Nx\space dup 2 exp 4 div 1 add)
    }
  \end{pspicture}
\end{document}
  • Thank you for your very complete answer. So with RPN I tried this directly : \psdots(! \Nx \Nx 2 pow 4 div 1 add) But it doesn't work. Is there a solution like this with no use of fp ? – Oli Nov 23 '16 at 19:20
  • @Oli See updated answer. – Heiko Oberdiek Nov 23 '16 at 19:50

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.