![]() |
Double has 52 bits allotted to store the mantissa and 11 bits for exponent and 1 bit for sign. The leading bit is not explicitly stored. Effectively 53 bits = 16 digits precision.
Now... for the silly bit. [CODE] scanf("%[B][COLOR="Red"]l[/COLOR][/B]f", &input); printf("You entered %f\n", input); printf("cos(x) = 0"/* %0.4f */", and sin(x) = %f\n", /*cos(input),*/ sin(input) );[/CODE] I've highlighted the key part. See anything missing? EDIT:- Useful link - [url]http://en.wikipedia.org/wiki/IEEE_754-2008[/url] |
[QUOTE=axn;289098]Double has 52 bits allotted to store the mantissa and 11 bits for exponent and 1 bit for sign. The leading bit is not explicitly stored. Effectively 53 bits = 16 digits precision.
Now... for the silly bit. [CODE] scanf("%[B][COLOR="Red"]l[/COLOR][/B]f", &input); printf("You entered %f\n", input); printf("cos(x) = 0"/* %0.4f */", and sin(x) = %f\n", /*cos(input),*/ sin(input) );[/CODE] I've highlighted the key part. See anything missing? EDIT:- Useful link - [url]http://en.wikipedia.org/wiki/IEEE_754-2008[/url][/QUOTE] While the link is certainly interesting and informative, it doesn't mention scanf formatting strings. I used %lf because that's what I found all over the place, e.g. [url]http://c-faq.com/stdio/scanf2.html[/url] [url]http://www.cs.utah.edu/~zachary/isp/tutorials/io/io.html[/url] and many many more. And also, input aside, the output is still only printed with like 7 digits, not the 15 you (or Wiki) suggest. |
Therein lies (one of) the problems with the HLL here, hidden precision conversions. Sure, it is convenient, but look at the confusion it has created for Dubslow.
|
In other news, I successfully divined the purpose of commona.c in the P95 source, and even mostly understood it. Conclusion: need to move on to the chapter about structures.
(Language: I almost typed course instead of source, and almost typed 'structure about chapters' instead of the reverse. Isn't the brain a funny thing? :smile:) |
[QUOTE=Dubslow;289100]While the link is certainly interesting and informative, it doesn't mention scanf formatting strings.
I used %lf because that's what I found all over the place, e.g. [url]http://c-faq.com/stdio/scanf2.html[/url] [url]http://www.cs.utah.edu/~zachary/isp/tutorials/io/io.html[/url] and many many more. And also, input aside, the output is still only printed with like 7 digits, not the 15 you (or Wiki) suggest.[/QUOTE] The wiki page was not meant for the problem at hand -- it's just a useful link, is all. Now, to the problem at hand: did I ask "see anything extra?", or "see anything missing?" |
[QUOTE=axn;289106]
Now, to the problem at hand: did I ask "see anything extra?", or "see anything missing?"[/QUOTE] %Lf for long double?... You highlighted the l, which I take to interpret that you mean something is missing between the % and f, but the only thing that goes there is "", "l", or "L" as best as I can tell (lol rhyme :smile:). In printf you might also find numbers in there, but I fail to see how that would change anything (and it's not printf we're dealing with). |
[QUOTE=Dubslow;289109]... (and it's not printf we're dealing with).[/QUOTE]Oh yes it is! And if your HLL compiler would tell you that it has produced a precision conversion you would've spotted the problem immediately.
|
[QUOTE=Dubslow;289109]
You highlighted the l, which I take to interpret that you mean something is missing between the % and f[/quote] It seems my hints have pointed you down the wrong road. [QUOTE=Dubslow;289109]In printf you might also find numbers in there, but I fail to see how that would change anything (and it's not printf we're dealing with).[/QUOTE] Here's the thing. It _is_ a printf issue. You used the correct format string for scanf (%lf for double), but used an incorrect format string for printf (%f = float). So printf sees the %f, takes your double, converts it into a float, and gives you the precision of a float. @retina: The precision conversion happens inside printf, not while passing the parameters. So compilers as such normally won't give any warning. Although some advanced compilers, and code analysis tools (like lint?) might. |
[QUOTE=axn;289118]Here's the thing. It _is_ a printf issue. You used the correct format string for scanf (%lf for double), but used an incorrect format string for printf (%f = float). So printf sees the %f, takes your double, converts it into a float, and gives you the precision of a float.[/QUOTE]
Hmm, I think you are wrong at least from ANSI C point of view. %f will indeed print doubles without internal conversion. The original issue I guess is rather that %f defaults to a precision of 6 digits. A dumb example: [code]#include <stdio.h> int main(void) { double d = 0.1234567890123456; float f = 0.1234567890123456; // doesn't fit in a float printf("%20.20f\n", d); printf("%20.20f\n", f); return 0; }[/code] |
[QUOTE=ldesnogu;289125]Hmm, I think you are wrong at least from ANSI C point of view. %f will indeed print doubles without internal conversion. The original issue I guess is rather that %f defaults to a precision of 6 digits.
[/QUOTE] Interesting... Thanks for that. Learn something new everyday :smile: |
[QUOTE=axn;289136]Interesting... Thanks for that. Learn something new everyday :smile:[/QUOTE]
Among those links and many others, they said that even if you feed %f a float, printf will in fact [i]promote[/i] that to a double. I guess for printing then, I just need to specify more than 6 significant digits. Now, though we are agreed that %lf is a double for scanf, why does 3.141592653589 get truncated to 3.141593, even though the former should fit inside a double? |
| All times are UTC. The time now is 08:00. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.