ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Re: Calculating in TeX
@ 2001-12-06 14:06 e_w_g
  0 siblings, 0 replies; 5+ messages in thread
From: e_w_g @ 2001-12-06 14:06 UTC (permalink / raw)


Tobias Burnus schrieb:
> 
> I have a nice document where I need to perform small 
> calculation (actually 
> only calculating the average value):
>  if((#1+#2+#3) != 0)
>    (#1*1 + #2*2 + #3*3)/(#1+#2+#3)
>  else
>    "--"
> 
> Now I was thinking where I could do so in TeX. (I think 
> there are some 
> macros in ConTeXt, but I cannot find them.)
> 

There is the fp package for latex. It provides rather complete 
fixed-point arithmetic. I have never used it, but probably it is 
generic enough to work with context. If not, there is the fltpoint 
package which will definitely work, but it is limited to addition, 
subtraction, multiplication and division (if-then-else could be done 
too). 

Eckhart


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Calculating in TeX
       [not found] <Pine.BSF.4.30.0112052223001.3513-100000@chip.physik.fu-ber lin.de>
@ 2001-12-10  8:46 ` Hans Hagen
  0 siblings, 0 replies; 5+ messages in thread
From: Hans Hagen @ 2001-12-10  8:46 UTC (permalink / raw)
  Cc: ntg-context

At 10:31 PM 12/5/2001 +0100, Tobias Burnus wrote:
>Hi,
>
>I have a nice document where I need to perform small calculation (actually
>only calculating the average value):
>  if((#1+#2+#3) != 0)
>    (#1*1 + #2*2 + #3*3)/(#1+#2+#3)
>  else
>    "--"
>
>Now I was thinking where I could do so in TeX. (I think there are some
>macros in ConTeXt, but I cannot find them.)

You can play a bit with etex's \dimexpr and \numexpr (docu on tex live)

\def\Tobiased#1#2#3%
   {\ifdim\dimexpr(#1pt+#2pt+#3pt)=\zeropoint
    \else
       \expandafter\withoutpt\the\dimexpr
         (\dimexpr(1\dimexpr(#1pt) + 2\dimexpr(#2pt) + 3\dimexpr(#3pt))/
          \dimexpr(          #1pt  +           #2pt  +           #3pt))
    \fi}

\Tobiased{10}{150}{310.2}

So, te numbers become dimens and are stripped afterwards

>The output should be of the form "1{,}4" (i.e. separator is a {,} and I
>get one figure after the separator).

How about:

\def\OneAfter#1%
   {\expandafter\beforesplitstring#1\at.\to\Before
    \expandafter\aftersplitstring#1\at.\to\After
    \Before.\expandafter\AfterIndeed\After0000\relax}

\def\AfterIndeed#1#2#3\relax{#1}

\OneAfter{1.23}
\OneAfter{1}
\def\Oeps{1.234}
\OneAfter\Oeps

You can probably figure out the rest

Hans
-------------------------------------------------------------------------
                                   Hans Hagen | PRAGMA ADE | pragma@wxs.nl
                       Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
  tel: +31 (0)38 477 53 69 | fax: +31 (0)38 477 53 74 | www.pragma-ade.com
-------------------------------------------------------------------------


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Calculating in TeX
  2001-12-06  9:50 ` Giuseppe Bilotta
@ 2001-12-06 11:27   ` Taco Hoekwater
  0 siblings, 0 replies; 5+ messages in thread
From: Taco Hoekwater @ 2001-12-06 11:27 UTC (permalink / raw)
  Cc: tobias.burnus, ntg-context

"Giuseppe Bilotta" <bourbaki@bigfoot.com> wrote:
> 
> TB> Now I was thinking where I could do so in TeX. (I think there are
> TB> macros in ConTeXt, but I cannot find them.)
> 
> e-TeX has some arithmetic primitives. Don't know if they are
> somehow wrapped in ConTeXt, though.

It is rather straightforward in normal TeX as well, as long as they
are integers. Floats are a bit of a problem. For those, perhaps
a texutil plugin would be easier. Then again, that would alway
force another run.

Assuming integers, try this:

\def\mycalc#1#2#3{%
  \count1=#1
  \advance\count1 #2
  \advance\count1 #3
  \ifnum\count1 > 0
     \count3=#1
     \count5=#2
     \multiply \count5 2
     \advance\count3 \count5
     \count5=#3
     \multiply \count5 3
     \advance\count3 \count5
     % 
     \multiply\count3 10
     \divide \count3 \count1
     \expandafter\domycalc\the\count3\relax\quit
  \else
   --%
  \fi
}

\def\domycalc#1#2\quit{%
  \if#2\relax
    .#1%
  \else
    #1\domycalc#2\quit%
  \fi
}

\mycalc{1}{3}{5}

-- 
groeten,

Taco


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Calculating in TeX
  2001-12-05 21:31 Tobias Burnus
@ 2001-12-06  9:50 ` Giuseppe Bilotta
  2001-12-06 11:27   ` Taco Hoekwater
  0 siblings, 1 reply; 5+ messages in thread
From: Giuseppe Bilotta @ 2001-12-06  9:50 UTC (permalink / raw)
  Cc: ntg-context

Wednesday, December 05, 2001 Tobias Burnus wrote:

TB> Hi,

TB> I have a nice document where I need to perform small calculation (actually
TB> only calculating the average value):
TB>  if((#1+#2+#3) != 0)
TB>    (#1*1 + #2*2 + #3*3)/(#1+#2+#3)
TB>  else
TB>    "--"

TB> Now I was thinking where I could do so in TeX. (I think there are some
TB> macros in ConTeXt, but I cannot find them.)

e-TeX has some arithmetic primitives. Don't know if they are
somehow wrapped in ConTeXt, though.

TB> If there is no good solution I will write a script which calculates this
TB> values and does a inline replacement of the data. -- This is not that
TB> error save since one needs to make sure that this script is run before
TB> texexec...

I'll take the chance to reiterate the need for a plug-in (module)
structure in TeXUtil ...

--
Giuseppe "Oblomov" Bilotta


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Calculating in TeX
@ 2001-12-05 21:31 Tobias Burnus
  2001-12-06  9:50 ` Giuseppe Bilotta
  0 siblings, 1 reply; 5+ messages in thread
From: Tobias Burnus @ 2001-12-05 21:31 UTC (permalink / raw)


Hi,

I have a nice document where I need to perform small calculation (actually
only calculating the average value):
 if((#1+#2+#3) != 0)
   (#1*1 + #2*2 + #3*3)/(#1+#2+#3)
 else
   "--"

Now I was thinking where I could do so in TeX. (I think there are some
macros in ConTeXt, but I cannot find them.)

The output should be of the form "1{,}4" (i.e. separator is a {,} and I
get one figure after the separator).

If there is no good solution I will write a script which calculates this
values and does a inline replacement of the data. -- This is not that
error save since one needs to make sure that this script is run before
texexec...

Tobias


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2001-12-10  8:46 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-12-06 14:06 Calculating in TeX e_w_g
     [not found] <Pine.BSF.4.30.0112052223001.3513-100000@chip.physik.fu-ber lin.de>
2001-12-10  8:46 ` Hans Hagen
  -- strict thread matches above, loose matches on Subject: below --
2001-12-05 21:31 Tobias Burnus
2001-12-06  9:50 ` Giuseppe Bilotta
2001-12-06 11:27   ` Taco Hoekwater

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).