开发者

What is p-notation in C programming?

I'm learning C right now and there is a conversion specifier %a which writes a number in p-notation as opposed to %e which writes something 开发者_如何学Cin e-notation (exponential notation).

What is p-notation?


You use %a to get a hexadecimal representation of a floating-point number. This might be useful if you are a student learning floating-point representations, or if you want to be able to read and write an exact floating-point number with no rounding error (but not very human-readable).

This format specificier, along with many others, was added as part of the C99 standard. Dinkumware have an excellent C99 library reference free online; it's PJ Plauger's company, and he had a lot to do with both C89 and C99 standard libraries. Link above is to printing functions; the general library reference is http://www.dinkumware.com/manuals/default.aspx


Here is an extract from the c99 standard, section 7.19.6.1 (7) which shows the details for %a or %A (similar to the mac details given by dmckee above):

A double argument representing a floating-point number is converted in the style [−]0xh.hhhhp±d, where there is one hexadecimal digit (which is nonzero if the argument is a normalized floating-point number and is otherwise unspecified) before the decimal-point character and the number of hexadecimal digits after it is equal to the precision; if the precision is missing and FLT_RADIX is a power of 2, then the precision is sufficient for an exact representation of the value; if the precision is missing and FLT_RADIX is not a power of 2, then the precision is sufficient to distinguish248) values of type double, except that trailing zeros may be omitted; if the precision is zero and the # flag is not specified, no decimal- point character appears. The letters abcdef are used for a conversion and the letters ABCDEF for A conversion. The A conversion specifier produces a number with X and P instead of x and p. The exponent always contains at least one digit, and only as many more digits as necessary to represent the decimal exponent of 2. If the value is zero, the exponent is zero.


From the printf(3) man page on my Mac OS X box (therefore the BSD c standard library implementation):

aA
The double argument is rounded and converted to hexadecimal nota- tion in the style [-]0xh.hhhp[+-]d, where the number of digits after the hexadecimal-point character is equal to the precision specification. If the precision is missing, it is taken as enough to represent the floating-point number exactly, and no rounding occurs. If the precision is zero, no hexadecimal-point character appears. The p is a literal character p', and the exponent consists of a positive or negative sign followed by a decimal number representing an exponent of 2. The A conversion uses the prefix ``0X'' (rather than ``0x''), the letters ``ABCDEF'' (rather than ``abcdef'') to represent the hex digits, and the letterP' (rather than `p') to separate the mantissa and exponent.

The 'p' (or 'P') serves to separate the (hexadecimal) mantissa from the (hexadecimal) exponent.

These specifiers are not in my K&R, and the man page is not specific about what standard (if any) specifies them.

I just checked my Debian 5.0 box (using glibc 2.7) which also has it; that man page says that it is c99 related (again, no reference to any particular standard).


This might be useful: http://www.cppreference.com/wiki/c/io/printf

Specifically, here are the format specifiers you can use in printf (w/o modifiers like .02 etc):

Code    Format
%c  character
%d  signed integers
%i  signed integers
%I64d   long long (8B integer), MS-specific
%I64u   unsigned long long (8B integer), MS-specific
%e  scientific notation, with a lowercase “e”
%E  scientific notation, with a uppercase “E”
%f  floating point
%g  use %e or %f, whichever is shorter
%G  use %E or %f, whichever is shorter
%o  octal
%s  a string of characters
%u  unsigned integer
%x  unsigned hexadecimal, with lowercase letters
%X  unsigned hexadecimal, with uppercase letters
%p  a pointer
%n  the argument shall be a pointer to an integer into which is placed the number of characters written so far


There is no %a format specifier (as as I'm aware, and certainly not in any of the common implementations).

There is a %p format specifier which prints a pointer address.

Ref.

UPDATE: please see other posts.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜