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

  • SEARCH
  • Home
  • 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 8517355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:40:38+00:00 2026-06-11T05:40:38+00:00

Is it possible to compute the square root of an integer with a metafunction

  • 0

Is it possible to compute the square root of an integer with a metafunction with the following signature :

template<unsigned int N> inline double sqrt();

(or maybe using the constexpr keyword, I don’t know what is the best).
With that, sqrt<2>() would be replaced by 1.414... at compile-time.

What would be the best implementation for a such function ?

  • 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-06-11T05:40:40+00:00Added an answer on June 11, 2026 at 5:40 am

    This may not be what you are looking for, but I wanted to make sure you realized that typically with optimization the compiler will calculate the result at compile time anyway. For example, if you have this code:

    void g()
    {
      f(sqrt(42));
    }
    

    With g++ 4.6.3 with optimization -O2, the resulting assembly code is:

       9 0000 83EC1C                subl    $28, %esp
      11 0003 DD050000              fldl    .LC0
      12 0009 DD1C24                fstpl   (%esp)
      13 000c E8FCFFFF              call    _Z1fd
      14 0011 83C41C                addl    $28, %esp
      16 0014 C3                    ret
      73                    .LC0:
      74 0000 6412264A              .long   1244009060
      75 0004 47EC1940              .long   1075440711
    

    The sqrt function is never actually called, and the value is just stored as part of the program.

    Therefore to create a function that technically meets your requirements, you simply would need:

    template<unsigned int N> inline double meta_sqrt() { return sqrt(N); }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Make it's possible to compute variable names in runtime using double-evaluation, like $($(var))
Given the following table: CREATE TABLE BitValues ( n int ) Is it possible
Is it possible to compute resulting css style on the element manually (without need
Is it possible to compute the hash of a class at runtime in C#
Is it possible to compute the number of different elements in an array in
Is it possible to directly compute the product (or for example sum) of two
Possible Duplicate: Python, compute list difference I have two lists For example: A =
Is it possible to have JavaScript compute a timestamp returned from PHP's time() function
Possible Duplicate: Compute fast log base 2 ceiling What is the fastest possible way
Given a tree (binary) structure, with depth N. Is it possible to compute some

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.