1*59d799daSIngo Weinhold
2*59d799daSIngo Weinhold /*
3*59d799daSIngo Weinhold * M_APM - mapm_flr.c
4*59d799daSIngo Weinhold *
5*59d799daSIngo Weinhold * Copyright (C) 2001 - 2007 Michael C. Ring
6*59d799daSIngo Weinhold *
7*59d799daSIngo Weinhold * Permission to use, copy, and distribute this software and its
8*59d799daSIngo Weinhold * documentation for any purpose with or without fee is hereby granted,
9*59d799daSIngo Weinhold * provided that the above copyright notice appear in all copies and
10*59d799daSIngo Weinhold * that both that copyright notice and this permission notice appear
11*59d799daSIngo Weinhold * in supporting documentation.
12*59d799daSIngo Weinhold *
13*59d799daSIngo Weinhold * Permission to modify the software is granted. Permission to distribute
14*59d799daSIngo Weinhold * the modified code is granted. Modifications are to be distributed by
15*59d799daSIngo Weinhold * using the file 'license.txt' as a template to modify the file header.
16*59d799daSIngo Weinhold * 'license.txt' is available in the official MAPM distribution.
17*59d799daSIngo Weinhold *
18*59d799daSIngo Weinhold * This software is provided "as is" without express or implied warranty.
19*59d799daSIngo Weinhold */
20*59d799daSIngo Weinhold
21*59d799daSIngo Weinhold /*
22*59d799daSIngo Weinhold * $Id: mapm_flr.c,v 1.4 2007/12/03 01:39:12 mike Exp $
23*59d799daSIngo Weinhold *
24*59d799daSIngo Weinhold * This file contains the floor and ceil functions
25*59d799daSIngo Weinhold *
26*59d799daSIngo Weinhold * $Log: mapm_flr.c,v $
27*59d799daSIngo Weinhold * Revision 1.4 2007/12/03 01:39:12 mike
28*59d799daSIngo Weinhold * Update license
29*59d799daSIngo Weinhold *
30*59d799daSIngo Weinhold * Revision 1.3 2002/11/05 23:25:30 mike
31*59d799daSIngo Weinhold * use new set_to_zero function instead of copy
32*59d799daSIngo Weinhold *
33*59d799daSIngo Weinhold * Revision 1.2 2002/11/03 21:47:43 mike
34*59d799daSIngo Weinhold * Updated function parameters to use the modern style
35*59d799daSIngo Weinhold *
36*59d799daSIngo Weinhold * Revision 1.1 2001/03/25 20:53:29 mike
37*59d799daSIngo Weinhold * Initial revision
38*59d799daSIngo Weinhold */
39*59d799daSIngo Weinhold
40*59d799daSIngo Weinhold #include "m_apm_lc.h"
41*59d799daSIngo Weinhold
42*59d799daSIngo Weinhold /*
43*59d799daSIngo Weinhold * input floor ceil
44*59d799daSIngo Weinhold * ----- ------ ------
45*59d799daSIngo Weinhold * 329.0 329.0 329.0
46*59d799daSIngo Weinhold * -837.0 -837.0 -837.0
47*59d799daSIngo Weinhold * 372.64 372.0 373.0
48*59d799daSIngo Weinhold * -237.52 -238.0 -237.0
49*59d799daSIngo Weinhold */
50*59d799daSIngo Weinhold
51*59d799daSIngo Weinhold /****************************************************************************/
52*59d799daSIngo Weinhold /*
53*59d799daSIngo Weinhold * return the nearest integer <= input
54*59d799daSIngo Weinhold */
m_apm_floor(M_APM bb,M_APM aa)55*59d799daSIngo Weinhold void m_apm_floor(M_APM bb, M_APM aa)
56*59d799daSIngo Weinhold {
57*59d799daSIngo Weinhold M_APM mtmp;
58*59d799daSIngo Weinhold
59*59d799daSIngo Weinhold m_apm_copy(bb, aa);
60*59d799daSIngo Weinhold
61*59d799daSIngo Weinhold if (m_apm_is_integer(bb)) /* if integer, we're done */
62*59d799daSIngo Weinhold return;
63*59d799daSIngo Weinhold
64*59d799daSIngo Weinhold if (bb->m_apm_exponent <= 0) /* if |bb| < 1, result is -1 or 0 */
65*59d799daSIngo Weinhold {
66*59d799daSIngo Weinhold if (bb->m_apm_sign < 0)
67*59d799daSIngo Weinhold m_apm_negate(bb, MM_One);
68*59d799daSIngo Weinhold else
69*59d799daSIngo Weinhold M_set_to_zero(bb);
70*59d799daSIngo Weinhold
71*59d799daSIngo Weinhold return;
72*59d799daSIngo Weinhold }
73*59d799daSIngo Weinhold
74*59d799daSIngo Weinhold if (bb->m_apm_sign < 0)
75*59d799daSIngo Weinhold {
76*59d799daSIngo Weinhold mtmp = M_get_stack_var();
77*59d799daSIngo Weinhold m_apm_negate(mtmp, bb);
78*59d799daSIngo Weinhold
79*59d799daSIngo Weinhold mtmp->m_apm_datalength = mtmp->m_apm_exponent;
80*59d799daSIngo Weinhold M_apm_normalize(mtmp);
81*59d799daSIngo Weinhold
82*59d799daSIngo Weinhold m_apm_add(bb, mtmp, MM_One);
83*59d799daSIngo Weinhold bb->m_apm_sign = -1;
84*59d799daSIngo Weinhold M_restore_stack(1);
85*59d799daSIngo Weinhold }
86*59d799daSIngo Weinhold else
87*59d799daSIngo Weinhold {
88*59d799daSIngo Weinhold bb->m_apm_datalength = bb->m_apm_exponent;
89*59d799daSIngo Weinhold M_apm_normalize(bb);
90*59d799daSIngo Weinhold }
91*59d799daSIngo Weinhold }
92*59d799daSIngo Weinhold /****************************************************************************/
93*59d799daSIngo Weinhold /*
94*59d799daSIngo Weinhold * return the nearest integer >= input
95*59d799daSIngo Weinhold */
m_apm_ceil(M_APM bb,M_APM aa)96*59d799daSIngo Weinhold void m_apm_ceil(M_APM bb, M_APM aa)
97*59d799daSIngo Weinhold {
98*59d799daSIngo Weinhold M_APM mtmp;
99*59d799daSIngo Weinhold
100*59d799daSIngo Weinhold m_apm_copy(bb, aa);
101*59d799daSIngo Weinhold
102*59d799daSIngo Weinhold if (m_apm_is_integer(bb)) /* if integer, we're done */
103*59d799daSIngo Weinhold return;
104*59d799daSIngo Weinhold
105*59d799daSIngo Weinhold if (bb->m_apm_exponent <= 0) /* if |bb| < 1, result is 0 or 1 */
106*59d799daSIngo Weinhold {
107*59d799daSIngo Weinhold if (bb->m_apm_sign < 0)
108*59d799daSIngo Weinhold M_set_to_zero(bb);
109*59d799daSIngo Weinhold else
110*59d799daSIngo Weinhold m_apm_copy(bb, MM_One);
111*59d799daSIngo Weinhold
112*59d799daSIngo Weinhold return;
113*59d799daSIngo Weinhold }
114*59d799daSIngo Weinhold
115*59d799daSIngo Weinhold if (bb->m_apm_sign < 0)
116*59d799daSIngo Weinhold {
117*59d799daSIngo Weinhold bb->m_apm_datalength = bb->m_apm_exponent;
118*59d799daSIngo Weinhold M_apm_normalize(bb);
119*59d799daSIngo Weinhold }
120*59d799daSIngo Weinhold else
121*59d799daSIngo Weinhold {
122*59d799daSIngo Weinhold mtmp = M_get_stack_var();
123*59d799daSIngo Weinhold m_apm_copy(mtmp, bb);
124*59d799daSIngo Weinhold
125*59d799daSIngo Weinhold mtmp->m_apm_datalength = mtmp->m_apm_exponent;
126*59d799daSIngo Weinhold M_apm_normalize(mtmp);
127*59d799daSIngo Weinhold
128*59d799daSIngo Weinhold m_apm_add(bb, mtmp, MM_One);
129*59d799daSIngo Weinhold M_restore_stack(1);
130*59d799daSIngo Weinhold }
131*59d799daSIngo Weinhold }
132*59d799daSIngo Weinhold /****************************************************************************/
133