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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:57:03+00:00 2026-05-15T01:57:03+00:00

I a writing a Simulation program and wondering if the use of const double

  • 0

I a writing a Simulation program and wondering if the use of const double is of any use when storing intermediate results. Consider this snippet:

double DoSomeCalculation(const AcModel &model) {
   (...)
   const double V = model.GetVelocity();
   const double m = model.GetMass();
   const double cos_gamma = cos(model.GetFlightPathAngleRad());
   (...)
   return m*V*cos_gamma*Chi_dot;
}

Note that the sample is there only to illustrate — it might not make to much sense from the engineering side of things. The motivation of storing for example cos_gamma in a variable is that this cosine is used many time in other expressions covered by (…) and I feel that the code gets more readable when using

cos_gamma 

rather than

cos(model.GetFlightPathAngleRad())

in various expressions. Now the actual is question is this: since I expect the cosine to be the same througout the code section and I actually created the thing only as a placeholder and for convenience I tend to declare it const. Is there a etablished opinion on wether this is good or bad practive or whether it might bite me in the end? Does a compiler make any use of this additional information or am I actually hindering the compiler from performing useful optimizations?

Arne

  • 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-15T01:57:04+00:00Added an answer on May 15, 2026 at 1:57 am

    Given your code:

    const double V = model.GetVelocity();
    const double m = model.GetMass();
    const double cos_gamma = cos(model.GetFlightPathAngleRad());
    

    I would probably leave cos_gamma as it is. I’d consider changing V and m to references though:

    const double &V = model.GetVelocity();
    const double &m = model.GetMass();
    

    This way you’re making it clear that these are strictly placeholders. It does, however, raise the possibility of lifetime issues — if you use a reference, you clearly have to ensure that what it refers to has sufficient lifetime. At least from the looks of things, this probably won’t be a problem though. First of all, GetVelocity() and GetMass() probably return values, not references (in which case you’re initializing the references with temporaries, and the lifetime of the temporary is extended to the lifetime of the reference it initializes). Second, even if you return an actual reference, it’s apparently to a member of the model, which (at a guess) will exist throughout the entire calculation in question anyway.

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

Sidebar

Related Questions

Right now I am writing a simulation program which output is formatted according to
I'm writing a program in C++ to perform a simulation of particular system. For
i am writing an event driven simulation program. i have 3 subclasses that inherit
I'm writing a program for a numerical simulation in C. Part of the simulation
I am writing a dhtml application that creates an interactive simulation of a system.
Writing something like this using the loki library , typedef Functor<void> BitButtonPushHandler; throws a
I'm writing a utility program with C# in WPF that allows users to create
I'm writing a simulation for class, and part of it involves the reproduction of
As an exercise I'm writing a program to calculate the odds of rolling 5
I am writing a iPhone program and testing it in the iPhone simulator. Can

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.