14

In the snippet below, auto deduces the variable to double, but I want float.

auto one = 3.5;

Does it always use double for literals with a decimal point? How does it decide between float and double?

curiousguy
  • 8,038
  • 2
  • 40
  • 58
Abhinav Kinagi
  • 3,653
  • 2
  • 27
  • 43
  • 2
    To answer your **exact** question (instead of what you wanted to know) -- `auto` deduces it that way because templates deduce it that way. Templates deduce it that way because function overloading does (function overloading rules are used to break ties). – Ben Voigt Aug 16 '19 at 05:36
  • You mean to say that it's an overload resolution of `type` here? – Abhinav Kinagi Aug 16 '19 at 05:40
  • 3
    I mean to say that the rule that makes `auto one = 3.5;` deduce `double` is the same rule that make `int f(float); int f(double);` choose `f(double)` when presented with `f(3.5)`. Your question has nothing peculiar to `auto`, it only involves the type and category of the floating-point literal `3.5` – Ben Voigt Aug 16 '19 at 05:43
  • 8
    Because `3.5` is a `double`, not a `float`. – user207421 Aug 16 '19 at 06:11
  • As a note, arithmetic types default to `int` for integer literals, `double` for floating-point literals, `char` for character literals, and `bool` for boolean literals. – Justin Time - Reinstate Monica Aug 16 '19 at 19:59
  • 1
    @JustinTime: Although in C, character literals are type `int`, which can cause subtle errors when code that looks like C is compiled with both C and C++ compilers, but depends on character literals being specifically `char` or `int` (and even more fun, potentially depends on the implementation defined signedness of `char`). – ShadowRanger Aug 17 '19 at 00:59
  • 1
    there are so many dupllicates: [All floats are doubles?](https://stackoverflow.com/q/17292371/995714), [How a floating point literal is treated either double or float in Visual C++?](https://stackoverflow.com/q/8757815/995714), [why sizeof(13.33) is 8 bytes?](https://stackoverflow.com/q/5537276/995714), [What is the type of the value 1.0e+1](https://stackoverflow.com/q/30260830/995714), [How function overloading works with double and float](https://stackoverflow.com/q/54090448/995714)... – phuclv Aug 17 '19 at 01:17

4 Answers4

30

Type of literal 3.5 is double. For float please use 3.5f

You can play with this snippet to see various type information.

Gyapti Jain
  • 4,056
  • 20
  • 40
7

3.5 is a double literal. Thus auto correctly deduces its type as double. You can still use it to initialize a float variable, but the most correct way is to use a float literal like 3.5f. The f at the end is called a suffix. Suffixes for floating point literals are:

  • (no suffix) defines double
  • f F defines float
  • l L defines long double

Besides floating point literals, there are also suffixes for integral literals and user-defined literals.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
7

In C++ (and C), floating literals are treated as double by default unless specified by f or F or l or L.

The standard has following:

2.14.4 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. If the scaled value is not in the range of representable values for its type, the program is ill-formed.

Hence,

auto one = 3.5;

is always double and if you intend float it should be coded as

auto one = 3.5f;
user207421
  • 305,947
  • 44
  • 307
  • 483
dlmeetei
  • 9,905
  • 3
  • 31
  • 38
1

The type of a floating point literal in C++ is automatically double unless:

  1. f is suffixed, in which case the type of the literal is float

  2. L is suffixed, in which case the type of the literal is long double

So, if you want your variable to be a float, do this:

auto one = 3.5f;
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76