amath  1.6.2
Simple command line calculator
ceil.c File Reference
#include "prim.h"
Include dependency graph for ceil.c:

Go to the source code of this file.

Functions

double ceil (double x)
 Mathematical ceiling function. More...
 

Variables

static const double huge = 1.0e300
 

Function Documentation

double ceil ( double  x)

Mathematical ceiling function.

Version
1.3
Date
95/01/18
Return x rounded toward -inf to integral value
Method:
 Bit twiddling.
Exception:
 Inexact flag raised if x not equal to ceil(x).

Definition at line 65 of file ceil.c.

References huge.

66 {
67  sword i0,i1,j0;
68  uword i,j;
69  EXTRACT_WORDS(i0,i1,x);
70  j0 = ((i0>>20)&0x7ff)-0x3ff;
71  if(j0<20) {
72  if(j0<0) { /* raise inexact if x != 0 */
73  if(huge+x>0.0) { /* return 0*sign(x) if |x|<1 */
74  if(i0<0) {
75  i0=0x80000000;
76  i1=0;
77  }
78  else if((i0|i1)!=0) {
79  i0=0x3ff00000;
80  i1=0;
81  }
82  }
83  } else {
84  i = (0x000fffff)>>j0;
85  if(((i0&i)|i1)==0) return x; /* x is integral */
86  if(huge+x>0.0) { /* raise inexact flag */
87  if(i0>0) i0 += (0x00100000)>>j0;
88  i0 &= (~i);
89  i1=0;
90  }
91  }
92  } else if (j0>51) {
93  if(j0==0x400) return x+x; /* inf or NaN */
94  else return x; /* x is integral */
95  } else {
96  i = ((uword)(0xffffffff))>>(j0-20);
97  if((i1&i)==0) return x; /* x is integral */
98  if(huge+x>0.0) { /* raise inexact flag */
99  if(i0>0) {
100  if(j0==20) i0+=1;
101  else {
102  j = i1 + (1<<(52-j0));
103  // NOTICE: Is this a correct cast?
104  if((sword)j<(sword)i1) i0+=1; /* got a carry */
105  i1 = j;
106  }
107  }
108  i1 &= (~i);
109  }
110  }
111  INSERT_WORDS(x,i0,i1);
112  return x;
113 }
#define INSERT_WORDS(d, ix0, ix1)
Set a double from two 32 bit ints.
Definition: prim.h:187
static const double huge
Definition: ceil.c:46
#define EXTRACT_WORDS(ix0, ix1, d)
Get two 32 bit ints from a double.
Definition: prim.h:153
signed int sword
32 bit signed integer.
Definition: prim.h:107
unsigned int uword
32 bit unsigned integer.
Definition: prim.h:101

Variable Documentation

const double huge = 1.0e300
static

Definition at line 46 of file ceil.c.

Referenced by ceil().