1

I've tried some examples with counters and numbers in LaTeX. I create a counter with \newcounter. I understand that it declares an integer. So if we make like this

\newcounter{num}
\setcounter{num}{10}
\divide\value{num} by 4\relax
\thenum

the result will be 2. It is an integer division (div) and the fractional part of the result is being neglected. Is there an opportunity to get 2.5 as a result in the example above? Maybe here we must define not a counter but a kind of float number? I also tried \newcount but the result is the same. Well, a count is an integer... Also I tried an example with \newlength:

\newlength\x
\setlength{\x}{10cm}
\divide\x by 4\relax
\the\x

The result is in pt's and if to convert it to cm's it would be about 2.5 cm. But I don't need lengths -- I am interested in just numbers. So how to define a float number and perform basic operations with it? Can we achieve this without using extra packages but with basic TeX/LaTeX capabilities?

Vladimir
  • 589
  • 2
    What are you trying to achieve with a counter variable that isn't integer-valued? – Mico Oct 14 '23 at 11:16
  • Are you willing and able to use LuaLaTeX? – Mico Oct 14 '23 at 11:17
  • 3
    \fpeval{10/4} will give 2.5. Or \newcommand\mynumber{10}\edef\mynumber{\fpeval{\mynumber/4}} \mynumber (there are also fp-variables in the expl3-layer.) – Ulrike Fischer Oct 14 '23 at 11:23
  • @Mico well, I don't have a particular purpose with it. I am trying to understand the counters in LaTeX. I've read a counter is an integer. I wrote some examples and found out that the fractional part is not saved. So I thought is there a way to define not only an integer but a float. For example, it can be used to calculate a coefficient of a length or just print it in a text. (No, I don't use LuaLaTeX, unfortunately) – Vladimir Oct 14 '23 at 11:27
  • @UlrikeFischer thank you, I've tried the examples, they both gave 2.5. So am I right there is no basic command like '\newcounter' but for float numbers? – Vladimir Oct 14 '23 at 11:30
  • 4
    tex has no floating point, but as Ulrike says, latex provides \fpeval – David Carlisle Oct 14 '23 at 11:33
  • @DavidCarlisle and am I right TeX has no floating point because it was originally designed like that to make it work the same on different computers (not to use calculations with a floating point to avoid errors)? – Vladimir Oct 14 '23 at 11:40
  • 7
    yes implementations are allowed to use floating point in glue stretching, but not where the result is accessable from tex. (you can not see, from tex how much an \hfill stretches) all tex length calculations are integer calculation in sp units and just use decimal pt value for display – David Carlisle Oct 14 '23 at 11:46
  • 2
    https://tex.stackexchange.com/questions/15526/floating-point-calculations-in-latex – Joseph Wright Oct 14 '23 at 12:39

0 Answers0