Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on Compile-time rounding with the pre-processor

Parent

Compile-time rounding with the pre-processor

+8
−0

When searching the Internet, I came across this SO post Rounding in C Preprocessor but immediately noted that none of the answers are showing a way to perform actual rounding - rather, they are doing flooring to the nearest integer.

The post is actually misleading and got nothing to do with rounding, but with getting the appropriate integer type back from the macro. Yet it is the #1 search engine hit when searching for rounding the the preprocessor, which is kind of sad.

Is there a way to round a floating point expression to an integer at compile-time, using the pre-processor?

I suppose such code could also either be written in a type-generic way accepting any of the common arithmetic types as input, or alternatively in a type safe manner only accepting one specific type as input. Is there an advantage in using one form or the other?

History

0 comment threads

Post
+0
−0

If you can truncate (get the floor() of a number), then you can round the number by adding 0.5 first. Or, I suppose that, for the sake of completeness, you add "the equivalent of 0.5," since a given program might not represent the values in decimal or you might want to round to a non-ones position in the number.

When you round numbers (and you know this, otherwise you wouldn't make the distinction, but to explain beyond "add a half"), a (decimal) digit of five or higher bumps the next-most-significant place up by one, otherwise you round. Assuming rounding to the nearest integer, then, from 0.0 to 0.49̅, adding 0.5 increases to 0.5 to 0.99̅, so truncation goes to the lower value. Anything higher crosses the threshold to the next integer, so truncation gives you that higher value.

History

1 comment thread

Compile-time (3 comments)
Compile-time
Lundin‭ wrote 4 months ago

The question is how to do this at compile-time in the C programming language. Notably floor(), round() etc are run-time functions requiring the math library.

John C‭ wrote 4 months ago

Did I say "use functions"? I did not. The question says:

...none of the answers are showing a way to perform actual rounding - rather, they are doing flooring to the nearest integer.

And the point that, if a programmer can find the floor, then they can round, doesn't change.

Lundin‭ wrote 4 months ago

Ok so maybe hint of how to find the floor of an integer at compile time then - it is not done by using floor().