Jump to content
  • Advertisement
Sign in to follow this  
jordi_0071

float number??

This topic is 4816 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey i made this code:
#include <iostream>
using namespace std;

int main()
{
    int a;
    float b, c;
    a = 231;
    b = 231.0012000;
    c = 103.0012120;
    
    cout << "Variable a is " << a << ".\nVariable b is " << b << ".\nVariable c is " << c << endl;
    cout << "Variable a + b - c = " << (a + b) - c << "." << endl;
    
    system("pause");
    
    return 0;
}

but it only shows: ------------------------------ Variable a is 231. Variable b is 231.001. Variable c is 103.001. Variable a + b - c = 359. ------------------------------ what am i doing wrong? and do i have to use static_cast or something like that?

Share this post


Link to post
Share on other sites
Advertisement
Well, to begin with, you could say what it is you want/expect it to do.

Best anyone can tell you right now is that yep, the code you showed does indeed print the output you showed.

Share this post


Link to post
Share on other sites
If you're complaining about the result of the last output, you need to keep in mind that IEEE single precision floating point numbers (which float is on most platforms) have only about 6 digits of precision, and you were asking for a result that needed more like 9 digits of precision. In order to get accurate results you would probably need to use doubles instead of floats.

Share this post


Link to post
Share on other sites
i want the program to print b and c als whole numbers, and count them as whole numbers so like this:

Variable a is 231.
Variable b is 231.0012000.
Variable c is 103.0012120.

Variable a + b - c = 565.0024120.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!