Vijay Kumbhani
Vijay Kumbhani

Reputation: 742

Format changes in Visual Studio 2015

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

Upvotes: 2

Views: 1261

Answers (2)

user3246883
user3246883

Reputation: 71

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 :-(

Upvotes: 1

SingerOfTheFall
SingerOfTheFall

Reputation: 29966

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.

Upvotes: 2

Related Questions