Questions tagged [ceil]

A function common to many programming languages that returns the next integer value by rounding the value up if necessary.

Returns the next highest integer value by rounding up value if necessary. php.net

Examples

echo ceil(1.3);    // returns   2
echo ceil(10.9);   // returns  11
echo ceil(-3.14);  // returns  -3
301 questions
5
votes
5 answers

PHP ceil function strange behavior ?

Can somebody explain this ? echo ceil( 20.7 * 100 ); // returns 2070 echo ceil( 2070 ); // returns 2070 all OK and logical, but echo ceil( 40.7 * 100 ); // returns 4071 echo ceil( 4070 ); // returns 4070 not OK and not logical... Why…
lorandd
  • 164
  • 3
  • 10
5
votes
3 answers

Alternative to ceil() and floor() to get the closest integer values, above and below of a floating point value?

I´m looking for an alternative for the ceil() and floor() functions in C, due to I am not allowed to use these in a project. What I have build so far is a tricky back and forth way by the use of the cast operator and with that the conversion from a…
5
votes
2 answers

Round a number to the nearest 0.05 ending with either a 0.025 or 0.075

A = [0.123 0.234 0.562 0.665 ] B = [0.125 0.225 0.575 0.675] C = [-43.975 -43.925 -43.875 -43.825 -43.775 -43.725 -43.675] I would like make array A look like array B. The satellite data I am working with has a 0.05 degree resolution so it's…
Cristina
  • 53
  • 3
5
votes
1 answer

Ruby round Float up or down to specific decimal significant figure

I'm using Ruby 2.3.1, and here's what I want to be able to do: 1.33333333.ceil(2) -> 1.34 1.33333333.floor(3) -> 1.333 The Float#round method allows me to round, but I need to be able to specify if I want to round up or down, similar to the #ceil…
jsmartt
  • 1,404
  • 1
  • 15
  • 22
5
votes
2 answers

counting N occurrences within a ceiling range of a matrix by-row

I would like to tally each time a value lies within a given range in a matrix by-row, and then sum these logical outcomes to derive a "measure of consistency" for each row. Reproducible example: m1 <- matrix(c(1,2,1,6,3,7,4,2,6,8,11,15), ncol=4,…
danny_C_O_T_W
  • 228
  • 2
  • 8
5
votes
1 answer

PHP ceil() and floor() returning float?

The PHP docs state the following for the ceil() function: Return Values value rounded up to the next highest integer. The return value of ceil() is still of type float as the value range of float is usually bigger than that of integer. floor()…
Audite Marlow
  • 1,034
  • 1
  • 7
  • 25
5
votes
1 answer

Fix floating point imprecision in ceiling

The problem: ceiling(31) #31 ceiling(31/60*60) #32 What is the correct way to fix this kind of errors? Doing the multiplication before the division is not an option, my code looks something like this: x <- 31/60 ... y <- ceiling(x*60) I'm…
pomber
  • 23,132
  • 10
  • 81
  • 94
5
votes
4 answers

Implement ceil() in C

I want to implement my own ceil() in C. Searched through the libraries for source code & found here, but it seems pretty difficult to understand. I want clean & elegant code. I also searched on SO, found some answer here. None of the answer seems to…
Green goblin
  • 9,898
  • 13
  • 71
  • 100
5
votes
5 answers

Fast Floor and Ceiling alternatives for positive System.Double values

double d = 0; // random decimal value with it's integral part within the range of Int32 and always positive. int floored = (int) Math.Floor(d); // Less than or equal to. int ceiled = (int) Math.Ceiling(d); // Greater than or equal to. int lessThan…
Raheel Khan
  • 14,205
  • 13
  • 80
  • 168
4
votes
4 answers

Can I trust a real-to-int conversion of the result of ceil()?

Suppose I have some code such as: float a, b = ...; // both positive int s1 = ceil(sqrt(a/b)); int s2 = ceil(sqrt(a/b)) + 0.1; Is it ever possible that s1 != s2? My concern is when a/b is a perfect square. For example, perhaps a=100.0 and b=4.0,…
Dan
  • 5,929
  • 6
  • 42
  • 52
4
votes
6 answers

PHP ceil gives wrong results if input is a float with no decimal

I've been wrestling with PHP's ceil() function giving me slightly wrong results - consider the following: $num = 2.7*3; //float(8.1) $num*=10; //float(81) $num = ceil($num); //82, but shouldn't this be 81?? $num/=10; //float(8.2) I have a number…
totallyNotLizards
  • 8,489
  • 9
  • 51
  • 85
4
votes
1 answer

Unexpected result with ceil() function

#include using namespace std; int main() { long long n,m; double ans; cin>>n>>m; ans=log(m)/log(n); cout<
4
votes
3 answers

Transact SQL: case when 1=1 then 0.5 else ceiling(sh) end /* returns 1 (!) why? */

-- Transact SQL: case when 1=1 then 0.5 else ceiling(sh) end /* returns 1 (!) why? */ declare @T table (h decimal(2,1)) insert @T (h) values (1.0) select case when 1=1 then 0.5 else ceiling(sh) end /* returns 1 (!) why? */ from @T T1 join…
Petr Kral
  • 43
  • 3
4
votes
1 answer

Math.Ceiling method

I am interessted in the actual Math.Ceiling function, how the algorithm behind this function is implemented in the .net framework. I've checked the function Math.Ceiling with a decompiler in mscorlib.dll , - but it seems like it is implemented by…
yq8
  • 145
  • 1
  • 10
4
votes
2 answers

Applying math.ceil to an array in python

What is the proper way to apply math.ceil to an entire array? See the following Python code: index = np.zeros(len(any_array)) index2 = [random.random() for x in xrange(len(any_array)) ##indexfinal=math.ceil(index2) <-? And I want to…
Brandon Ginley
  • 249
  • 3
  • 12