[SOLVED] Would I look retarded turning this in?(The answer is: Not this time)
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.
That's cool. I haven't been in a situation where I thought the L would be necessary.
so what you mean is -
1.3 is a float, whereas 1.3L is a double? (long float?)
I'm not sure your guess is correct.
A number like 1.3 is double. To make it float one needs to write it as 1.3f . Small 'l' means long, but it's an integer long. I am not sure what capital L means - there are long doubles in C99. You better check it - this all is documented.
I looked it up and it says: "Floating point constants are assumed to be double. During calculations, other types are promoted (float), then after calculation, if the answer is stored as float, it is demoted (can reduce speed). To override this, add 'f' or 'F' suffix, ex. 2.3F, or if you want long double use 'L', ex. 54.3L. Otherwise it is of type double."
Yes, as you say, double is the default - here are the relevant quotes from sections 2.13.3 and 3.9.1 of the ISO C++ standard:
Quote:
The type of a floating literal is double unless explicitly specified by a suffix. The suffixes f and F specify float, the suffixes l and L specify long double.
Quote:
The type double provides at least as much precision as float, and the type long double provides at least as much precision as double.
If you work with (single precision) float, the compiler may or may not convert the intermediate results to double (it is neither required nor disallowed by the standard). In fact, you can get different results depending on whether optimization is turned on or not.
The GNU compiler implements the long double using the 80 bit floating point facility on the 32 bit x86 machines.
I feel certain that after having read the comments in this thread, that I will always, from here on, be able to distinguish accurately between ints, floats, and doubles.
As a side note - Yesterday's class went pretty well. We technically discussed programming, if you consider discussing the failures of certain governmental programs to be forms of programming. Terrorism was mentioned, too. Looked at us as if he was thinking, "You never can tell... Any one of em could be, but statistically, all of them are out to get me!"
Terrorism was mentioned, too. Looked at us as if he was thinking, "You never can tell... Any one of em could be, but statistically, all of them are out to get me!"
They have to drive it into you like some demon. They do this with other things too, in case you haven't noticed. It they say something enough you will believe it. These things that they push so hard, these things must be questioned the most, in fact they should be assumed to be false at first and later proven to be true or otherwise.
A number like 1.3 is double. To make it float one needs to write it as 1.3f.
Furthermore, the return type of atof() is double. And float gets promoted to double within varags. So double is really the "normal" floating-point type in C. Whoever named them screwed up: the 64-bit type should have been called "single precision" (so the 32-bit would be "half precision").
I disagree with an earlier poster that float is "totally useless" though. I used it last year to build millions-of-elements correlation matrices for the Netflix Prize. Had to save memory.
I feel certain that after having read the comments in this thread, that I will always, from here on, be able to distinguish accurately between ints, floats, and doubles.
...
Just remember not to trust us and to recheck what we are saying in the official documentation - I am serious.
Indeed you would, you should also take into account sig figs and rounding errors. But heck for programming class you probably don't care and neither does the teacher.
But, it all conversions are written in terms of a common base unit, there are only two steps: Multiply to convert from the source unit to the base unit, and divide to convert from the base unit to the destination unit.
Sure, this may mean getting 1 mile = 63360.00000000001 inches instead of 63360. But it has the huge advantage that you only have to write O(n) conversion factors instead of O(nē).
I find truly, the computer is only accurate to about 2 decimal places after the point.
"float" (usually) has 24 significant bits. That's only 7.22 significant digits.
Suppose the value you're converting is between 65536 and 131072 meters (about 41-81 miles). Then you need 17 bits to store the integer portion of the meters, so that leaves you only 7 bits for the fractional part, giving you a resolution of 1/128 m.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.