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 4084284
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:25:13+00:00 2026-05-20T18:25:13+00:00

I have created the following functions in C#: float GetPI() { return 22.0f/7.0f; }

  • 0

I have created the following functions in C#:

float GetPI()
{
  return 22.0f/7.0f;
}

void Calculate()
{
  float f1 = GetPI()*4;
  float f2 = GetPI()*5;
}

If I create a release build, how will I be able to verify whether the JIT compiler will cache the 22/7 calculation, or whether it will be calculated each time the function GetPI is called?

PS : I do not have visual C# studio professional edition

EDIT : changed 22/7 to 22.0f/7.0f to make the example function more accurate

  • 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-20T18:25:13+00:00Added an answer on May 20, 2026 at 6:25 pm

    I’m not sure what you mean by being cached. Unless the compiler doesn’t do any constant folding (which it will in this case) or the runtime doesn’t inline your function (which it probably would in this case), it will not do any “caching” automatically for you.


    The expression 22f/7f is a constant expression and can be evaluated at compile time (aka, constant folding). So in the compiled assembly, it will appear as:

    float GetPI()
    {
      return 3.142857f; // however many digits of precision
    }
    

    The compiler will always make this kind of optimization if given the opportunity. Unless there’s a compiler option to disable this (which I’m not sure there is). To verify this happens, you could use Reflector (as the others have pointed out) to see the compiled assembly. You should see that it does indeed do this whether it is Debug or Release builds or using the express versions (unless this optimization is explicitly disabled).


    If the function is simple enough, the runtime might inline your function call. So rather than calling this function, it will insert the result in place of the function call. Seeing as this is just a function to just return some number, it will probably will do this optimization. I don’t know if there even is a way to see if this happens without actually seeing the JIT compiled code through a debugger.


    If you mean caching as in memoization, then no this will not be done automatically and you’ll have to do this yourself.


    If you want an absolute guarantee that the compiler will “cache” (as in not recalculate the quotient) the value, you should declare your value as a constant (or use the existing and more accurate System.Math.PI constant). Nothing is being determined at runtime, everything is known at compile time. And every time you use this value, it will be “inlined” so you won’t have to worry about that.

    const float PI = 22f / 7f; // will be stored as 3.142857f
    
    // or another way, if you need PI to be a float but want a more accurate value
    const float PI = (float)Math.PI; // will store as much precision possible
    // note that this only works because Math.PI is declared as a constant
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created the following XSLT that will ensure that the field being sent
I have the following code: public void SetMove(Position3D pos, float time, float linearity, bool
I have created following stored user defined it gets executed successfully. CREATE FUNCTION spherical_distance1(@a
I have the following function: CREATE FUNCTION [dbo].[ListStockBySubCategory] ( @CategoryID varchar(10), @SubCategoryID varchar(10), @startRowIndex
I have a strange one. Create a new form. Then add the following function
I have the following scalar function in MS SQL 2005: CREATE FUNCTION [dbo].[Distance] (
I have the following jQuery code: $('span.readMoreReputation').mouseover(function(event) { createTooltip(event); }).mouseout(function(){ // create a hidefunction
I have created following rule in the .htaccess file located at the root of
I have created following thing in android using android compatibility support package Basically i
I have the following function: void Register(Data* _pData, uint32 _Line, const char* _pFile, ...)

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.