473,473 Members | 1,571 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

problem with ceil function

momotaro
357 Contributor
am trying to implement the ceil function but i dont know where am i wrong....???!!
Plz help! this is my code:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int myCeil(double);
  4.  
  5. main()
  6. {
  7.     int ans;
  8.     double x;
  9.     printf("enter your num: ");
  10.     scanf("%lf", &x);
  11.     ans = myCeil(x);
  12.     printf("%d", ans);
  13. }
  14.  
  15. int myCeil(double x)
  16. {
  17.   int ans = (int)x;
  18.   if (ans == x)
  19.       return (ans);
  20.   else return (ans + 1);
  21. }
Dec 15 '06 #1
8 4333
DeMan
1,806 Top Contributor
Works beautifully on my machine - is there a specific case yours fails for?
Dec 16 '06 #2
fiendzarmor
2 New Member
Your code is working well...
There's no error in your code...
What's the problem??
Dec 16 '06 #3
momotaro
357 Contributor
if the input is for example 1 the out put hsould 1 and in case the in put is 1,2 the function should return 2!! not 1!
Dec 16 '06 #4
macklin01
145 New Member
if the input is for example 1 the out put hsould 1 and in case the in put is 1,2 the function should return 2!! not 1!
This may or may not be the cause of your trouble, but you should never use '==' for doubles. Due to floating point error, equality will almost never happen. Whenever you want to compare doubles, you need to do something like this:

Expand|Select|Wrap|Line Numbers
  1. if( fabs( a - b ) < eps )
  2. {
  3.  // do something ...
  4. }
  5.  
where eps is something small, like 1e-8.

As for your test case, do you mean to test "myCeil(1.2) = 2"? I'm afraid that with the syntax of your sentence, I'm not sure if the "comma" is punctuation or a European decimal point. ;)

Lastly, I'm a little bit concerned about your function. It seems to me (and again, I may be misreading it) that you are assuming that the integer cast will always return the integer part of the number (i.e., is acting almost like a floor function). I'm not sure how entirely safe it is to assume that a compiler will cast a double to an integer in a certain way. You might want to consider using "modf" instead.

BTW, why aren't you using the built-in "ceil" function? I can understand not using the horrible built-in "rand" and "round" functions, but as I've seen, ceil works quite fine.

Thanks -- Paul
Dec 16 '06 #5
momotaro
357 Contributor
because this was part of the project which is to build our own library!
so own math.h ...== my "ceil"...
thx anyway!
Dec 16 '06 #6
momotaro
357 Contributor
Sorry loooooooooooooooool!
i was using "," instead of "." hahahhahaah!
Sorry again!
i was really wondring if i should give up programing!
Dec 16 '06 #7
macklin01
145 New Member
because this was part of the project which is to build our own library!
so own math.h ...== my "ceil"...
thx anyway!
Ahh, that makes sense. :) I can completely understand the desire to do that. I think you might want to look into "modf", then. -- Paul
Dec 16 '06 #8
momotaro
357 Contributor
Niiiiiiice function!
thx!
but my code work fine no need for modf!
but again thx for u r help!!
see soon!
Dec 16 '06 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Jack | last post by:
I have a problem that I can't seem to solve. I have checked the round, ceil and floor functions and they don't seem to do what I want. I have an entry like this <?php $tax = .065; $ad2day =...
4
by: Kevin Duggan | last post by:
Here is the URL of my page: http://www.kevsblues.com/CATER/din.html ________________________________________ I can not understand how to get this so the page reads "Only 9 days till...
6
by: RobG | last post by:
I am writing a script to move an absolutely positioned element on a page by a factor using style.top & style.left. The amount to move by is always some fraction, so I was tossing up between...
1
by: Joannes Vermorel | last post by:
I am currently trying to port a small open source scientfic library written in C++ to .Net. The code (including the VS solution) could be found at http://www.vermorel.com/opensource/selfscaling.zip...
8
by: Cott Lang | last post by:
I have come up with a simple query that runs horribly depending on the number of columns selected. select order_lines.* from orders, order_lines where orders.merchant_order_id = '11343445' and...
14
by: yansong1990 | last post by:
Uhm..it's my 1st time using this fuction...could someone provide anexample for me please? thanks
13
by: ptn | last post by:
Hi everyone, I was messing around with math.h and I got this error: """ /tmp/ccefZYYN.o: In function `digcount': itos.c:(.text+0x103): undefined reference to `log10' itos.c:(.text+0x111):...
7
by: MrIncognito | last post by:
Am I using this correctly? This line of code is supposed to find the number of bytes in one line of a 16 color bitmap, but it doesn't seem to work most of the time. long linesize = ceil((double)...
7
by: mingke | last post by:
I've learned that we can round up and down a float number by using Ceil and floor function. But, is this function still able to work if I the number I want to round up (or down) is zero? I have...
1
by: nwclark | last post by:
I am a noobie so pleas bear with me.. I am trying to link a image to a MYSQL variable. I have user photos that i would like to link this page. So far I created an image upload and renamed the...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.