Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am exploring some of the LaTeX3 routines and syntax. How do I get to use functions such as sin etc. I get an error with the sin function in the following (please uncomment sin function to view the error) :

\documentclass{article}
\usepackage{expl3}

\begin{document}

\ExplSyntaxOn
\newcommand*\sampleCalculation{%
  \fp_new:N  \mynumber
  \fp_set:Nn \mynumber{150}
  \fp_sub:Nn \mynumber{5}
  \fp_add:Nn \mynumber{-1}
  \fp_div:Nn \mynumber{12}
  \fp_mul:Nn \mynumber{0.0001}
  \fp_sub:Nn \mynumber{-38}
  \fp_abs:N  \mynumber
  \fp_neg:N  \mynumber
 %\fp_sin:N  \mynumber
  \fp_use:N  \mynumber 
 }

% aliases 
\let\real\fp_new:N
\let\realset\fp_set:Nn
\let\realadd\fp_add:Nn
\let\realsub\fp_sub:Nn
\let\realdiv\fp_div:Nn
\let\realmul\fp_mul:Nn



\newcommand\sampleCalculations{%
  \real    \Z
  \realset \Z{150}
  \realsub \Z{5}
  \realadd \Z{-1}
  \realdiv \Z{12}
  \realmul \Z{0.0001}
  \Z
}

\ExplSyntaxOff

\sampleCalculation

\sampleCalculations


\end{document}
share|improve this question
    
For anyone stumbling upon this questions (and others like it), the form of the l3fp expressions used here is now deprecated; the expandable version (discussed by Joseph in his answer) is the only one that should be used now. –  Will Robertson Jul 1 at 7:29

1 Answer 1

up vote 10 down vote accepted

The FPU for LaTeX3 has undergone some major changes. Up to the version included in the DVD of TeX Live 2012 (mid-June 2012) the 'old' FPU worked one way. The improved FPU, available from the development repository and scheduled for release to CTAN some time in late June 2012, is expandable and features a number of improvements.

Updated (expandable) FPU answer

Most assignments should be done using \fp_set:Nn or \fp_gset:Nn. The second argument of those macros accepts arbitrary floating point expressions.

\fp_set:Nn \l_my_fp { sin ( 7.415e-1 + 2.4 ) }

Original (non-expandable) FPU answer

Currently, \fp_sin:Nn needs two arguments, the variable in which the result is stored and the angle (in radians):

\fp_sin:Nn \l_my_fp { 3.1415 }

The best syntax for fp operations is something that feedback would be very welcome on. The current thinking is that working out a sine makes more sense with separate input and output arguments than a hypothetical

\fp_set:Nn \l_my_fp { 3.1415 }
\fp_sin:N \l_my_fp

This is not set in stone: the fp module is much more recent than a lot of the rest of expl3 and thus needs a good work-out. This syntax question is one such area. Another is using degrees or radians. The calculation is in radians internally, so perhaps \fp_rad_to_deg:N and \fp_deg_to_rad:N might be a useful compromise approach.

I'd also point out that there are currently no arc functions: there was a need for sine and cosine for other purposes, so I got these done. I'll do the arc functions when the demand arises.

share|improve this answer
    
The code in l3fp is based largely on the well-known fp package. So most of the work done has been translation, although we've also gone for floating rather than fixed point numbers and have done some optimisation for speed. –  Joseph Wright Nov 23 '10 at 17:42
    
Joseph thanks, but I still get an error with \fp_new:N \l_my_fp, \fp_sin:Nn \l_my_fp{3.1416} even if I set to zero first and run it on its own. I am familiar with the fp package and I use it a lot. Most libraries use radians so IMHO it is a good idea to keep it as such as you say it is fairly easy to provide a function to change from the one to the other. Syntax is an issue, but can be changed by providing aliases and higher level functions. Libraries such as jQuery for JavaScript succeeded by simplifying the interface and using shorter variable names and chaining. Great work though thanks! –  Yiannis Lazarides Nov 23 '10 at 18:48
    
@Yiannis: Could you give a bit more detail on the error you get? I've just spotted a couple of bugs in the code for this area: there will be a CTAN update later this week. So if there are other fixes, I'd like to get them in. –  Joseph Wright Nov 23 '10 at 21:45
    
@Joseph it gives undefined control sequence. Code as per my comment above. –  Yiannis Lazarides Nov 24 '10 at 2:51
    
@Yiannis. Very odd: you did use \fp_sin:Nn and not \fp_sin:N, yes? Perhaps it's the version of expl3 you have: what's the version number of l3fp in your log? –  Joseph Wright Nov 24 '10 at 7:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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