TransWikia.com

Why the result of two equivalent TikZ math calculations is different?

TeX - LaTeX Asked by C.F.G on September 11, 2020

I am sure that there are many similar posts in TeX.SE but I couldn’t find it. I want to know why the result of the following equal expressions is different? (cos(30)*0.8=sqrt(3)*0.4)

documentclass[12pt, margin=3mm]{standalone}
usepackage{tikz}
usepackage{tikz-3dplot}
%usetikzlibrary{calc}
usetikzlibrary{arrows.meta}  
begin{document}
    
    begin{tikzpicture}[scale=10]
    coordinate (O) at (0,0,0);
    
    tdplotsetcoord{a}{0.4*sqrt(3)}{0}{0}
    draw[-stealth,color=blue] (O) -- (a);
    
    tdplotsetcoord{b}{0.8*cos(deg(30))}{0}{0}
    draw[-stealth,color=red, dashed] (O) -- (b);
    end{tikzpicture}
end{document}

enter image description here

One Answer

By default, pgfmath does trigonometric calculations using degrees, so rather than cos(deg(30)) you should use cos(30). With this correction, your MWE produces:

enter image description here

For completeness, I have also used pgfmathresult to explicitly show the numerical results of these calculations. Here is the updated code:

documentclass[12pt, margin=3mm]{standalone}
usepackage{tikz}
usepackage{tikz-3dplot}
%usetikzlibrary{calc}
usetikzlibrary{arrows.meta}
begin{document}

cos: pgfmathparse{0.8*cos(30)} pgfmathresult,
sqrt: pgfmathparse{0.4*sqrt(3)} pgfmathresult

    begin{tikzpicture}[scale=10]
    coordinate (O) at (0,0,0);

    tdplotsetcoord{a}{0.4*sqrt(3)}{0}{0}
    draw[-stealth,color=blue] (O) -- (a);

    tdplotsetcoord{b}{0.8*cos(30)}{0}{0}
    draw[-stealth,color=red, dashed] (O) -- (b);
    end{tikzpicture}
end{document}

Correct answer by Andrew on September 11, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP