1

The issue I am having is that when I define a variable n as long double and set the value to 5 when i run it to print the value of n it prints -0.0000. I'm trying to achieve a larger range of numbers so i decided to switch from double to long double. OS: windows 10 IDE: Code blocks compiler: mingw32-gcc.exe

long double n;
n=5;
printf("n= %Lf\n",n);
Mr Alam
  • 19
  • 2
  • Try `n = 5.0;` -- `5` is an integer constant. – torstenvl Nov 26 '18 at 21:33
  • please try to use %lf, not %Lf. – Julian Nov 26 '18 at 21:34
  • Please provide a [Minimal Complete Verifiable Example](https://stackoverflow.com/help/mcve). – Eric Postpischil Nov 26 '18 at 21:34
  • 3
    @torstenvl That doesn't matter as the assignment will do the proper conversion from 5 – Craig Estey Nov 26 '18 at 21:35
  • 3
    @Jin: `l` in `%lf` has no effect (compared to `%f`); it would expect the argument to be `double`. It is inappropriate for `long double`. For `long double`, %Lf` is correct. – Eric Postpischil Nov 26 '18 at 21:36
  • 2
    @Jin No, `%Lf` is the correct format for `long double` – Craig Estey Nov 26 '18 at 21:36
  • 3
    Your code, as is, produces the expected result (e.g. `n= 5.000000`) on my system, so I am unable to reproduce the problem here (linux, fedora, gcc, glibc). What combo are you using? – Craig Estey Nov 26 '18 at 21:38
  • Perhaps you're using a limited (smaller) version of `printf`. Do you have an option to use full version? What compiler, IDE, etc? – Fiddling Bits Nov 26 '18 at 21:38
  • @CraigEstey I am coding this in code blocks. – Mr Alam Nov 26 '18 at 21:41
  • @torstenvl I tried that, didn't seem to work. – Mr Alam Nov 26 '18 at 21:43
  • 4
    @MrAlam : Code::Blocks is not a compiler. Although you were asked about an IDE, that information is entirely irrelevant. Compiler and target execution environment are important. – Clifford Nov 26 '18 at 21:44
  • @FiddlingBits : You need to be more specific than "_etc_" perhaps when asking for information? Not sure why IDE would be relevant. – Clifford Nov 26 '18 at 21:46
  • I just created a code::blocks project and it works here, too. What OS are you using (e.g. linux, WinX, MacOS) and what `libc` are you using (e.g. `glibc` under linux, or what under the other OSes)? – Craig Estey Nov 26 '18 at 21:47
  • @Clifford Just trying to understand how compiler options are selected. – Fiddling Bits Nov 26 '18 at 21:47
  • off topic to your question, but if you're "trying to achieve a larger range of numbers" and you have a factorial example in your screenshot, you probably want a multi precision library like GNU MP/gmplib – BurnsBA Nov 26 '18 at 21:48
  • I am running this on Windows 10 and code blocks is using gnu gcc compiler to compile the code @CraigEstey – Mr Alam Nov 26 '18 at 21:51
  • 2
    There is no need to post _pictures of text_. – Clifford Nov 26 '18 at 21:52
  • 1
    You should be more specific - GCC version is required, and in particular you are likely to be using MinGW which uses Microsoft's C library rather then glibc. Moreover the information should be in the question (edit it), not hidden in comments, – Clifford Nov 26 '18 at 21:57
  • GCC uses the FPU's internal 80 bit representation for `long double`, while the Microsoft C library `printf()` used by MinGW uses 64bit because Microsoft'c compiler use a 64bit `long double` (i.e. same as `double`). – Clifford Nov 26 '18 at 22:02
  • Your libc doesn't support `long double`. Consider either `cygwin` or: https://www.microsoft.com/en-us/p/ubuntu/9nblggh4msv6?activetab=pivot:overviewtab to download ubuntu under windows and use that – Craig Estey Nov 26 '18 at 22:03
  • @CraigEstey: The problem is not that `long double` isn't supported. The problem is that the compiler (gcc) and the runtime library use different and incompatible representations for `long double`. Either representation would be perfectly legal as far as C is concerned, as long as the implementation uses it consistently. (There are ways to configure the implementation so it uses its own `printf` implementation that works correctly with gcc.) – Keith Thompson Nov 27 '18 at 01:10

0 Answers0