Text FormattingDefinition of non-trivial numerical Values

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Definition of non-trivial numerical Values

Post by svend_tveskaeg »

Hi all.

Inspired by Clemens' answer in siunitx | Calculated Value in physical Quantities, I would like to define numbers like

Code: Select all

\def\horse{\the\numexpr sqrt(3)-sqrt(2) \relax}
I.e., involving square roots.

I have tried

Code: Select all

\def\horse{\the\numexpr 3^(1/2)-2^(1/2) \relax}
but that is not correct. (Therefore, it would also be nice to know how to define expressions involving powers.)

How do I do this?

Furthermore, why does the following not produce any output for \area?

Code: Select all

\documentclass{article}

\usepackage{auto-pst-pdf,pstricks-add}
\usepackage{siunitx}

\begin{document}

\begin{figure}
\def\minimum{0 }
\def\maximum{10 }
\def\length{\the\numexpr \maximum-\minimum \relax}
\def\PI{3.1415926535897932384626 }
\def\area{\the\numexpr 1/2*(\PI-2)*\length*\length \relax}
 \centering
  \begin{pspicture}(\maximum,\maximum)
   \pnode(!\minimum \minimum){A}
   \pnode(!\minimum \maximum){B}
   \pnode(!\maximum \maximum){C}
   \pnode(!\maximum \minimum){D}
   \pspolygon(A)(B)(C)(D)
   \psarc(!\maximum \minimum){!\length}{90}{180}
   \psarc(!\minimum \maximum){!\length}{270}{360}
  \psset{offset=12pt,nrot=:U}
   \psIntersectionPoint(A)(C)(B)(D){Y}
   \rput(Y){$A = \SI{\area}{\square\cm}$}
   \pcline{|-|}(B)(C)
   \ncput*{\SI{\length}{\cm}}
   \pcline{|-|}(C)(D)
   \ncput*{\SI{\length}{\cm}}
  \end{pspicture}
\end{figure}

\end{document}
Thank you in advance!
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Definition of non-trivial numerical Values

Post by localghost »

Actually TeX is not a calculator, at least not a good one. Its calculation capabilities are enhanced with the upcoming and at the moment experimental LaTeX3 programming language. As far as I know, the »l3fp« package from the l3kernel bundle offers some more convenient structures. And perhaps the mathematical engine of the pgf package comes also into question.


Thorsten
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Definition of non-trivial numerical Values

Post by cgnieder »

I'm unsure why your \area fails, but »l3fp« actually is a good choice as an alternative:

Code: Select all

\documentclass{article}
\usepackage{expl3}

\ExplSyntaxOn
\cs_new_eq:NN \calculate \fp_eval:n
\ExplSyntaxOff

\def\minimum{0 }
\def\maximum{10 }
\def\length{\calculate{\maximum-\minimum}}
\def\horse{\calculate{3^.5 - 2^.5}}
\def\area{\calculate{1/2*(pi-2)*\length*\length}}
\begin{document}

\noindent
\horse\\
\length\\
\area

\end{document}
Further reading: interface3

Regards
site moderator & package author
CrazyHorse
Posts: 351
Joined: Sat Aug 02, 2008 8:47 am

Definition of non-trivial numerical Values

Post by CrazyHorse »

svend_tveskaeg wrote: Inspired by Clemens' answer in siunitx | Calculated Value in physical Quantities, I would like to define numbers like
it is a bit tricky but possible:

Code: Select all

\def\minimum{0 }
\def\maximum{10 }
\def\length{\the\numexpr \maximum-\minimum \relax}
\def\PI{3.1415923}
\newlength\lTMP
\lTMP=\PI pt
\advance \lTMP by -2pt
\lTMP=\length\lTMP 
\lTMP=\length\lTMP 
\lTMP=0.5\lTMP
\makeatletter \edef\area{\strip@pt\lTMP} \makeatother
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Definition of non-trivial numerical Values

Post by svend_tveskaeg »

@Thorsten, Clemens, and Herbert: Thank you very much!

One last thing regarding this: How do I get only two decimals accuracy, using Herbert's implementation?

I tried

Code: Select all

\rput(Y){$A \approx \SI[round-precision=2]{\areal}{\square\cm}$}
but then I (still) get

Code: Select all

A \approx 57,07932
instead of

Code: Select all

A \approx 57,08
Last edited by svend_tveskaeg on Sat Oct 06, 2012 3:17 am, edited 2 times in total.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Definition of non-trivial numerical Values

Post by cgnieder »

You need to set siunitx' round-mode which is off as default:

Code: Select all

\sisetup{round-mode=places}
Regards
site moderator & package author
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Re: Definition of non-trivial numerical Values

Post by svend_tveskaeg »

I see. Thank you!
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
Post Reply