2

The sample program. simply print converted value.

#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
    char buffer[256] = "";
    sprintf_s(buffer, "%.2e", -20.12345);
    cout << buffer << endl;;
    return 0;
}

Run same program in Visual Studio 2010 and Visual Studio 2015.

They are showing different output.

Visual Studio 2010 output:

-2.01e+001

Visual Studio 2015 output:

-2.01e+01

why is it showing different output? anybody can explain.

Thanks

pedrorijo91
  • 7,635
  • 9
  • 44
  • 82
Vijay Kumbhani
  • 734
  • 1
  • 6
  • 26

2 Answers2

2

The scientific notation in the form of <m>E<n> means m*10n, where both m and n can be positive or negative. This means that -2.01e+001 and -2.01e+01 are in fact the same number (-2.01*101). However, when using e format specifier, you can actually output numbers with a really big or really small e values, for example you can output 2e150. The 3-digit exponent is used to pad the output strings and make them more uniform (consider 2e99, 2e101 vs 2e099, 2e101).

It is also possible to use _set_output_format function to change the amount of digits that will be shown. It's worth noting that on that documentation page it is also stated that

By default, the output of floating point numbers by functions such as printf, wprintf, and related functions in the Visual C++ Standard C library prints three digits for the exponent, even if three digits are not required to represent the value of the exponent. Zeroes are used to pad the value to three digits.

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
  • can I remove extra padded zero? if yes then how? I have converted my project from VS 2010 to VS 2015. VS 2015 give me compile error like that " Failure in Format: Expected -2.01e+01 but was -2.01e+001 " – Vijay Kumbhani Jan 25 '17 at 09:08
  • @VijayKumbhani, please read the answer thoroughly, I've already mentioned that: `It is also possible to use _set_output_format function to change the amount of digits that will be shown.` – SingerOfTheFall Jan 25 '17 at 09:12
  • 1
    _set_output_format actually does not exist anymore, starting from VS2015 – bartoli Dec 28 '17 at 13:40
1

As noted by Chux in How to control the number of exponent digits after 'e' in C printf %e?, the compliant behavior is to use two digits for the exponent unless more are needed. Prior to VS-2015, VS was non-compliant. Apparently _set_output_format was provided to allow compliant behavior.

Since _set_output_format was removed in VS-2015 - and the behavior was changed to two digit exponents - one must assume that VS is attempting to be more compliant.

I found this because I need the compliant behavior in my application :-(

user3246883
  • 71
  • 1
  • 4
  • I need the non compliant behavior to be compatible with an older non compliant format. Now that MS decided to be compliant and removed _set_output_format it has become impossible (without writing your own fprint variants or going back to older Visual Studio versions). Microsoft at it's best! – thewhiteambit Feb 25 '20 at 10:36