auxarithmetic.h /size: 2909 b    last modification: 2024-01-16 10:22
1/*
2    See license.txt in the root of this project.
3*/
4
5# ifndef LMT_UTILITIES_ARITHMETIC_H
6# define LMT_UTILITIES_ARITHMETIC_H
7
8/* The |fabs| macro is used in mp. */
9
10/*tex
11
12There has always be much attention on accuracy in \TEX, especially in the perspective of portability. 
13Keep in mind that \TEX\ was written when there was no IEEE floating point defined so all happens in 
1416.16, or actually in 14.16 precission. We could actually consider going 16.16 if we use long integers 
15in some places but it needs some checking first. We could just accept wrapping around as that already
16happens in some places anyway (not all dimension calculation are checked).
17
18In \LUATEX\ and \LUAMETATEX\ we have the \LUA\ engine and that one was exclusively using doubles till 
195.3 when it went for a more hybrid approach. Because we go a lot between \TEX\ and \LUA\ (in \CONTEXT) 
20that had some consequences and rounding happens all over the place. It is also for that reason that 
21we now use doubles and rounding in some more places in the \TEX\ part: it is more consistent with what 
22happens at the \LUA\ end. And, because IEEE is common now, we are (afaiks) portable enough. 
23
24We don't use round but lround as that one rounds away from zero. In a few places we use llround. Also 
25in some places we clip to the official maxima but not always. 
26
27*/
28
29
30/*
31# undef abs
32# undef fabs
33
34# define abs(x)    ((int)(x) >= 0 ? (int)(x) : (int)-(x))
35# define fabs(x)   ((x) >= 0.0 ? (x) : -(x))
36*/
37
38# define odd(x)    ((x) & 1)
39
40# define lfloor(x) ( (lua_Integer)(floor((double)(x))) )
41# define tfloor(x) ( (size_t)     (floor((double)(x))) )
42# define ifloor(x) ( (int)        (floor((double)(x))) )
43
44//define lround(x) ( ((double) x >= 0.0) ? (lua_Integer) ((double) x + 0.5) : (lua_Integer) ((double) x - 0.5) )
45//define tround(x) ( ((double) x >= 0.0) ? (size_t)      ((double) x + 0.5) : (size_t)      ((double) x - 0.5) )
46//define iround(x) ( ((double) x >= 0.0) ? (int)         ((double) x + 0.5) : (int)         ((double) x - 0.5) )
47//define sround(x) ( ((double) x >= 0.0) ? (int)         ((double) x + 0.5) : (int)         ((double) x - 0.5) )
48
49//define lround(x) ( ((double) x >= 0.0) ? (lua_Integer) ((double) x + 0.5) : (lua_Integer) ((double) x - 0.5) )
50//define tround(x) ( ((double) x >= 0.0) ? (size_t)      ((double) x + 0.5) : (size_t)      ((double) x - 0.5) )
51//define iround(x) ( (int) lround((double) x) )
52
53//define zround(r) ((r>2147483647.0) ? 2147483647 : ((r<-2147483647.0) ? -2147483647 : ((r >= 0.0) ? (int)(r + 0.5) : ((int)(r-0.5)))))
54//define zround(r) ((r>2147483647.0) ? 2147483647 : ((r<-2147483647.0) ? -2147483647 : (int) lround(r)))
55
56# define scaledround(x)  ((scaled) lround((double) (x)))
57# define longlonground   llround
58# define clippedround(r) ((r>2147483647.0) ? 2147483647 : ((r<-2147483647.0) ? -2147483647 : (int) lround(r)))
59# define glueround(x)    clippedround((double) (x))
60
61# endif
62