G
G
German2019-03-21 09:23:44
C++ / C#
German, 2019-03-21 09:23:44

Why is float initialized as double by default?

I read that when declaring a variable of type float, the value is stored in it as double, and in order to set the storage type as float, we use the letter f.
Why is it so? After all, float is a type smaller than double. That is, double occupies 8 bytes (minimum), and float 4 bytes (minimum), that is, we store a number in a larger representation type, in a smaller variable.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel, 2019-03-21
@HEKOT

Everything is stored as expected, but by default a numeric constant, for example, 1.5, is of type double. To shove it into a float, you must explicitly specify the type: 1.5f, otherwise it will not fit.
https://docs.microsoft.com/en-us/cpp/c-language/c-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question