I am a little confused - what is the purpose of 'double' type if 'float' already defaults to 'double'?
Code:float number_1 23.00
double number_2 23.00
Printable View
I am a little confused - what is the purpose of 'double' type if 'float' already defaults to 'double'?
Code:float number_1 23.00
double number_2 23.00
It isn't the variable that "defaults" to double. It's only the CONSTANTS. I.e., in
x is a float (4 bytes), whereas 1.23 is a double (8 bytes) which will need to be converted to a float before being assigned to x.Code:float x = 1.23;
Hmmm... I am still struggling I thought that since we declaired the variable type with 'float x' then whatever gets assigned to 'x' is a float. How is '1.23' a double, when X was assigned float?
Do you know of any resources that I can use that will explain it too with layman's terms and examples?
I am not a programmer just learning it because I always wanted to learn C.
Thank you
I suggest that for now you just forget that float exists and use double instead. With modern desktop systems you really should generally prefer doubles over floats because of the added precision and less round off errors. The difference in storage size and speed is really not much of a problem, except in certain cases (such as some games or when you have millions of operations). And note that most of the functions prototyped in math.h use doubles for parameters not floats so if you use a float you will do an implicit conversion when you use those functions.
The problem you seem to be having is that you don't seem to understand that by default floating point literals are considered doubles. It doesn't matter what type of floating point variable you're trying to use, be it a float, a double, or a long double.
In the above the 1.23 is the part we are talking about, the variable and the type doesn't matter.Code:float fvalue = 1.23;
double dvalue = 1.23;
long double ldvalue = 1.23;
To force the compiler to use the proper type for the literal you need to use the proper suffix when declaring the variable.
Code:float fvalue = 1.23f; // or 1.23F;
double dvalue = 1.23; // No suffix required, the literal is already a double.
long double ldvalue = 1.23l; // or 1.23L;
Hi Jim,
Ok I will move on, and I hope to get it at a later time. I do understand that float is 4 bytes and double is 8 bytes, and some additional precision on decimal place.
One last question before I move with my studies, since I really lost a lot of time trying to understand this. I am one of those people that will exhaust all options and sources to understand something in which I do not, so I appreciate your help.
could the above be writen as the following to force to a 'float' at 4 bytes?Code:double dvalue = 1.23;
Just to confirm - it does not matter in this case if it 'f' or 'F' correct?Code:double fvalue = 1.23F;
Case only matters in 'printf' function correct?
thank you
You don't have a float to "force" a floating point literal into so??Quote:
could the above be writen as the following to force to a 'float' at 4 bytes?
By the way your second snippet will probably cause problems because your telling the compiler that that literal is a float, with much less precision then that the double you're trying to store that literal.
Remember float and double are distinct types.
The literal "1.23" is considered a double by the compiler so no conversion will be necessary to assign that literal to a double.
Oh.. I do appreciate it.
Do you know of any update to date books or resources that are good for learning?
Currently using a resources that used 'void main()' oppose to 'int main(void)' and I had some problems trying to add the 'return 0' into my code. It would appear that 'void main()' is not a preferred method.
My attempt, may this helps you:Quote:
I am still struggling I thought that since we declaired the variable type with 'float x' then whatever gets assigned to 'x' is a float. How is '1.23' a double, when X was assigned float?
Code:float a = …anything…; // 'a' is always a float, guaranteed.
double b = …anything…; // 'b' is always a double.
…anything… = 1.23; // 1.23 is always a double (hence the "default" for numeric literals).
…anything… = 1.23f; // 1.23f is always a float.
float af = 1.23; // 1.23 (double) will get stored as a float, losing precision.
float bf = 1.23f; // 1.23f (float) will get stored as a float, no conversion necessary.
double ad = 1.23; // 1.23 (double) will get stored as double, no conversion...
double bd = 1.23f; // 1.23f (float) will get stored as double, but lacks initial precision as Jim pointed out.
Unadorned floating point numbers will be implicitly converted to the variable type they are being assigned to. If you use suffixes correctly and turn the warning on, then it will create warnings when you mess with the true value.
As for books, there is a recommendation sticky in the C forum. Anyone with a high post count probably recommended a good book.Code:C:\Users\jk\Desktop>gcc -Wfloat-conversion -c foo.c
foo.c:1:11: warning: conversion to 'float' alters 'long double' constant value [-Wfloat-conversion]
float f = 1.23L;
^
So can I think of it this way?
float f = 1.23 but what is really being said is the following assuming double is 15 place decimal precision and float is 7 decimal precision according to my book
1.23 really seen by the application as 1.230000000000000
However,
float f = 1.23 will convert down to 1.23000000 and the conversion might not be correct?
Well, okay, 1.23 might not be the best example, because the precision would not be harmed in any case (1.23 fits fine in a double or a float). But if for some reason - God help you - you are working with high precision numbers or perhaps numbers on the large end of its range, then representing constants and using the right types will matter a lot. Your numbers will get truncated otherwise, and in that case it could lead to misleading answers from the computer, and such.
Would you hav been just as confused with this somewhat analogous example?
Let's assume ints are 4-bytes and shorts are 2-bytes. Unadorned integer constants default to int (unless beyond int range). So 42 is a 4-byte int constant. Clearly there needs to be some kind of "conversion" to assign it to a 2-byte short since you can't just copy 4 bytes into 2 bytes. In the case of 42 there's no problem, but if we wrote 1000000 instead of 42 we should get a compiler warning that the value can't be converted to fit. BTW, there is no suffix to make the constant itself a short.Code:short n = 42;
Here's a look at the different precision of float and double:
Code:#include <stdio.h>
int main() {
double d = 1.23456789012345678901234567890;
float f = d;
printf("s: 1.23456789012345678901234567890\n");
printf("d: %.60lf\n", d);
printf("f: %.60f\n", f);
return 0;
}
/* Output
s: 1.23456789012345678901234567890
d: 1.234567890123456690432135474111419171094894409179687500000000
f: 1.234567880630493164062500000000000000000000000000000000000000
*/