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

**posted on**

- Water Lin

September 16, 2009, 7:12 am

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?

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

contradiction.

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?

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

#### Site Timeline

- » FAQ 5.27 How do I print to more than one file at once?
- — Next thread in » PERL Discussions

- » Re: Need expert help matching a line
- — Previous thread in » PERL Discussions

- » s suffix question
- — Newest thread in » PERL Discussions

- » Dell Battery Slice LED codes
- — The site's Newest Thread. Posted in » Laptop Computers Forum