Data type and its size

Aug 28, 2012 at 4:14am
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
Aug 28, 2012 at 4:59am
its just double the size of floats
Aug 28, 2012 at 5:12am
Thats all?
Aug 28, 2012 at 5:26am
yup.

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

I try to stick with either just floats or just doubles when writing a program.
Aug 28, 2012 at 5:39am
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.
Aug 28, 2012 at 5:44am
@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 Aug 28, 2012 at 5:45am
Aug 28, 2012 at 6:23am
@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; 
Aug 28, 2012 at 7:57am
Thanks for the explanations :)
Topic archived. No new replies allowed.