I wonder if there is any functional difference between \the\numexpr and \number\numexpr. Some obscure context where they would yield different results?

(I'm obviously aware that \the and \number differ, wildly. I'm just curious about the case where they are followed by \numexpr.)

  • Without actually knowin anything, I always thought that \number was \the with the only difference that if followed by a dimension it would print the number of sp. What are the differences? – Manuel May 1 '16 at 17:49
  • In one sentence: \the expands to the value of an (any) internal quantity, while \number expands to the decimal representation of a number (integer). – Sašo Živanović May 1 '16 at 17:51
  • 2
    I don't think there are any differences assuming the csnames refer to the primitives., but would need to cite etex web sources to really justify an answer:-) – David Carlisle May 1 '16 at 17:53
  • 1
    @Manuel compare \the\font and \number\font – David Carlisle May 1 '16 at 18:15

While it seems that there is no difference in semantics (but I'm still waiting for answers here), there seems to be a small but consistent difference in performance.

First, \the consists of 4 characters and \number of 7, so you can type it almost twice as fast! ;-)

Seriously, \the\numexpr seems to be 10 - 15% faster than \number\numexpr. As it's not absolutely trivial to measure this, this is how I did it.

I ran pgf's profiler library on these three loops:

L1. \edef\a{\the\numexpr 40+2}

L2. \edef\a{\number\numexpr 40+2}

L3. \edef\a{}

... in the hope that subtracting the execution time of L3 from L1 (L2) gives me the time actually spent in \the\numexpr (\number\numexpr). (L1-L3) / (L2-L3) then yields about 85 - 90%.

To get a rough feel for the actual speed: a million repetitions of \the\numexpr takes about a second on my computer.

  • 2
    It's known that \the performs better than \number. – egreg May 2 '16 at 9:28
  • @egreg I had no idea. But it makes sense. And there's nothing like personal experience! ;-) – Sašo Živanović May 2 '16 at 9:57
  • 1
    all of xint uses \the\numexpr because of that. Other thing: \number\dimexpr is useful as it gives integer number in sp units. And a \dimexpr inside a \numexpr is tacitly converted this way. – user4686 May 2 '16 at 18:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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