float number??

Started by
3 comments, last by nmi 18 years, 7 months ago
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?
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.
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.
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.
Use setprecision:
http://www.cplusplus.com/ref/iostream/iomanip/setprecision.html

Und use double if you care about more digits in your mantissa.

This topic is closed to new replies.

Advertisement