# integerize function using abs(), floor(), ceil(), if-then-else

## Overview

8/7/2013 to my knowledge, I am the only one who has invented this algorithm. this is an intgerize(n) function. it comes in handy for programmable calculators, which usually don't come with such a function, so you need to make your own with functions that DO exist in the calculator you have, because all calcs are different just like computer languages are different, but calculators vary wildly more.

usually, ABS(), FLOOR(), CEIL() and IF-THEN_ELSE exist, and there is usually some sort of way to define a procedure/sub/function/macro.

the calculators around the \$100 price point such as the HP 50g and maybe some of the high end casios and the TI voyage 200, usually have the required functions to do decent programming.

integerize() for this page's purpose, int(), parseInt(), int64_t(), intval(), Int(), or your computer language of choice, truncates all digits past the decimal point (removes all digits past the decimal point). for example, if you have 1.1 or 1.6, you will get 1 as a result, and if you have -1.1 or -1.6, you will get -1 as a result. it simply chops off and throws away everything from the decimal point on to the right side and throws it away.

computer languages typically have their own conversion functions from floating point to integer and back. see the language's Language Reference in the documentation.

## the trouble with using just floor() or ceil()

what most people don't expect is that floor() and ceil() aren't integerize functions exactly. they behave strangely with negative (with floor()) or positive numbers (wih ceil()).

think about these definitions carefully and what they imply. if I remember right:

ceil is defined as taking the highest integer.

floor is defined as taking the lowest integer.

and it does just that, as we see below. this can mess you up when all you wanted to do was get the integer value of a float() on your calculator. one would think floor() would be the function of choice, but for just truncating the fractional digits off of the decimal point, floor's input domain is only 0 and positive numbers, and ceil's input domain is 0 and negative numbers, for that purpose only (to integerize()).

here are some sample runs of floor and ceil, to show you that integerize works as intended under unit testing (note the strange things ceil() does with positive numbers and floor() does with negative numbers):

Unit Tests: sample runs of floor(), integerize() and ceil()
floor()integerize()js parseInt()ceil()
```floor(2.6)=
floor(2.5)=
floor(2.1)=
floor(2.0)=
floor(1.6)=
floor(1.5)=
floor(1.1)=
floor(1.0)=
floor(0.6)=
floor(0.5)=
floor(0.1)=
floor(0)=
floor(-0.1)=
floor(-0.5)=
floor(-0.6)=
floor(-1.0)=
floor(-1.1)=
floor(-1.5)=
floor(-1.6)=
floor(-2.0)=
floor(-2.1)=
floor(-2.5)=
floor(-2.6)=
```
```integerize(2.6)=
integerize(2.5)=
integerize(2.1)=
integerize(2.0)=
integerize(1.6)=
integerize(1.5)=
integerize(1.1)=
integerize(1.0)=
integerize(0.6)=
integerize(0.5)=
integerize(0.1)=
integerize(0)=
integerize(-0.1)=
integerize(-0.5)=
integerize(-0.6)=
integerize(-1.0)=
integerize(-1.1)=
integerize(-1.5)=
integerize(-1.6)=
integerize(-2.0)=
integerize(-2.1)=
integerize(-2.5)=
integerize(-2.6)=
```
```js parseInt(2.6)=
js parseInt(2.5)=
js parseInt(2.1)=
js parseInt(2.0)=
js parseInt(1.6)=
js parseInt(1.5)=
js parseInt(1.1)=
js parseInt(1.0)=
js parseInt(0.6)=
js parseInt(0.5)=
js parseInt(0.1)=
js parseInt(0)=
js parseInt(-0.1)=
js parseInt(-0.5)=
js parseInt(-0.6)=
js parseInt(-1.0)=
js parseInt(-1.1)=
js parseInt(-1.5)=
js parseInt(-1.6)=
js parseInt(-2.0)=
js parseInt(-2.1)=
js parseInt(-2.5)=
js parseInt(-2.6)=
```
```ceil(2.6)=
ceil(2.5)=
ceil(2.1)=
ceil(2.0)=
ceil(1.6)=
ceil(1.5)=
ceil(1.1)=
ceil(1.0)=
ceil(0.6)=
ceil(0.5)=
ceil(0.1)=
ceil(0)=
ceil(-0.1)=
ceil(-0.5)=
ceil(-0.6)=
ceil(-1.0)=
ceil(-1.1)=
ceil(-1.5)=
ceil(-1.6)=
ceil(-2.0)=
ceil(-2.1)=
ceil(-2.5)=
ceil(-2.6)=
```

## integerize() algorithm in HP 50g RPL

```'S00' STO
IF (ABS(FLOOR(S00) < ABS(CEIL(S00)) THEN
FLOOR(S00)
ELSE
CEIL(S00)
END
{'S00'} PURGE
'INTEGERIZE' STO
```

this is for those of you who are gasping like a fish out of water for an integerize function. fortunately, there is already a MOD() to help us with the rest.

## integerize() algorithm in C (though best done and more efficiently with int()

```#include <math.h>
#include <stdint.h>
int64_t integerize(double n) {/*by Jim Michaels*/
if (abs(floor(n) < abs(ceil(n)) {
return floor(n);
} else {
return ceil(n);
}
}
```

## integerize() algorithm in javascript (though best done and more efficiently with parseInt()

```function integerize(n) {/*by Jim Michaels*/
if (Math.abs(Math.floor(n) < Math.abs(Math.ceil(n)) {
return Math.floor(n);
} else {
return Math.ceil(n);
}
}
```