ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Wolfgang Schuster <schuster.wolfgang@googlemail.com>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Re: Dividing dimensions
Date: Mon, 23 Mar 2009 13:12:54 +0100	[thread overview]
Message-ID: <76FB4EC5-838D-4B83-AAE8-9BA544F45CE7@gmail.com> (raw)
In-Reply-To: <200903230815.15528.alan.braslau@cea.fr>


Am 23.03.2009 um 08:15 schrieb Alan BRASLAU:

> Can someone explain this?
> The solutions posted in lua seem more complicated
> than that using \dimexpr
>
> I will add further examples to
> http://wiki.contextgarden.net/Expressions
> (or someone else can do this directly, of course)
> once I understand the advantages and limitations.


Here is a example document.

Wolfgang


\starttext

\setupwhitespace[line]

Our todays question is how to divide two dimension and compare their
result with the result of another division, to do this we assign values
two four dimensions.

\startbuffer
\dimen0=15pt
\dimen2=20pt
\dimen4=15pt
\dimen6=30pt
\stopbuffer

\typebuffer \getbuffer

Because we can use \ETEX\ functions with \CONTEXT\ let's try to use  
them,
the following example should work:

\startbuffer
\ifdim\dimexpr\dimen0/\dimen2\relax<\dimexpr\dimen4/\dimen6\relax
     Second number is bigger.
\else
     First number is bigger.
\fi
\stopbuffer

\typebuffer \getbuffer

This seems to be working but did it also work when the first result of
the second division is bigger.

\startbuffer
\dimen6=15pt

\ifdim\dimexpr\dimen0/\dimen2\relax<\dimexpr\dimen4/\dimen6\relax
     Second number is bigger.
\else
     First number is bigger.
\fi
\stopbuffer

\typebuffer \getbuffer

This seems to be weird because we still get as result that the first  
number
is bigger but why did this happen? Let's take a closer look at the  
real values
of the dimensions and their results after the divison.

Dimen0 has a value of \the\dimen0{} which is \number\dimen0{} in  
scaled points.
Only the second value in scaled points is important for our  
calculation, we wan't
to also know what the value of the second dimensions is: \number 
\dimen2. The last
we want to know is the result of the division from the two number,  
this is
\number\dimexpr\dimen0/\dimen2\relax.

Before I want to say if this number is or good let's see what the  
value of
the second number is before we changed the value for dimension 6 and  
after
we changed it:

\startlines
Before: \dimen6=30pt \number\dimexpr\dimen4/\dimen6\relax
After:  \dimen6=15pt \number\dimexpr\dimen4/\dimen6\relax
\stoplines

The result is always 1 and not very helpful for us, you can see, if  
you to divide
two number which are nearly equal the result in \TEX\ is not ver  
helpful, to compensate
this we can multiply the first number in the division to get a better  
result.

We try this first with the pure number before we include this in the  
\type{\ifdim}
text we wanted.

\startlines
Result 1: \number\dimexpr\dimen0*100/\dimen2\relax
Result 2: \number\dimexpr\dimen4*100/\dimen6\relax
\stoplines

This gives us now to higher number we can compare with reliable results.
We will now try to test both this with our number above again.

\startbuffer
\dimen6=30pt

\ifdim\dimexpr\dimen0*100/\dimen2\relax<\dimexpr 
\dimen4*100/\dimen6\relax
     Second number is bigger.
\else
     First number is bigger.
\fi
\stopbuffer

\typebuffer \getbuffer

\startbuffer
\dimen6=15pt

\ifdim\dimexpr\dimen0*100/\dimen2\relax<\dimexpr 
\dimen4*100/\dimen6\relax
     Second number is bigger.
\else
     First number is bigger.
\fi
\stopbuffer

\typebuffer \getbuffer

You can see both tests give us a correct result after we multiplied  
the first
number in each division by 100 but should we rely on such limitations  
in \TEX\
when we can also use \LUATEX\ to get a correct result.

The \type{tex.dimen} function allows us to access the dimen register  
from \TEX\ in Lua.

\startbuffer
\dimen6=30pt

\startluacode
if tex.dimen[0] / tex.dimen[2] < tex.dimen[4] / tex.dimen[6] then
     tex.sprint("Second number is bigger.")
else
     tex.sprint("First number is bigger.")
end
\stopluacode
\stopbuffer

\typebuffer \getbuffer

\startbuffer
\dimen6=15pt

\startluacode
if tex.dimen[0] / tex.dimen[2] < tex.dimen[4] / tex.dimen[6] then
     tex.sprint("Second number is bigger.")
else
     tex.sprint("First number is bigger.")
end
\stopluacode
\stopbuffer

\typebuffer \getbuffer

The output is now correct in both values without any tricks.

\stoptext

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : https://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


  parent reply	other threads:[~2009-03-23 12:12 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-21 22:08 Aditya Mahajan
2009-03-21 22:26 ` Arthur Reutenauer
2009-03-21 22:41   ` Wolfgang Schuster
2009-03-21 22:49     ` Aditya Mahajan
2009-03-21 23:00       ` Wolfgang Schuster
2009-03-23  7:15     ` Alan BRASLAU
2009-03-23 11:35       ` Arthur Reutenauer
2009-03-23 12:12       ` Wolfgang Schuster [this message]
2009-03-23 12:22         ` Arthur Reutenauer
2009-03-23 23:27         ` Alan BRASLAU
2009-03-24  6:51           ` Mojca Miklavec
2009-03-24  7:07             ` Hans Hagen
2009-03-22  8:38   ` Wolfgang Schuster
2009-03-22  0:02 ` Marcin Borkowski
2009-03-22  2:13   ` Aditya Mahajan
2009-03-22  9:41 ` Hans Hagen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=76FB4EC5-838D-4B83-AAE8-9BA544F45CE7@gmail.com \
    --to=schuster.wolfgang@googlemail.com \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).