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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:07:41+00:00 2026-05-12T12:07:41+00:00

I am aware that casting ints to floats (and vice versa) is fairly expensive.

  • 0

I am aware that casting ints to floats (and vice versa) is fairly expensive. However, does the compiler automatically do it at compile time for constants in your code? For e.g. is there any difference between

float y = 123;
float x = 1 / y;

and

float y = 123.f;
float x = 1.f / y;

I see some code that does the latter, but I’m not sure if it’s for optimization or safety issues (ie just making sure that the divide is floating point even if y happens to be an int).

I’m using gcc (since the answer might be compiler specific.)

Also, any pointers to a list of what the compiler can and cannot optimize in general would be appreciated. Thanks!

  • 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-12T12:07:42+00:00Added an answer on May 12, 2026 at 12:07 pm

    Yes, the compiler will do the conversion automatically. Your two blocks of code are identical.

    It is not an optimization. Turning off optimization won’t make the compiler include the int-to-float conversion in the executable code, unless it’s a very poor-quality implementation.

    It’s not for safety, either. The compiler never does anything “just in case” an operand happens to be of a different type. The compiler knows the types of everything in your code. If you change the type of a variable, everything that uses that variable gets recompiled anyway; the compiler doesn’t try to keep everything else untouched and just update the changed sections.

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

Sidebar

Related Questions

im aware that j2se 6 has introduced the JavaCompiler API; however, it's cumbersome to
I am aware that XDebug's delta should not be considered accurate. However, when tracing
I am aware that Action Script does not provide multithreading so when writing flex
I am aware that suffixing a floating point number with f lets the compiler
I aware that Java object constructors implicitly initialize their instance's non-static fields. However, I'm
I aware that this will be a less programming question, but still... How can
I am aware that I can set null like string val = null; But
I'm aware that this is possible with the os module's os.system(color) function, but this
I am aware that MD5 hashes are not advisable for security any more but
Now I'm aware that adding listeners, then setting fields would trigger the change. So

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.