0

I am using mingw-gcc and I want to print a float.

#include <cstdio>
#include <iostream>

int main(){
  float a =1.23;
  std::cout << std::scientific << a << std::endl;
  printf("%e\n",a);
  return 0;
}

the output is

1.230000e+000
1.230000e+000

However float does not need more than two digits. Is there any way in gcc to export number with 2 digit exponent?

1.230000e+00
1.230000e+00

Is there any similar function like _set_output_format of Visual studio?

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
ztik
  • 3,482
  • 16
  • 34
  • http://stackoverflow.com/questions/31331723/how-to-control-the-number-of-exponent-digits-after-e-in-c-printf-e – ewcz Sep 16 '16 at 14:11
  • @ewcz I want exactly the opposite. Two digit with mingw – ztik Sep 16 '16 at 14:14
  • this ? std::fixed and std::setprecision http://stackoverflow.com/questions/14677448/how-to-cout-a-float-number-with-n-decimal-places – Binarynam Sep 16 '16 at 14:22
  • And that is apparently C++, not C. Mixing C++ iostreams with C functions on stdin is a bad idea. – too honest for this site Sep 16 '16 at 14:33
  • @Olaf I just wanted to show that `cout` and `printf` have the same behavior. It is obvious that this is not my original source. – ztik Sep 16 '16 at 14:40
  • @Olaf I mean `FLT_MAX_EXP`. You can refer to http://www.cplusplus.com/reference/cfloat/ for more information. – ztik Sep 16 '16 at 14:53
  • The standard library for none of the languages C or C++ are not part of the gcc compiler. So that is not relevant. And the behaviour for the C standard function `printf` is clearly defined in the standard. Similar for the C++ standard library (resp. which types you can pass to the `<<` method of an iostream to set the conversion behaviour). – too honest for this site Sep 16 '16 at 15:18
  • Btw: for `printf`, you pass a `double`. It is not possible to pass a `float` to a variadic function. Read the man-page! – too honest for this site Sep 16 '16 at 15:24
  • @Olaf the `gcc` compiler is a C and also a C++ compiler. If you read the title of the question it clearly refers to gcc. The question is not about the standard definition of `printf` or `cout`. It is clearly a question about the gcc C and C++ compiler and their non-standard capabilities. – ztik Sep 16 '16 at 15:26
  • @Olaf when `printf` prints a float the exponent will always start with a zero. So the question simply asks, if there is a way to avoid the extra character. – ztik Sep 16 '16 at 15:28
  • 1) The code is C++ **only** 2) The function you complain about are **not part of the compiler** - neither for C nor C++. They are not provided by gcc, but your platform. 3) The formating options are defined by the standards. RTFineManual! gcc just provides a freestanding implementation. The rest is a matter of your platform; the compiler is **no way** involved in that! As much as `_set_output_format` is an extension of the Windows standard C library, not of Visual Studio! Did you try using that function? – too honest for this site Sep 16 '16 at 15:28
  • No, as I wrote: `printf` prints a `double`. **You cannot pass a `float` to it. Read about standard conversions. (not sure about C++, but I suspect it does the same). – too honest for this site Sep 16 '16 at 15:33

1 Answers1

2

at least as far as printf is concerned, it seems that one can set the environment variable PRINTF_EXPONENT_DIGITS to 2 and compile with -posix switch (tested with mingw g++ 5.3.0 on clean install of Windows 10)

ewcz
  • 12,819
  • 1
  • 25
  • 47