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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:33:55+00:00 2026-05-26T14:33:55+00:00

So I have the following code: #include <iostream> int main(){ float number = 0.6;

  • 0

So I have the following code:

#include <iostream>
int main(){
    float number = 0.6;
    int anotherNumber = 20;
    int value = (int) ((float) anotherNumber * number);
    std::cout << value;
    std::cin.get();
}

It gives 12, like it should considering 20 * 0.6 = 12. However, if I change all floats to doubles:

#include <iostream>
int main(){
    double number = 0.6;
    int anotherNumber = 20;
    int value = (int) ((double) anotherNumber * number);
    std::cout << value;
    std::cin.get();
}

It gives me 11 instead. And to make things even weirder, if I change the code so that the value is stored in a variable first and then case after, it gives the right answer (12) again.

#include <iostream>
int main(){
    double number = 0.6;
    int anotherNumber = 20;
    double intermediate = (double) anotherNumber * number;
    int value = (int) intermediate;
    std::cout << value;
    std::cin.get();
}

What in God’s name is going on here? I’m using g++ 4.5.3 to compile.

  • 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-26T14:33:55+00:00Added an answer on May 26, 2026 at 2:33 pm

    0.6 cannot be represented in any binary floating point format exactly. Sometimes it somewhat larger and somewhat it’s somewhat smaller, depending on the data-type. See What Every Programmer Should Know About Floating-Point Arithmetic for detailed explanation.

    The ‘store in memory’ version is different because the x87 FPU uses 80-bit floating point registers internally.

    EDIT: Detailed computation:

    float 0.6 in memory;
    .100110011001100110011010
    loaded to register:
    .10011001100110011001101000000000000000000000000000000000000000000
    multiplied by 20:
    1100.0000000000000000000010000000000000000000000000000000000000000
    rounded down:
    1100
    
    double 0.6 in memory
    .10011001100110011001100110011001100110011001100110011
    loaded to register:
    .10011001100110011001100110011001100110011001100110011000000000000
    multiplied by 20:
    1011.1111111111111111111111111111111111111111111111111110000000000
    rounded down:
    1011
    
    double 0.6 in memory
    .10011001100110011001100110011001100110011001100110011
    loaded to register:
    .10011001100110011001100110011001100110011001100110011000000000000
    multiplied by 20:
    1011.1111111111111111111111111111111111111111111111111110000000000
    converted to double-precision and stored to memory:
    1100.0000000000000000000000000000000000000000000000000
    loaded to register:
    1100.0000000000000000000000000000000000000000000000000000000000000
    rounded down:
    1100
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have following code #include <iostream> using namespace std; int main(int argc,char arg[]){ int
i have following code #include <iostream> using namespace std; int main(){ int i; char
i have following code #include <iostream> #include <cstdlib> using namespace std; int main() {
i have the following code: #include <stdio.h> int main(void) { float a[4] __attribute__((aligned(0x1000))) =
I have following code #include <iostream> #include<exception> #include <cstdlib> int main(){ for (int i
I have following code #include <iostream> #include <string> #include <algorithm> using namespace std; int
I have the following code: #include <iostream> using namespace std; struct stud{ int roll;
I have the following code: #include <iostream> int main() { int n = 100;
I have the following code: #include <iostream> #include <vector> #include <algorithm> int main( int
I have the following code: #include <iostream> #include <string> #include sqlite3.h int main() {

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.