I am a newbie to programming.
I have a very basic question that
“Can double or float type variable use for integer type values?”
I mean,e.g
float a = 2;
double b = 3.2;
Thank you
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, but there is a loss of precision if you use a float to store an integer.
Typically an integer uses 32 bits to store the number, meaning that it can hold a number in the range pus or minus 2 billion (approximately).
But a float uses 32 bits to store both the part before and after the decimal point, so there is not enough space to store as much precision. A float can store numbers greater than 2 billion, but the bigger the number, the more precision you lose, so if you store a number like 2134567891 in a float it might get changed to something like 2134567000, making it a bad idea to use floats to store precise numbers like an amount of money.
The good news is, a double uses 64 bits to store the number, so there’s more than enough space to store an integer value with the same precision as an int, so as long as you use doubles, you shouldn’t run into too many problems.