int n float

when do we use both int n float at the same time and why
Can you explain further? please!
Use int for integer numbers ex: 1 , 9, 100 , -1 , -5
Use float for rational numbers with up to about 7: 0.000000009f , 1.234f , 1.30003f 1222.231f //excluding the zeros
Use double for twice the precision of floats 1.22222222222 , 1.999999 12222.2321
You have many choices when it comes to the fundamental types. So how do you know which type to use? well, if you need an integer value, you're best off using int, that's because int is generally implemented so that it occupies an amount of memory that is most efficiently handled by the computer and if you need a floating pointer number (ex: 1.5251) you're best off using float, which again is likely to be implemented so that it occupies an amount of memory that is most efficiently handled by the computer.
Unless you absolutely have to, never use the actual float type. Use double instead.

EDIT: Changed "long double" to "double" as requested by JLBorges.
Last edited on
Prefer using double as the default floating point type.
Unless there is a specific requirement, avoid the use of float or long double.
Last edited on
If you need to use a long double you are using some really large numbers and would probably be better off just making a custom class IMO to make it more precise ( for integers anyways ).
@giblit: unless you need so much precision that you don't care for the efficiency, making your own class instead of using long double directly seems like a really bad idea.
how would it be a bad idea to make a custom integer class? It would be a lot more precise than doing floating point calculations

1 + 1 = 2
.99999999999999 + .9999999999999 != 2
Last edited on
Convince me that there's a minimal performance decrease and I'll believe you ;p
I'm not sure about performance but it would be slightly more accurate if you really cared about like 100000000000000000000000000000000000000000 + 1000000000000000000000000000000000000000000
I'm willing to bet the performance penalty makes it completely not worth it.
> If you need to use a long double you are using some really large numbers

The range of long double is not all that much larger than the range of double on most architectures.

For instance, on GCC x86 and x86_64, the value representation of a long double is 10 bytes (80-bit extended precision format). sizeof( long double ), which is the amount of memory occupied by the object representation of long double is a larger value (12 or 16 bytes) to meet alignment requirements. SSE/SSE2/SSE3 instructions, which are limited to operations on (multiples of) 64-bits, are way faster than operations on the archaic FPU x87 which supports 80-bit floating point operations.

GCC 4.3 and above on x86_64 has a (nonstandard) __float128 type which has true quadruple precision.
thank u all ...
Topic archived. No new replies allowed.