Truncation of fractions in floating-point literals

C

Cezary Noweta

Hello,

In Control Panel, Regional Settings, set decimal point to something other then dot
,,.'' (ASCII 0x2E). Let it be comma (ASCII 0x2C) for example. Compile and run the
following code

=== BEGIN of test.c ===
#include <math.h>
#include <stdio.h>

main()
{
#pragma setlocale(".ACP")
printf("ANSI: %3g %3g %3g %3g %7g\n", 0.1, 0.9, 1.1, 1.9, pow(1.999, 10.0));
#pragma setlocale("C")
printf("C: %3g %3g %3g %3g %7g\n", 0.1, 0.9, 1.1, 1.9, pow(1.999, 10.0));
}
=== END of test.c ===

The program gives following result:

ANSI: 0 0 1 1 1
C: 0.1 0.9 1.1 1.9 1018.89

It looks that the lexical analizer internally uses setlocaled version of
atof()/sscanf() to retrieve value of the FP literals. This is strange bug in the
lexer. Such behaviour breaks elementary rules of ISO C standard. Moreover, it does
not generate any warnings/errors regardless of warning level.

Tested on all versions of CL from VS 6.0 SP5 to VS 2005 SP1.

-- best regards
Cezary Noweta
 
C

Charles Wang[MSFT]

Hi Cezary,
I reproduced your issue at my side. It indeed seems to be a product issue.
I need to confirm this with the product team, however the process may need
a long time. If I could not get a response within two business days, I will
file a product issue to them and let you know.

Thanks for your feedback. Have a nice day!

Best regards,
Charles Wang
Microsoft Online Community Support
=====================================================
Get notification to my posts through email? Please refer to:
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications

If you are using Outlook Express, please make sure you clear the check box
"Tools/Options/Read: Get 300 headers at a time" to see your reply promptly.


Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
======================================================
When responding to posts, please "Reply to Group" via
your newsreader so that others may learn and benefit
from this issue.
======================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
======================================================
 
C

Cezary Noweta

Hello,
I need to confirm this with the product team, however the process may need
a long time.

Thank you. Hopefuly, the bug is easy to omitting in a code, but under condition that
programmer know about it. Usually nobody assumes that compiler incorectly calculates
a value of literals. When pi == 3 and exp(1) == 2 results can be strange and nobody
knows why.

I'd like to inform that there are other strange behaviours (if not bugs) of the
compiler concerning character sets. I will describe CL 14.0 (from VS 2005 SP1). I'm
using English version of VS - not Asian ones. To illustrate the problems I've
attached two files. If they are deleted from my post then I will place their
description:

=== BEGIN OF test-ansi.c ===
#pragma setlocale(".932")

#define MSG_SPACE "Some Japanese DBCS text\n" \
"containing backslash\n" \
"as the trailing character"

main()
{
char *s = MSG_SPACE;
}
=== END OF test-ansi.c ===

=== BEGIN OF test-uni.c ===
The same as above test-ansi.c file, but encoded as UTF-16
=== END OF test-uni.c ===

My system codepage is 1250.

I will refer to the ISO C (9899:1999) which is the same as the ISO C++ in the
following case.

Compiling ,,test-ansi.c'' gives following warning:

test-ansi.c(9) : warning C4129: '' : unrecognized character escape sequence

According to ISO C ,,5.2.1.2 Multibyte characters'': <<While in the initial shift
state, all single-byte characters retain their usual interpretation and do not alter
the shift state. The interpretation for subsequent bytes in the sequence is a
function of the current shift state.>>

In ISO C ,,5.1.1.2 Translation phases'' states that conversion of escape sequences
occurs after preprocessing directives are executed. So preprocessor should know that
the string is in 932 DBCS and the backslash is a trailing char - not a leading one.

Adding the second backslash produce desired results. Fortunately CL 14.0 handles
UTF-16 files. However compiling ,,test-uni.c'' gives many of the following warning:

test-uni.c(9) : warning C4566: character represented by universal-character-name
'\u7A7A' cannot be represented in the current code page (1250)

which gives "??????????" string - not a 932 DBCS one.

As above, according to the ,,5.1.1.2 Translation phases'', <<5. Each source character
set member and escape sequence in character constants and string literals is
converted to the corresponding member of the execution character set; if there is no
corresponding member, it is converted to an implementation defined member other than
the null (wide) character.>>, which occurs _after_ preprocessing directives are
executed (phase 4.), so preprocessor should know that it should convert unicode
string to DBCS codepage 932 and not to SBCS 1250 codepage (which is my system
codepage).

-- best regards

Cezary Noweta
 
C

Charles Wang[MSFT]

Hi Cezary,
Thank you very much for your feedback.

For the warning C4129, it seems to be Microsoft specific. It is explained
in this article:
C++ Character Constants
http://msdn2.microsoft.com/en-us/library/6aw8xdf2(VS.80).aspx

"If the character following the backslash does not specify a legal escape
sequence, the result is implementation defined. In Microsoft C++, the
character following the backslash is taken literally, as though the escape
were not present, and a level 1 warning ("unrecognized character escape
sequence") is issued."

If you change "\•" to something like "\\", "\r", "\n", or "\t" etc, then
the warning will disappear. Just the character following the backslash
should be a legal escape sequence.

For the C4566 warning, you may refer to this article:
Compiler Warning (level 1) C4566
http://msdn2.microsoft.com/en-us/library/5zkb5h07(VS.80).aspx

However it seemed that this article could not explain the C4566 error in
your scenario. I will also consult the product team on this issue.

If it is convenient for you, I recommend that you can give Microsoft
feedback directly via:
https://connect.microsoft.com/VisualStudio/Feedback

Best regards,
Charles Wang
Microsoft Online Community Support
=====================================================
Get notification to my posts through email? Please refer to:
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications

If you are using Outlook Express, please make sure you clear the check box
"Tools/Options/Read: Get 300 headers at a time" to see your reply promptly.


Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
======================================================
When responding to posts, please "Reply to Group" via
your newsreader so that others may learn and benefit
from this issue.
======================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
======================================================
 
C

Charles Wang[MSFT]

Hi Cezary,
I just get the response from development team regarding your issues:
"You're using #pragma, which by definition is implementation-dependent, so
concerns about ISO standard are irrelevant. You're explicitly using
non-standard additions to C. In your first program in the mail, perhaps you
mean to use the setlocale runtime function, rather than the compile-time
#pragma. Then you ARE in ISO territory, and you'll likely see behavior more
like your expecting."

Based on his description, I also tried using the run-time setlocale
function and I found that the result was as expected:
Code:
setlocale(LC_ALL,".ACP");
printf("ANSI: %3g %3g %3g %3g %7g\n", 0.1, 0.9, 1.1, 1.9, pow(1.999,
10.0));
setlocale(LC_ALL,"C");
printf("C: %5g %5g %5g %5g %7g\n", 0.1, 0.9, 1.1, 1.9, pow(1.999,
10.0));

Result:
ANSI: 0;1 0;9 1;1 1;9 1018;89
C: 0.1 0.9 1.1 1.9 1018.89

For your second concerns, they gave me the following response:
"When I open your test-uni.c program in codepage 932 (Shift-JIS), it sure
doesn't look right. Note the first \n at the end of the line has been
eaten, so it looks like the text is not really valid codepage 932. Codepage
936 (Chinese Simplified (GB2312)) looks more like the right codepage.
I would fully expect to see errors if the text and the codepage don't match.
The documentation says that #pragma setlocale:
Defines the locale (Country/Region and language) to be used when
translating wide-character constants and string literals.
Your program doesn't have any wide-char text. The statement is ambiguous,
though (bad doc writer!). Does "wide-character" apply to "string literals"
or only "constants"? IOW does setlocale affect ALL string literals?
"
However from my test even using wchar_t, the issue was still there, I am
further consulting the development team to see if I should submit a product
issue to them.

Please feel free to let me know if you have any other questions or
concerns. Have a nice day!

Best regards,
Charles Wang
Microsoft Online Community Support
=====================================================
Get notification to my posts through email? Please refer to:
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications

If you are using Outlook Express, please make sure you clear the check box
"Tools/Options/Read: Get 300 headers at a time" to see your reply promptly.


Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
======================================================
When responding to posts, please "Reply to Group" via
your newsreader so that others may learn and benefit
from this issue.
======================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
======================================================
 
C

Charles Wang[MSFT]

Hi Cezary,
Thanks for your detailed feedback.
test-uni.c is in UTF-16 format while test-ansi.c is CP 932. Maybe test-uni.c is
converted to ansi by your news client program - actually I have no 936 texts under my
hand. I have only Japanese and Chinese Traditional ones. I have attached zipped files
and snapshot of contents - Japanese speaking people can recognize it.
I did not change the program. It seemed that the developer just meant that
it was more like CP 936, not real.

Anyway, I have submitted a product issue to the development team. I will
track the issue status. If there is any update, I will post it here.

Please feel free to let me know if you have any other questions or
concerns. Thanks again for your kind feedback.

Best regards,
Charles Wang
Microsoft Online Community Support
=====================================================
Get notification to my posts through email? Please refer to:
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications

If you are using Outlook Express, please make sure you clear the check box
"Tools/Options/Read: Get 300 headers at a time" to see your reply promptly.


Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
======================================================
When responding to posts, please "Reply to Group" via
your newsreader so that others may learn and benefit
from this issue.
======================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
======================================================
 
C

Charles Wang[MSFT]

Hi Cezary,
It is confirmed that the 1st floating-point literals has been fixed in the
next release Orcas (VC9.0).
For the left issues, the product team has accepted them as product issues
and I believe that they will try to fix them in future.

Best regards,
Charles Wang
Microsoft Online Community Support
=====================================================
Get notification to my posts through email? Please refer to:
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications

If you are using Outlook Express, please make sure you clear the check box
"Tools/Options/Read: Get 300 headers at a time" to see your reply promptly.


Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
======================================================
When responding to posts, please "Reply to Group" via
your newsreader so that others may learn and benefit
from this issue.
======================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
======================================================
 
C

Cezary Noweta

Hello,
It is confirmed that the 1st floating-point literals has been fixed in the
next release Orcas (VC9.0).

Thank you. I belive the world will be better when next programmers will not waste
time with the question ,,why this does not work?'' in mind.
For the left issues, the product team has accepted them as product issues
and I believe that they will try to fix them in future.

OK - so I am waiting patiently.

-- best regards

Cezary Noweta
 
Top