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