Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

What do u think? My own floor/ceil func...

Posted on 1998-09-20
5
Medium Priority
?
776 Views
Last Modified: 2012-06-27
hi guys
  I am out of points so pls. do try to help... :(
I am writing my own floor/ceil implementation without using those from 'math.h' lib and I want your opinions on the efficiency and possible bugs/flaws on my coding design...
I am using macros..
/* conversion from float/double to nearest integer */
#define floor(a) (((a - (int)a)>0)? (int)a:a)
#define ceil(a) (((a - (int)a)>0)? ((int)a + 1):a)

usage :
  eg.
  1. if I key in a = 22.3, (a-(int)a) = 22.3 - 22=0.3 > 0
so I would ret (int)a = 22 ( for Floor() )
  2. if I key in a = 22.3, (a-(int)a) = 22.3 - 22.0 = 0.3
>0 so I would ret ((int)a+1) = 22+1 = 23 (for Ceil())

I am not sure whether the syntax is ok as I haven't tested it yet... :)

David
0
Comment
Question by:Haho
5 Comments
 
LVL 10

Accepted Solution

by:
rbr earned 60 total points
ID: 1252904
It's is very dangerous to use such an function as a macro. e.g. Try


double x=23.2;

printf ("%d", floor (x++));

What will be the result and the value after this statement. Better you use a real function.
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1252905
To make your floor function more efficient you could eliminate the test ((a - (int)a)>0) and just define floor as

#define floor(a) (int)a

The test that you currently have just filters out numbers that are integers. Therefore numbers that are already integers are not converted.  This test does not have to be performed because if the number is already an integer, typecasting it as an integer won't change it anyway!  Defining it as above will save the time of a subtraction and a test.

My advice applies only to your floor function.  As far as I can see, the ceiling function requires the test.

scrapdog
0
 
LVL 1

Author Comment

by:Haho
ID: 1252906
Thank rbr and scrapdog ( thanks for the obvious!!I missed that...  :)),
I am waiting to see if anybody else has any comments/answers....
cause it is important that this 'floor'/'ceil'  works without any major flaws or bugs as I am using it for a commercial application...
BTW, I heard that floor/ceil from the math.h lib has some possible flaws...
I don't know how true it is ... do u guys know anything bout this??

David
0
 
LVL 85

Expert Comment

by:ozo
ID: 1252907
What if you have

 double x = INT_MAX + 1.5;

What possible flaws have you heard of in math.h
Who wrote your math.h?

#define floor(a) (int)a
doesn't work for negative a
0
 
LVL 1

Author Comment

by:Haho
ID: 1252908
thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question