Data type and its size

Hi,

Can someone explain to me this data type and size.

double denominator, numerator, result;

Is it about having these numbers in decimals and not whole numbers?
Thanks =D
its just double the size of floats
Thats all?
yup.

http://www.cplusplus.com/doc/tutorial/variables/

I try to stick with either just floats or just doubles when writing a program.
I believe you're asking about integer division.

1
2
3
4
5
int a = 5, b = 2;
cout << a/b << endl;

double c = 5, d = 2;
cout << c/d << endl;
2
2.5


If you divide doubles or floats, you're going to get the decimal. If you divide floats, you're going to get a whole number. That's it.
@Anthony
That works if you are just doing calculations, but what about conditional statements?

if (a == 1.0)
is never a good idea because floats rarely "equal" a desired number. They can be infinitely close, but an equal sign just won't work 99 percent of the time. You'd need to do something like: if (a > 0.99 && a < 1.01) but that gets annoying and verbose. Something like this will almost guarentee an infinite loop:
for (float i = 0.f; i != 100.f; i += 0.1f) { /*do something*/ }

ints are great for switch statements, conditional statements, counting, or math where you need to know precision and cant afford to loose any digits (such as banking software).
Last edited on
@Stewbond

That's true. It's hard to account for precision loss when dealing with floating points.

I was working on some homework and the book I'm reading had this to account for the precision loss:

1
2
3
4
5
6
#define EPSILON 0.000001

// Other code...

//add in a small amount to cover any precision errors we may have made
m_dBestPossibleRoute += EPSILON; 
Thanks for the explanations :)
Topic archived. No new replies allowed.