josephwright / siunitx

A comprehensive (SI) units package for LaTeX
LaTeX Project Public License v1.3c
348 stars 25 forks source link

exponent-base gives non-sensical results when parsing numbers with exponent-mode=scientific #736

Open JasonGross opened 2 weeks ago

JasonGross commented 2 weeks ago
\documentclass{article}
\usepackage{siunitx}
\begin{document}
\num[exponent-base=2,exponent-mode=scientific]{1024}
\end{document}

I expect to see something like 210, but instead I see 1.024 × 23. I get that someone might want to pass something non-numerical expression as exponent-base, like \mathbb{R} or whatever, but I'd quite like to have a version of exponent-base that was treated semantically.

josephwright commented 2 weeks ago

This setting is really intended for manually-set values, like \qty[exponent-base = 2]{1e4}{\byte} or similar.

JasonGross commented 2 weeks ago

I ended up using the following, which I guess works well enough

\makeatletter
\NewDocumentCommand{\NumAsPow}{oO{2}m}{\ensuremath{%
    \edef\NumAsPow@exp{\fpeval{ln(#3)/ln(#2)}}%
    \edef\NumAsPow@expf{\fpeval{floor(\NumAsPow@exp)}}%
    \edef\NumAsPow@mantissa{\fpeval{(#3)/(#2^{\NumAsPow@expf})}}%
    \IfNoValueTF{#1}{%
        #2^{\num[round-precision=0,round-mode=places]{\NumAsPow@exp}}%
    }{%
        \num[exponent-base=#2,#1]{\NumAsPow@mantissa e\NumAsPow@expf}%
    }%
}}
\makeatother