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.
Post History
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 doi...
#2: Post edited
When searching the Internet, I came across this SO post [Rounding in C Preprocessor](https://stackoverflow.com/questions/3279620/rounding-in-c-preprocessor) but immediately noted that none of the answers are showing a way to perform actual _rounding_ - rather, they are dooing _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?
- When searching the Internet, I came across this SO post [Rounding in C Preprocessor](https://stackoverflow.com/questions/3279620/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?
#1: Initial revision
Compile-time rounding with the pre-processor
When searching the Internet, I came across this SO post [Rounding in C Preprocessor](https://stackoverflow.com/questions/3279620/rounding-in-c-preprocessor) but immediately noted that none of the answers are showing a way to perform actual _rounding_ - rather, they are dooing _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?
