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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:12:09+00:00 2026-05-31T13:12:09+00:00

I am confused as to this part of exemplary code: float f = 3.01;

  • 0

I am confused as to this part of exemplary code:

float f = 3.01;
int i;
  • Case 1: i = f * 100; // i == 300
  • Case 2: i = f *= 100; // i == 301

I’ve no idea why in first case the decimal part gets “lost”.
I’ve also tried:

(double) f * 100
f * 100.0

Then I tried volatile and flags to gcc that turn off all optimization.
The result is still the same.

Can somebody explain to me why does the first case behave so? Thank you.

  • 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-31T13:12:10+00:00Added an answer on May 31, 2026 at 1:12 pm

    The issue is in type conversion:

    int i = f * 100 ;
    

    is equivalent to

    double tmp = f* 100.0;
    int i = tmp;
    

    And the difference with second case is that tmp is double! And yes, there is a case, where double is “less precise”

    To illustrate this, I wrote the follwing program:

    int main()
    {
        float f = 3.01;
        float mf = f * 100;
        double md = f * 100;
    
        int if_ = mf;
        int id = md;
    
        std::cout << f << ' ' << mf << ' ' << md << ' ' << ' ' << (mf - md) << ' ' << if_ << ' ' << id << '\n';
    }
    

    Edit:

    On my system I get same result for my gcc 4.6.1 and cl 16.00 (MS VS 10):

    3.01 301 301  9.53674e-007 301 300
    

    Edit2

    Found, that enabling optimization -O2 eliminates issue. This explains difference with IDEONE.

    Here is another code:

    int main()
    {
        float f = 3.01;
        float mf = f * 100;
        return mf + (f*100);
    
        //std::cout << f << ' ' << mf << ' ' << md << ' ' << ' ' << (mf - md) << ' ' << if_ << ' ' << id << '\n';
    }
    

    And its assembly with -O0

        movl    $0x4040a3d7, %eax
        movl    %eax, 28(%esp)
    
    
        flds    28(%esp)
        flds    LC1
        fmulp   %st, %st(1)
        fstps   24(%esp)
        flds    28(%esp)
        flds    LC1
        fmulp   %st, %st(1)
        fadds   24(%esp)
        fnstcw  14(%esp)
        movw    14(%esp), %ax
        movb    $12, %ah
        movw    %ax, 12(%esp)
        fldcw   12(%esp)
        fistpl  8(%esp)
        fldcw   14(%esp)
        movl    8(%esp), %eax
    

    We can see only one explicit conversion to single precision instruction (fspts). This conforms my guess, that temporary evaluates from double, not float.

    And with -O2 assembly is pretty simple:

        movl    $602, %eax
    

    And at last

    If we disable optimization of evaluting, IDEONE compiler reproduce behavior too:

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

Sidebar

Related Questions

I am so confused with this code in the book : typedef int (*healthCalcFunc)
just a bit confused by this code var counter = function() { //... var
I am confused on this part of the Django tutorial . It says something
This part of my code checks to see that when the user clicks on
Hi this a part of my code : I have a list which its
Let's say we have this part of code for a UIView subclass: self.myImage =
I am a bit confused when working with objective-c. This part in particular confuses
THis is part of my bash code; b=`cat 101127_2_aa_1.fastq|head -$a|tail -1|sed 's/\(.\)B*$/\1/g'|wc -c` d=`cat
I was confused about this part while I study MIPS. The textbook written by
Totally confused by this one... We have a WAMPServer installation set up, running a

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.