# How to find the smallest int to a number?

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

I want to find the easiest way to do such kind of thing:

for a number:
\$test = 4.34

I want to find a int which is bigger than \$test, but it is also the
smallest int to \$test. The result should be 5 if \$test = 4.34. The
result should also be -5 if \$test = -5.4.

Any built in way to do this in perl?

--
Water Lin's blog: http://blog.waterlin.org
Email: WaterLin@ymail.com

## Re: How to find the smallest int to a number?

You're looking for the ceil() function. It is available from the POSIX
module.

use POSIX qw(ceil);

//Makholm

## Re: How to find the smallest int to a number?

Not quite. The ceil function returns the smallest integer greater or
equal than the argument. But Water wants the smallest integer greater
than the argument. The floor function can be used to compute that:

#!/usr/bin/perl
use warnings;
use strict;

use POSIX qw(ceil floor);

for (-5.4, -3, 4.34, 5) {
printf("%5.2f %5.2f %5.2f\n", \$_, ceil(\$_), floor(\$_)+1);
}
__END__
-5.40 -5.00 -5.00
-3.00 -3.00 -2.00
4.34  5.00  5.00
5.00  5.00  6.00

hp

## Re: How to find the smallest int to a number?

No, ceil returns the largest integer greater or equal to the fraction.

He mistated, but ceil is what he needs, based on his expected result:
"The result should be 5 if \$test = 4.34. The
result should also be -5 if \$test = -5.4."

On the number line -5 is greater than -5.5 and 5 is greater than
4.34

if (-5 > -5.4) { print "-5 is greater than -5.4\n" }

-sln

## Re: How to find the smallest int to a number?

sln@netherlands.com schrieb:

Nonsense. What is - in your opinion - the largest integer?!

The answer is: there is no "largest integer"! Assume n is the largest
integer. Then n+1 is larger. So we have a very simple proof by

ceil returns the SMALLEST integer greater or equal than the argument,
just as Peter Holzer wrote.

Wolf

## Re: How to find the smallest int to a number?

As Wolf mentioned, that makes no sense, unless you expect to return
omega for every call to ceil().

How do you know he misstated?  Perhaps he really needs the integer
strictly greater than \$test.

What should the result be if \$test == 4?  Should it be 4 or 5?

If 4, then he needs floor(\$test).

If 5, then he needs ceil(\$test+1).

floor(\$test+1) equals ceil(\$test) except when \$test is an integer, as
Peter's code demonstrates (though that's not a proof).

--keith

--
kkeller-usenet@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
see X- headers for PGP signature information

## Re: How to find the smallest int to a number?

sln@netherlands.com wrote:

You are mistaken.

How do you know?

## Re: How to find the smallest int to a number?

It looks like you are describing the ceil() function.

Because ceil() completely describes exactly what you want to do,
adding .000000001 to \$test only causes \$result to be > \$test, that would
otherwise be equal, since both ceil/floor return the argument if a whole number.

\$result = ceil(\$test + .000000001)

-sln

## Re: How to find the smallest int to a number?

On Thu, 17 Sep 2009 17:25:42 -0700, sln@netherlands.com wrote:

^^^^^^^^^^^^^^^^

print ceil(4.99999999999999 + .000000001), "\n";

So don't use this because it prints 6 when it should be 5

-sln

## Re: How to find the smallest int to a number?

sln@netherlands.com wrote:

But now you've changed everything to get the result you wanted to
expect.  What's the point to doing that?