Thu, 8 Nov 2007 21:01:00 +0100
|
On Thu, Nov 08, 2007 at 11:18:55AM +0100, Jonathan Sauer wrote:
> > Also both solutions have the disadvantage, that intermediate
> > results may cause an arithmetic overflow. As result the
> > accepted number range is limited unnecessarily.
> > Therefore I have choosen a more complicate route in package
> > `intcalc' by a case switch for zero, positive and negative numbers.
>
> It looks like that is the only way to be safe.
\def\divtrunc#1#2{%
\numexpr
\ifnum\numexpr#1=0 %
0%
\else
(#1%
\ifnum\numexpr#1<0 %
\ifnum\numexpr#2<0 %
-(#2+%
\else
+(#2-%
\fi
\else
\ifnum\numexpr#2<0 %
+(#2+%
\else
-(#2-%
\fi
\fi
1)/2)%
\fi
/(#2)%
\relax
}
\def\test#1#2#3{%
\immediate\write16{%
(#1) / (#2) = \number\divtrunc{#1}{#2} %
\ifnum\divtrunc{#1}{#2}=#3 [ok]\else <> #3 [error]\fi
}%
}
\test{-3}{1}{-3}
\test{-2}{1}{-2}
\test{-1}{1}{-1}
\test{0}{1}{0}
\test{1}{1}{1}
\test{2}{1}{2}
\test{3}{1}{3}
\test{-3}{-1}{3}
\test{-2}{-1}{2}
\test{-1}{-1}{1}
\test{0}{-1}{0}
\test{1}{-1}{-1}
\test{2}{-1}{-2}
\test{3}{-1}{-3}
\test{-4}{2}{-2}
\test{-3}{2}{-1}
\test{-2}{2}{-1}
\test{-1}{2}{0}
\test{0}{2}{0}
\test{1}{2}{0}
\test{2}{2}{1}
\test{3}{2}{1}
\test{4}{2}{2}
\test{-4}{-2}{2}
\test{-3}{-2}{1}
\test{-2}{-2}{1}
\test{-1}{-2}{0}
\test{0}{-2}{0}
\test{1}{-2}{0}
\test{2}{-2}{-1}
\test{3}{-2}{-1}
\test{4}{-2}{-2}
\test{-6}{3}{-2}
\test{-5}{3}{-1}
\test{-4}{3}{-1}
\test{-3}{3}{-1}
\test{-2}{3}{0}
\test{-1}{3}{0}
\test{0}{3}{0}
\test{1}{3}{0}
\test{2}{3}{0}
\test{3}{3}{1}
\test{4}{3}{1}
\test{5}{3}{1}
\test{6}{3}{2}
\test{-6}{-3}{2}
\test{-5}{-3}{1}
\test{-4}{-3}{1}
\test{-3}{-3}{1}
\test{-2}{-3}{0}
\test{-1}{-3}{0}
\test{0}{-3}{0}
\test{1}{-3}{0}
\test{2}{-3}{0}
\test{3}{-3}{-1}
\test{4}{-3}{-1}
\test{5}{-3}{-1}
\test{6}{-3}{-2}
\test{2147483647}{2}{1073741823}
\test{-2147483647}{2}{-1073741823}
\test{2147483647}{-2}{-1073741823}
\test{-2147483647}{-2}{1073741823}
\test{2147483647}{2147483647}{1}
\test{-2147483647}{2147483647}{-1}
\test{2147483647}{-2147483647}{-1}
\test{-2147483647}{-2147483647}{1}
% tests for expressions ...
\csname @@end\endcsname
\end
Yours sincerely
Heiko <[log in to unmask]>
|
|
|