Page 1 of 1

double vs float

Posted: Thu Apr 27, 2023 4:55 pm
by hin1129
int rate = 10; int t = 5;
XXX amount = 1000.0;
for(int i=0; i<t; t++) {
amount = amount*(1 - rate/100);
}
What can XXX be?

i am struggling to understand the difference between double and float
i know that they got different sizes:
float is Up to 7 digits after decimal and default value is 0.0f
double is Up to 15 digits after decimal and default value is 0.0d
but how do you tell whether the value is double or float? (apart from the letters d and f)
and for this question why it can only be double?

Re: double vs float

Posted: Fri Apr 28, 2023 3:28 am
by admin
> how do you tell whether the value is double or float? (apart from the letters d and f)
1. When you write any number in your code with a decimal point, it is a double except when you suffix it with f or F. There is no other way.
2. When any number is computed by doing arithmetic operations on any other number which is a double or a float, then the result is a double. e.g. result of multiplying two floats is also a double (not a float).

So, you can see why XXX can only be a double.

>i know that they got different sizes:
Yes, but the size is that of the size of the datatype i.e. 16 bits or 32 bits of 64 bits. It is not the size of the decimal part. The size of float (32 bits) or double (64 bits) may or may not necessarily correspond to the number of digits after decimal.