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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:46:33+00:00 2026-05-27T18:46:33+00:00

Suppose I have something like this: for (int i = 0; i < 1001;

  • 0

Suppose I have something like this:

for (int i = 0; i < 1001; i++)
{
    double step = i / 1000.0;

    // do some math here
}

Basically turning:

double step = i / 1000.0;

into this:

double step = i * 0.001;

I am not sure if this kind of change can be made without changing the result of the program, but was wondering if C# compiler or the jitter does something like this? If not, why? I assume either it’s not worth it or they didn’t add this optimization yet.

  • 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-27T18:46:34+00:00Added an answer on May 27, 2026 at 6:46 pm

    Let’s break it down into several questions:

    Can the jitter legally change d / 1000.0 into d * 0.001?

    No, because those two computations give different results. Remember, floating point numbers are binary fractions, not decimal fractions; that 0.001 as a double is not exactly equal to 1 / 1000 any more than 0.333333333 as a double is exactly equal to 1 / 3. 0.001 is the closest fraction to 1/1000 that can be expressed in 52 binary bits. And therefore there are values such that x / 1000.0 does not equal x * 0.001.

    Can the jitter legally change d / 2.0 into d * 0.5?

    Yes. In that case the values can be represented exactly in binary because 1/2 has a small power of two on the bottom.

    The jitter can also change integer divisions and multiplications like x / 2 or x * 2 into x >> 1 or x << 1.

    Does the jitter actually do so when it is legal?

    I don’t know. Try it!

    What you’ll want to do is compile the program “retail” and then start it up not in the debugger and run it until you know the code in question has been jitted. Then attach the debugger and examine the jitted code. The jitter will generate worse code if it knows that a debugger is attached, because it is trying to generate code that is easier to debug.

    I assume either it’s not worth it or they didn’t add this optimization yet.

    For the division-to-multiplication case you are assuming that multiplication is faster than division. Modern chips are pretty darn good at both; though division typically does require more bit operations it might be the case that the difference is negligable.

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

Sidebar

Related Questions

Suppose I have something like this int strLen; printf(Please enter a number: ); scanf(%d,
Suppose you have something like this: class intlist: def __init__(self,l = []): self.l =
Suppose I have code something like this: #include boost/thread/mutex.hpp using boost::mutex; typedef mutex::scoped_lock lock;
Suppose I have some code like this: class Visitor { public: Visitor(callBackFunction) {} void
Suppose I have a 2 column table like this: | user_id | int(11) |
Suppose you have a simple class like this: class foo{ private: int* mData; int
Suppose I have something like the following in test.cxx (and that I do the
Suppose I have a large number of strings formatted something like: <tag>blah blahXXXXXblah blah</tag>
Suppose I have a table of users. Something like: ID integer, USER text, POSITION
Suppose you have 2 classes like so: public class ClassA { public int X

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.