Solved

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

Posted on 1998-09-20
5
770 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 20 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 84

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 how to create, access, and change arrays in the C programming language.

821 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