Math & Sciencenumerical conditions

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
kent
Posts: 57
Joined: Thu Oct 20, 2016 3:41 pm

numerical conditions

Post by kent »

I often should have liked to be able to test for numerical conditions in my PGF macros.
Let me illustrate with one simple example:
Let A and B be two given poinst where we have access to their coordinates, say A = (\xA,\yA) and B = (\xB,\yB).
I would then like to be able to check if A = B within some tolerance.
But how should I test to see, say, for \dx = \xB-\xA that abs(\dx) < \eps for a small defined value of \eps (i.e 0.0005)?
As far as I know the Latex \ifthenelse cannot be used.
Kent Holing, NORWAY

Recommended reading 2024:

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

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

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

mas
Posts: 226
Joined: Thu Dec 04, 2008 4:39 am

numerical conditions

Post by mas »

Looking at the pgf manual, Section 90, you can see that there is a pgfmath library which has commands to do what you need. I am just pasting below from the manual (page 939):
90.2
Comparison and logical functions
In addition to the commands described in Section 89.3.5, the following command was provided by Christian
Feuersänger:
\pgfmathapproxequalto{<x>}{<y>}
Defines \pgfmathresult 1.0 if |<x>−<y>|< 0.0001, but 0.0 otherwise. As a side-effect, the global boolean
\ifpgfmathcomparison will be set accordingly.

OS: Debian/GNU Linux; LaTeX System : TeXLive; Editor : Vim
kent
Posts: 57
Joined: Thu Oct 20, 2016 3:41 pm

numerical conditions

Post by kent »

Thanks, I see I need to upgrade the PGF version I am using or could anybody supply the code needed/mentioned?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

numerical conditions

Post by Stefan Kottwitz »

Hi Kent!

It's probably good to update in any case. I remember you worked on an intersection topic, that also was solvable with PGF 3 aka TikZ 3. It provides a lot more features than PGF 2. Otherwise you may re-program already existing new features.

You may consider to update the whole TeX installation. LaTeX evolves, new features, packages, and bug fixes appear.

Stefan
LaTeX.org admin
kent
Posts: 57
Joined: Thu Oct 20, 2016 3:41 pm

numerical conditions

Post by kent »

Since I recently got PGF 3.0 installed, I like to revisit the above.
What's wrong with the if statement below?

Code: Select all

\pgfmathapproxequalto{0.15}{0.1499999999};
\let\out\pgfmathresult;%here \out is 1.0 as it should
\ifthenelse{\ifpgfmathcomparison}{true}{false};%here I thought I should get true
% since I thought \ifpgfmathcomparison is true since \out is 1.0
Kent
kent
Posts: 57
Joined: Thu Oct 20, 2016 3:41 pm

numerical conditions

Post by kent »

By using \let\outt\pgfmathint{\out} I transformed 1.0 to 1 and I could then use \ifthenelse{\outt>0} etc..
But, I still wonder how to use the boolean \ifpgfmathcomparison.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

numerical conditions

Post by cgnieder »

\ifpgfmathcomparison can be checked like any boolean defined with \newif:

Code: Select all

\documentclass{article}

\usepackage{tikz,etoolbox,ifthen}

\begin{document}

\pgfmathapproxequalto{0.15}{0.1499999999}

% standard TeX:
\ifpgfmathcomparison
  true%
\else
  false%
\fi

% etoolbox:
\ifbool{pgfmathcomparison}{true}{false}

% ifthen:
\ifthenelse{\boolean{pgfmathcomparison}}{true}{false}

\end{document}
site moderator & package author
kent
Posts: 57
Joined: Thu Oct 20, 2016 3:41 pm

numerical conditions

Post by kent »

One more question, how do I compare numerically numbers in if statements such that
if(2.3>5.7}{false}{true}?
kent
Posts: 57
Joined: Thu Oct 20, 2016 3:41 pm

numerical conditions

Post by kent »

Reading the manuals more carefully, I managed to resolve myself. Sorry, for bothering you with this. :(
Post Reply