0
long double i, j, ans;
scanf("%Lf %Lf", &i, &j);
ans=i*j;
printf("%Lf %Lf", ans, i*j);

i/p -> 1 1
o/p -> -2.000000 -2.000000
expected o/p -> 1.000000 1.000000

Please explain this behaviour, and where is it going wrong and how can we come over it? If the o/p appx to close to 1 also acceptable.

FYI: long double format specifier I tried Lf, lf, LF and llf according to other StackOverflow post.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Yogeesh Seralathan
  • 1,396
  • 4
  • 15
  • 22
  • %Lf is fine here. It'll surely solve your case. – Am_I_Helpful Jun 09 '14 at 05:13
  • 1
    I see no problems, could you provide the compilation options you are using? – IllusiveBrian Jun 09 '14 at 05:13
  • this will help you http://stackoverflow.com/questions/4089174/printf-and-long-double – Jayesh Bhoi Jun 09 '14 at 05:16
  • Try initialsing your `ans` varible with 0. – Am_I_Helpful Jun 09 '14 at 05:18
  • @Jayesh-It won't because both questions have no relationship except for the point which I have already mentioned above, %Lf is fine here. – Am_I_Helpful Jun 09 '14 at 05:21
  • 2
    @shekharsuman: Initializing `ans` to `0` will make no difference. – Keith Thompson Jun 09 '14 at 05:21
  • @KeithThompson- Sometimes initialisation matters,just like in Java it gives compilation error if the resulting variable is not initialised. – Am_I_Helpful Jun 09 '14 at 05:22
  • 1
    [works here](http://coliru.stacked-crooked.com/a/d4b043343b7e292e), try print return value of `scanf` – Bryan Chen Jun 09 '14 at 05:23
  • 2
    @shekharsuman: No, in this case initializing `ans` to `0` *will make no difference*. A value is assigned to it on the third line of the posted code snippet, overwriting whatever value was used to initialize it. – Keith Thompson Jun 09 '14 at 05:23
  • 1
    @shekharsuman: ans gets assigned to before it gets used. It is fine. The initialization to 0 would be redundant. – dave Jun 09 '14 at 05:23
  • 1
    Are you using MinGW? As I recall, it has problems with `long double` because the representation used by the compiler (gcc) doesn't match the representation used by Microsoft's runtime library. – Keith Thompson Jun 09 '14 at 05:26
  • Oh yeah, I told `sometimes` in my point-It is true as said by you guys. – Am_I_Helpful Jun 09 '14 at 05:26
  • `%Lf` is the correct format, both for `printf` and for `scanf`. I don't know where the close votes are coming from; the question is clearly about programming, and it's stated reasonably clearly, with input, output, and expected output. My only suggested improvements are (a) update the question to show a complete self-contained program ([read this](http://sscce.org/) and (b) tell us what platform you're using. This *might* be a duplicate of [this question](http://stackoverflow.com/q/1764350/827263). – Keith Thompson Jun 09 '14 at 05:40
  • I'm running it on CodeBlocks with MinGW32. GCC version 4.7.1. – Yogeesh Seralathan Jun 09 '14 at 05:42
  • @keithThompson Yes. Using MinGW. Have any suggestions ? – Yogeesh Seralathan Jun 09 '14 at 05:43
  • @BryanChen It returns 2. Its working fine. @ shekhar initialising with ans with 0 didn't make any difference. – Yogeesh Seralathan Jun 09 '14 at 05:44
  • 3
    This is a bug in MinGW, caused by its incorrect integration of gcc and Microsoft's runtime library (neither of which is at fault; it's the combination that doesn't work). Computations with `long double` should work correctly, but `printf` and `scanf` do not. Do you really need the full precision of `long double` or would `double` suffice? – Keith Thompson Jun 09 '14 at 05:46
  • Oh! And Double would be enough and dealing with number values upto 10^18. – Yogeesh Seralathan Jun 09 '14 at 05:56
  • It's not quite that simple. Both `double` and `long double` can represent numbers much bigger than 10^18. For their representable ranges, `long double` usually has better precision. `double` is typically good enough for most purposes; I can't guessing whether it's good enough for yours. – Keith Thompson Jun 09 '14 at 07:23
  • Bet the result of `scanf("%Lf %Lf", &i, &j);` was not 2. – chux - Reinstate Monica Jun 09 '14 at 12:48
  • @chux: That's an excellent point; the code needs to check the value returned by `scanf`. But I'd bet that the result *is* `2`. If the problem is what I think it is, `scanf` will successfully convert 2 `long double` values -- but it will store them to `i` and `j` with the format used by Microsoft's runtime library, which differs from the format used by the compiler gcc. – Keith Thompson Jun 09 '14 at 20:03
  • @0g1337h What is the return value of `scanf("%Lf %Lf", &i, &j)`? – chux - Reinstate Monica Jun 09 '14 at 22:28
  • @Keith Thompson OP confirms your thought. – chux - Reinstate Monica Jun 12 '14 at 14:40

0 Answers0