Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6318759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:40:54+00:00 2026-05-24T15:40:54+00:00

I have a function in C that accepts and returns a double (and uses

  • 0

I have a function in C that accepts and returns a double (and uses several doubles internally). Is there a good way to make a second version of the function, just like it except with float in place of double? Also constants like DBL_EPSILON should be updated.

I suppose I could do this with the preprocessor, but that seems awkward (and probably difficult to debug if there’s a compile error). What do best practices recommend? I can’t imagine I’m the only one who’s had to deal with this.

Edit: I forgot, this is stackoverflow so I can’t just ask a question, I have to justify myself. I have code which is very sensitive to precision in this case; the cost of using doubles rather than floats is 200% to 300%. Up until now I only needed a double version — when I needed it I wanted as much precision as possible, regardless of the time needed (in that application it was a tiny percentage). But now I’ve found a use that is sensitive to speed and doesn’t benefit from the extra precision. I cringed at my first thought, which was to copy the entire function and replace the types. Then I thought that a better approach would be known to the experts at SO so I posted here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T15:40:55+00:00Added an answer on May 24, 2026 at 3:40 pm

    don’t know about “best practices”, but the preprocessor definitely was the first thing to jump to my mind. it’s similar to templates in C++.

    [edit: and the Jesus Ramos answer mentions the different letters on functions with different types in libraries, and indeed you would probably want to do this]

    you create a separate source file with your functions, everywhere you have a double change it to FLOATING_POINT_TYPE (just as an example) and then include your source file twice from another file. (or whatever method you choose you just need to be able to ultimately process the file twice, once with each data type as your define.) [also to determine the character appended to distinguish different versions of the function, define FLOATING_POINT_TYPE_CHAR]

    #define FLOATING_POINT_TYPE double
    #define FLOATING_POINT_TYPE_CHAR d
    #include "my_fp_file.c"
    #undef FLOATING_POINT_TYPE_CHAR
    #undef FLOATING_POINT_TYPE
    
    #define FLOATING_POINT_TYPE float
    #define FLOATING_POINT_TYPE_CHAR f
    #include "my_fp_file.c"
    #undef FLOATING_POINT_TYPE
    #undef FLOATING_POINT_TYPE_CHAR
    

    then you can also use a similar strategy for your prototypes in your headers.

    but, so in your header file you would need something something like:

    #define MY_FP_FUNC(funcname, typechar) \
       funcname##typechar
    

    and for your function definitions/prototypes:

    FLOATING_POINT_TYPE
      MY_FP_FUNC(DoTheMath, FLOATING_POINT_TYPE_CHAR) 
      (
        FLOATING_POINT_TYPE Value1,
        FLOATING_POINT_TYPE Value2
      );
    

    and so forth.

    i’ll definitely leave it to someone else to talk about best practices 🙂

    BTW for an example of this kind of strategy in a mature piece of software you can check out FFTW (fftw.org), although it’s a bit more complicated than the example i think it uses basically the same strategy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.