Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 1998-09-20
5
Medium Priority
?
775 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

730 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