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 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

I am aware that in .NET there are three timer types (see Comparing the
I am currently aware that ASP.NET 2.0 is out and about and that there
I'm fully aware that set division can be accomplished through a series of other
I am aware that you can lock an object in c# using lock but
I'm aware that Python 3 fixes a lot of UTF issues, I am not
I'm aware that in Vim I can often repeat a command by simply adding
I recently became aware that the strdup() function I've enjoyed using so much on
I'm aware that in WPF you want to keep the sizes of controls as
OK, I'm aware that triple-quotes strings can serve as multiline comments. For example, Hello,
Disclaimer Yes, I am fully aware that what I am asking about is totally

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.