ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#include <stdio.h>
int main() {
float a = .5, b = .7;
if (b < .7)
if (a < .5)
printf("TELO");
else
printf("LTTE");
else
printf("JKLF");
return 0;
}
Here is the output:
Quote:
LTTE
The output that I expect is JKLF as the condition b < .7 is false; therefore, the control should not enter the inner if and should reach its own or else printing JKLF.
QUESTION#1: How does the control reach LTTE?
However, if the float data type is changed to int, constants .5 and .7 to 5 and 7 respectively, then the output yields what I expect, i.e., JKLF
QUESTION#2: How does the control reach JKLF after the aforementioned change?
Probably something to do with the fact that their floating point.
I'd look at it in the debugger where (a) you can see the exact values of those variables, need to use -O0, and (b) you can see the disassembly of the compare lines.
I wonder if the constants are treated as ints and rounded up.
I'd try the same experiment with variables in place of the constants.
Whether or not you've run into this before, this is a detail to remember, that floats and doubles are 'special'. Not that there aren't any conventions, there absolutely are, but one has to learn them. Myself, the few times I work with them, I learn what I need. Certain types of scientific programming sometimes require a lot of this.
#include <stdio.h>
int main() {
float a = .7;
double c = .7;
if (a < c) {
printf("What the...?\n");
}
return 0;
}
Run it, watch the result, and then recompile with both values changed to .5.
What's going on here, is that some decimal values do not map directly to binary, so different approximations are used when such values are stored as double or float respectively. Conversion between types will amplify these inaccuracies.
Constants in if statements are not treated as the type they are being compared to. Instead, they default to double. As a result, the statement if (a < .7) in your original program ends up becoming "if the result of typecasting a to double is less than the double approximation of the decimal number .7", which is probably not what you wanted. Or what anyone would want, really.
#include <stdio.h>
int main() {
float a = .7;
double c = .7;
if (a < c) {
printf("What the...?\n");
}
return 0;
}
Run it, watch the result, and then recompile with both values changed to .5.
What's going on here, is that some decimal values do not map directly to binary, so different approximations are used when such values are stored as double or float respectively. Conversion between types will amplify these inaccuracies.
Constants in if statements are not treated as the type they are being compared to. Instead, they default to double. As a result, the statement if (a < .7) in your original program ends up becoming "if the result of typecasting a to double is less than the double approximation of the decimal number .7", which is probably not what you wanted. Or what anyone would want, really.
The output of your code is:
Quote:
What the...?
Which clearly means, float a = .7 is considered smaller than double c = .7.
But the output of the code after changing .7 to .5 simply returns. This clearly means that float a = .5 is considered greater than double c = .5
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.