Rounding of strings

Consider the following program:

int main()
{
auto f = 1.0f;
printf(“%f\n”, f / 0);
printf(“%.1f\n”, f / 0);
printf(“%.2f\n”, f / 0);
printf(“%.3f\n”, f / 0);
printf(“\n”);

f = 0.0f;
printf(“%f\n”, f / 0);
printf(“%.1f\n”, f / 0);
printf(“%.2f\n”, f / 0);
printf(“%.3f\n”, f / 0);
}

With Visual Studio 2012, it outputs the following:

1.#INF00
1.$
1.#J
1.#IO

-1.#IND00
-1.$
-1.#J
-1.#IO

The strings are also rounded! That is, ‘#’ + 1 == ‘$’. This can be a bit surprising.

 

Advertisements

2 Responses to Rounding of strings

  1. yuhang says:

    did you find this yourself? interesting …

  2. Petter says:

    Yes, I noticed strange output in my program when NaN was encountered. I am sure I am not the first one to be surprised by this, though…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s