Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #25220 > unrolled thread
| Started by | Guillaume Dargaud <use_the_contact_form@www.gdargaud.net> |
|---|---|
| First post | 2012-08-28 13:28 +0200 |
| Last post | 2012-08-29 17:50 +0200 |
| Articles | 10 — 6 participants |
Back to article view | Back to comp.lang.c
OxCp syntax ? Guillaume Dargaud <use_the_contact_form@www.gdargaud.net> - 2012-08-28 13:28 +0200
Re: OxCp syntax ? Les Cargill <lcargill99@comcast.com> - 2012-08-28 07:30 -0500
Re: OxCp syntax ? Les Cargill <lcargill99@comcast.com> - 2012-08-28 07:41 -0500
Re: OxCp syntax ? James Kuyper <jameskuyper@verizon.net> - 2012-08-28 09:00 -0400
Re: OxCp syntax ? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-28 09:10 -0400
Re: OxCp syntax ? James Kuyper <jameskuyper@verizon.net> - 2012-08-28 09:58 -0400
Re: OxCp syntax ? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-28 09:02 -0400
Re: OxCp syntax ? Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> - 2012-08-29 19:16 +0200
Re: OxCp syntax ? Nobody <nobody@nowhere.com> - 2012-08-28 14:17 +0100
Re: OxCp syntax ? Guillaume Dargaud <use_the_contact_form@www.gdargaud.net> - 2012-08-29 17:50 +0200
| From | Guillaume Dargaud <use_the_contact_form@www.gdargaud.net> |
|---|---|
| Date | 2012-08-28 13:28 +0200 |
| Subject | OxCp syntax ? |
| Message-ID | <k1ia14$kjn$1@ccpntc8.in2p3.fr> |
Hello all, I just ran across this syntax: #define NAN 0xCp125f Can somebody explain ? A quick googling turned out nothing. -- Guillaume Dargaud http://www.gdargaud.net/
[toc] | [next] | [standalone]
| From | Les Cargill <lcargill99@comcast.com> |
|---|---|
| Date | 2012-08-28 07:30 -0500 |
| Message-ID | <k1idco$i2k$1@dont-email.me> |
| In reply to | #25220 |
Guillaume Dargaud wrote: > Hello all, > I just ran across this syntax: > #define NAN 0xCp125f > Can somebody explain ? A quick googling turned out nothing. > p appears to be shorthand for "decimal point". 0xap2f seems tp map to 100 decimal. -- Les Cargill
[toc] | [prev] | [next] | [standalone]
| From | Les Cargill <lcargill99@comcast.com> |
|---|---|
| Date | 2012-08-28 07:41 -0500 |
| Message-ID | <k1ie1a$lrv$1@dont-email.me> |
| In reply to | #25229 |
Les Cargill wrote:
> Guillaume Dargaud wrote:
>> Hello all,
>> I just ran across this syntax:
>> #define NAN 0xCp125f
>> Can somebody explain ? A quick googling turned out nothing.
>>
>
> p appears to be shorthand for "decimal point".
>
> 0xap2f seems tp map to 100 decimal.
>
> --
> Les Cargill
>
>
Oops! 0xAp001f is 20 decimal. 0xap2f would be *40*, not 100.
C:\c\usenet>gcc -o nan.exe nan.c
C:\c\usenet>nan
NAN=1.#INF00
NAN=20.000000
C:\c\usenet>cat nan.c
#define NAN 0xCp125f
#define MAN 0xAp001f
int main(void)
{
double nan = NAN;
double man = MAN;
printf("NAN=%lf \n",nan);
printf("NAN=%lf \n",man);
return 0;
}
--
Les Cargill
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2012-08-28 09:00 -0400 |
| Message-ID | <k1ifdp$v35$1@dont-email.me> |
| In reply to | #25220 |
On 08/28/2012 07:28 AM, Guillaume Dargaud wrote: > Hello all, > I just ran across this syntax: > #define NAN 0xCp125f > Can somebody explain ? A quick googling turned out nothing. C99 added the capability of specifying floating point constants using hexadecimal notation. They are case-insensitive, just like hexadecimal constants and decimal floating point constants. The 0x is the hexadecimal prefix. "C" is the hexadecimal digit sequence, corresponding to a decimal value of 13. 'p' is the separator, corresponding to "e" in decimal floating point constants. 125 is the exponent. "f" is a suffix indicating that this constant has the type 'float'. This particular constant represents a float value of 0xC*2^125. Compare to 5e60, which represents 5*10^60. Fractional parts are also allowed, but unlike decimal floating point constants, the binary exponent part cannot be dropped: #define PI 0x3.243F6A88p0 One of the most important characteristics of hexadecimal floating point constants is explained in 6.4.4.2p3: > ... For decimal floating constants, and also for > hexadecimal floating constants when FLT_RADIX is not a power of 2, the result is either > the nearest representable value, or the larger or smaller representable value immediately > adjacent to the nearest representable value, chosen in an implementation-defined manner. > For hexadecimal floating constants when FLT_RADIX is a power of 2, the result is > correctly rounded. ... Almost all real-world implementations of C have a power of 2 for FLT_RADIX, so this guarantee means that using a hexadecimal floating point value gives you more precise control over the value that it represents than a decimal floating point value would. Note: it's not clear to me that, on a system which supports true NaNs, that 0xCp125f could be a NaN. If 0xC*2^125 is representable, then by definition it isn't a NaN. If it is not representable, then a diagnostic is required (the behavior being undefined at that point, it would be permissible for a true NaN to be generated). However, if you ran into this definition inside of <math.h>, then I might be wrong about that. -- James Kuyper
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2012-08-28 09:10 -0400 |
| Message-ID | <k1ig0u$vjt$2@dont-email.me> |
| In reply to | #25232 |
On 8/28/2012 9:00 AM, James Kuyper wrote:
> [...]"C" is the hexadecimal digit sequence, corresponding
> to a decimal value of 13. [...]
Baker's hex?
--
Eric Sosman
esosman@ieee-dot-org.invalid
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2012-08-28 09:58 -0400 |
| Message-ID | <k1iiq0$jqi$2@dont-email.me> |
| In reply to | #25234 |
On 08/28/2012 09:10 AM, Eric Sosman wrote: > On 8/28/2012 9:00 AM, James Kuyper wrote: >> [...]"C" is the hexadecimal digit sequence, corresponding >> to a decimal value of 13. [...] > > Baker's hex? :-} I'll use that excuse. -- James Kuyper
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2012-08-28 09:02 -0400 |
| Message-ID | <k1ifgt$vjt$1@dont-email.me> |
| In reply to | #25220 |
On 8/28/2012 7:28 AM, Guillaume Dargaud wrote:
> Hello all,
> I just ran across this syntax:
> #define NAN 0xCp125f
> Can somebody explain ? A quick googling turned out nothing.
The expansion is a hexadecimal floating-point constant
(see 6.4.4.2). "0xC" is the fraction, "p125" is the power
of two, and "f" means `float' as opposed to `double'. So
the value is (float)(12*pow(2,125)), but without all that
tedious computation and risk of inaccuracy.
Note that 12 * 2^125 is 3 * 2^127, which would represent
a 32-bit quiet NaN in IEEE 32-bit format. Off-hand I don't
know whether the compiler is obligated to produce this result
for a constant whose "lexical" value is out of range, but that
seems to be the intent. Ordinarily one would use the NAN macro
from <math.h> -- is that where you found this code?
--
Eric Sosman
esosman@ieee-dot-org.invalid
[toc] | [prev] | [next] | [standalone]
| From | Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> |
|---|---|
| Date | 2012-08-29 19:16 +0200 |
| Message-ID | <u7s%r.203095$GZ3.85080@tornado.fastwebnet.it> |
| In reply to | #25233 |
Il 28/08/2012 15:02, Eric Sosman ha scritto: [..] > Ordinarily one would use the NAN macro > from <math.h> -- is that where you found this code? Yes, I think this is how National Instruments' LabWindows/CVI compiler defines it: http://forums.ni.com/t5/LabWindows-CVI/clang-CVI-C99/td-p/1463014 -- Vincenzo Mercuri
[toc] | [prev] | [next] | [standalone]
| From | Nobody <nobody@nowhere.com> |
|---|---|
| Date | 2012-08-28 14:17 +0100 |
| Message-ID | <pan.2012.08.28.13.18.11.113000@nowhere.com> |
| In reply to | #25220 |
On Tue, 28 Aug 2012 13:28:35 +0200, Guillaume Dargaud wrote: > I just ran across this syntax: > #define NAN 0xCp125f > Can somebody explain ? A quick googling turned out nothing. It's a hexadecimal floating-point constant (see 6.4.4.2). "0x" is the hexadecimal prefix. "C" is the value of the significand (mantissa) in hex, i.e. 12 decimal. "p" is the binary exponent indicator (binary version of "e"). "125" is the binary exponent in decimal. "f" is the "float" (single-precision) suffix. So: 0xCp125f is 12*2^125 ~= 5.104e+38.
[toc] | [prev] | [next] | [standalone]
| From | Guillaume Dargaud <use_the_contact_form@www.gdargaud.net> |
|---|---|
| Date | 2012-08-29 17:50 +0200 |
| Message-ID | <k1ldoi$ino$1@ccpntc8.in2p3.fr> |
| In reply to | #25220 |
Thanks all. -- Guillaume Dargaud http://www.gdargaud.net/
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web