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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:59:43+00:00 2026-06-17T17:59:43+00:00

Say I had this snippet of code: #include <cmath> // … float f =

  • 0

Say I had this snippet of code:

#include <cmath>

// ...

float f = rand();
std::cout << sin(f) << " " << sin(f);

As sin(f) is a well defined function there is an easy optimisation:

float f = rand();
float sin_f = sin(f);
std::cout << sin_f << " " << sin_f;

Is this an optimisation that it’s reasonable to expect a modern C++ compiler to do by itself? Or is there no way for the compiler to determine that sin(f) should always return the same value for an equal value of f?

  • 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-17T17:59:44+00:00Added an answer on June 17, 2026 at 5:59 pm

    Using g++ built with default optimization flags:

    float f = rand();
    40117e: e8 75 01 00 00          call   4012f8 <_rand>
    401183: 89 44 24 1c             mov    %eax,0x1c(%esp)
    401187: db 44 24 1c             fildl  0x1c(%esp)
    40118b: d9 5c 24 2c             fstps  0x2c(%esp)
    std::cout << sin(f) << " " << sin(f);
    40118f: d9 44 24 2c             flds   0x2c(%esp)
    401193: dd 1c 24                fstpl  (%esp)
    401196: e8 65 01 00 00          call   401300 <_sin>  <----- 1st call
    40119b: dd 5c 24 10             fstpl  0x10(%esp)
    40119f: d9 44 24 2c             flds   0x2c(%esp)
    4011a3: dd 1c 24                fstpl  (%esp)
    4011a6: e8 55 01 00 00          call   401300 <_sin>  <----- 2nd call
    4011ab: dd 5c 24 04             fstpl  0x4(%esp)
    4011af: c7 04 24 e8 60 40 00    movl   $0x4060e8,(%esp)
    

    Built with -O2:

    float f = rand();
    4011af: e8 24 01 00 00          call   4012d8 <_rand>
    4011b4: 89 44 24 1c             mov    %eax,0x1c(%esp)
    4011b8: db 44 24 1c             fildl  0x1c(%esp)
    std::cout << sin(f) << " " << sin(f);
    4011bc: dd 1c 24                fstpl  (%esp)
    4011bf: e8 1c 01 00 00          call   4012e0 <_sin>  <----- 1 call
    

    From this we can see that without optimizations the compiler uses 2 calls and just 1 with optimizations, empirically I guess, we can say the compiler does optimize the call.

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

Sidebar

Related Questions

Say I had this code $x = array("a", "b", "c", "d", "e"); Is there
So lets say I had this switch: switch(str){ case something: //a defined value //
I had this doubt for sometime now, some people say that there's no such
Looking for a non-JS solution. Say I had something like this: <ul> <li> <form>
Let's say you had a fairly basic client/server code, where each client creates three
Let's say I had: protected void performLogic(List<Object> docs) { ... } In the code
How would I loop through a multidimensional array? Say we had something like this:
Say you had this: class A { public string name; } And then you
Say if I had a string which had this text/html stored in it: Hello.
Can anyone please help me? Say if I had this text or a smaller

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.