Is there any advantage to using int instead of float without decimal numbers?

Is there any advantage to using int instead of float when the number you want to keep is not a decimal number?

eg.

int number = 10; float number = 10; 

Why do I want to use int instead of float here?

+4
source share
4 answers

Yes, there are many advantages.

  • Arithmetic on int is much faster.
  • int will never suffer from loss of precision.
  • People reading your code will know that the variable is actually an integer.
  • It is not possible to accidentally put a non-integer number in a variable
+11
source

for one, because int math is faster. Better to ask why you want to use type A to represent what you know, type B?

In general, you should use a type that matches what you are trying to represent

+4
source
  • Inta is faster
  • When you declare a huge number of ints, ints can save memory (however, it depends on other factors).
  • Conceptually, for example, the number of people cannot swim to the right ?!
+2
source
  • Int faster computing
  • Int exactly in the range that it works for
  • Floats have a significantly larger range of possible, less accurate values ​​(int from -2147483648 to 2147483647) (float from -3.402823E + 38 to 3.402823E + 38)
+1
source

Source: https://habr.com/ru/post/1441901/


All Articles