-1

I just started learning C and I have a very funny problem. I will let the code speak for itself. On scanf I give the value 25.15:

long double input;
scanf("%Lf", &input);
printf("%Lf\n", input); //its correctly 25.150000
input = input - 0.75;
printf("%Lf\n", input); //Instead of something resembling 24.4 I get -2

How is it possible that 25.15 - 0.75 gives me -2?

itsundefined
  • 1,409
  • 2
  • 12
  • 32

1 Answers1

0

I also have same problem as you in CodeBlocks. Works perfectly with double.. Done some googling. It seems its known problem. Also try googling "long double giving wrong results c", i hope you will find your answer there.

marko
  • 77
  • 7
  • Also using codeblocks. How is it possible that an IDE is causing this and not a well known compiler? – itsundefined Oct 17 '17 at 19:16
  • I honestly don't know, looks very strange. If you find any more detailed answer about this behaviour, please leave comment here. :)) – marko Oct 17 '17 at 19:20
  • 2
    @itsUndefined, read the answers to the question Mare linked. They claim that the problem is in the Microsoft C library on which Mingw32 binaries rely. Thus, it is not your IDE, but rather your operating environment. Microsoft has never been overly interested in providing a conforming C (or C library) implementation. – John Bollinger Oct 17 '17 at 19:23
  • @JohnBollinger I figured that out after I read the link. I just thought it's the IDE because it was stated by Mare70. – itsundefined Oct 17 '17 at 19:24
  • I'm using CodeBlocks and I tell you that you wrong. – Michi Oct 17 '17 at 21:08
  • @itsUndefined `I just thought it's the IDE because it was stated by Mare70` You do not need an IDE to test that Code. – Michi Oct 17 '17 at 21:10
  • Yeah man I completely understand now the relation between the two. I could as easily write the code using notepad and then call the compiler from the command line prompt. – itsundefined Oct 17 '17 at 21:17