Pregunta

I'm getting confused between double and float in C++. For example:

Q. For each type state its constant:

a.) 1.0
b.) 2.8e-10

According to me, the a.) part is a float (as it's less precise) and b.) is a double. Or are both double?

¿Fue útil?

Solución 2

Without any suffixes all floating-point literals are double in C++. If an f suffix is attached then the literal is a float and if written with L suffix then it'll be a long double. Literal constants generally don't depend on their magnitude. Integer literals like 1 or 2 are of type int although their values lies completely in char's range

The type of a floating literal is double unless explicitly specified by a suffix. The suffixes f and F specify float, the suffixes l and L specify long double

ISO C++ 2013 draft

Otros consejos

I think precision is the main difference between the two:

Float - 7 digits (32 bit)

Double-15-16 digits (64 bit)

Your answer may depend on the language which you are using since precision factor is a critical one. But I would say that you can go with that both are DOUBLE. Also 1.0 can be float as well, so without knowing your requirement or language it is difficult to answer that.

you might consider them both as double , at the end it is all about size 1.0 is small in size so you can consider it as float too.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top