Solved

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

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now