Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ceil function: how can we implement it ourselves?

Tags:

I know that C++ provides us with a ceil function. For practice, I was wondering how can we implement the ceil function in C++. The signature of the method is public static int ceil(float num)

Please provide some insight.

I thought of a simple way: Convert num to a string, find the index of the decimal point, check if the decimal part is greater than 0. If yes, return num+1 else return num. But I want to avoid using the string conversion

like image 262
TimeToCodeTheRoad Avatar asked Dec 04 '11 18:12

TimeToCodeTheRoad


People also ask

How do you use ceil function?

The ceil() function takes a single argument and returns a value of type int . For example: If 2.3 is passed to ceil(), it will return 3. The function is defined in <math. h> header file.

How do you implement ceil in Python?

The method ceil(x) in Python returns a ceiling value of x i.e., the smallest integer greater than or equal to x. Syntax: import math math.ceil(x) Parameter: x:This is a numeric expression. Returns: Smallest integer not less than x.

Is ceil () is an user defined function?

Ceil function is a predefined function of math. h header file. It returns the nearest integer number, which is greater than or equal to the number passed as an argument in it.

Which library is used for ceil () floor () functions?

The Python ceil() function rounds a number up to the nearest integer, or whole number. Python floor() rounds decimals down to the nearest whole number. Both of these functions are part of the math Python library.


2 Answers

You can take apart the ingredients of an IEEE754 floating point number and implement the logic yourself:

#include <cstring>  float my_ceil(float f) {     unsigned input;     memcpy(&input, &f, 4);     int exponent = ((input >> 23) & 255) - 127;     if (exponent < 0) return (f > 0);     // small numbers get rounded to 0 or 1, depending on their sign      int fractional_bits = 23 - exponent;     if (fractional_bits <= 0) return f;     // numbers without fractional bits are mapped to themselves      unsigned integral_mask = 0xffffffff << fractional_bits;     unsigned output = input & integral_mask;     // round the number down by masking out the fractional bits      memcpy(&f, &output, 4);     if (f > 0 && output != input) ++f;     // positive numbers need to be rounded up, not down      return f; } 

(Insert the usual "not portable" disclaimer here.)

like image 133
fredoverflow Avatar answered Sep 20 '22 17:09

fredoverflow


Here is a naive implementation for positive numbers (this uses the fact that casting to (int) truncates toward zero):

int ceil(float num) {     int inum = (int)num;     if (num == (float)inum) {         return inum;     }     return inum + 1; } 

It is easy to extend this to work with negative numbers too.

Your question asked for a function returning int, but normally the ceil() function returns the same type as its argument so there are no problems with the range (that is, float ceil(float num)). For example, the above function will fail if num is 1e20.

like image 43
Greg Hewgill Avatar answered Sep 20 '22 17:09

Greg Hewgill