9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] NaN, +Inf, and -Inf, constants?
@ 2010-02-06 15:38 Tristan Plumb
  2010-02-06 15:57 ` erik quanstrom
  2010-02-07  0:06 ` Russ Cox
  0 siblings, 2 replies; 15+ messages in thread
From: Tristan Plumb @ 2010-02-06 15:38 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

I'm porting PROJ4.3.3 (the cleanest version of proj I could find), and
it's a little ugly.

One thing leading to another, I am currently centralizing constants, and
found that enums can hold doubles (which is quite nice), but I have no
way of defining NaN or Inf as a constant, is there such a way?

Or is there a way to evaluate constant functions at compile time (eep)?

Or I could just use #define (much safer than the above).

enjoy,
tristan

--
All original matter is hereby placed immediately under the public domain.



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [9fans] NaN, +Inf, and -Inf, constants?
  2010-02-06 15:38 [9fans] NaN, +Inf, and -Inf, constants? Tristan Plumb
@ 2010-02-06 15:57 ` erik quanstrom
  2010-02-06 17:14   ` Tristan Plumb
  2010-02-07  0:06 ` Russ Cox
  1 sibling, 1 reply; 15+ messages in thread
From: erik quanstrom @ 2010-02-06 15:57 UTC (permalink / raw)
  To: 9fans

> One thing leading to another, I am currently centralizing constants, and
> found that enums can hold doubles (which is quite nice), but I have no
> way of defining NaN or Inf as a constant, is there such a way?
>
> Or is there a way to evaluate constant functions at compile time (eep)?

NaN(2) and Inf(2) are not constant functions.  the result depends on the
settings of the fcr (getfcr(2)).

- erik



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [9fans] NaN, +Inf, and -Inf, constants?
  2010-02-06 15:57 ` erik quanstrom
@ 2010-02-06 17:14   ` Tristan Plumb
  2010-02-07 16:12     ` erik quanstrom
  0 siblings, 1 reply; 15+ messages in thread
From: Tristan Plumb @ 2010-02-06 17:14 UTC (permalink / raw)
  To: 9fans

> NaN(2) and Inf(2) are not constant functions.  the result depends on
> the settings of the fcr (getfcr(2)).
Really? My reading of /sys/src/libc/port/nan.c makes me think NaN(2)
returns a uvlong value of 0x7ff0000000000001, miss I something?

On the other hand, the assignment of NaN to a double depends on the fcr.
(And on my machine, curiously changes 0x7ff0...1 to 0x7ff8...1).

So if I think of enum definitions as assignment, it makes sense, mostly.

thanks,
tristan

--
All original matter is hereby placed immediately under the public domain.



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [9fans] NaN, +Inf, and -Inf, constants?
  2010-02-06 15:38 [9fans] NaN, +Inf, and -Inf, constants? Tristan Plumb
  2010-02-06 15:57 ` erik quanstrom
@ 2010-02-07  0:06 ` Russ Cox
  2010-02-07 10:46   ` Charles Forsyth
  1 sibling, 1 reply; 15+ messages in thread
From: Russ Cox @ 2010-02-07  0:06 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

[-- Attachment #1: Type: text/plain, Size: 153 bytes --]

On Sat, Feb 6, 2010 at 7:38 AM, Tristan Plumb <9p-st@imu.li> wrote:

> enums can hold doubles (which is quite nice)
>

Umm, no they can't.

Russ

[-- Attachment #2: Type: text/html, Size: 429 bytes --]

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [9fans] NaN, +Inf, and -Inf, constants?
  2010-02-07  0:06 ` Russ Cox
@ 2010-02-07 10:46   ` Charles Forsyth
  2010-02-07 11:05     ` Charles Forsyth
  2010-02-07 20:01     ` Russ Cox
  0 siblings, 2 replies; 15+ messages in thread
From: Charles Forsyth @ 2010-02-07 10:46 UTC (permalink / raw)
  To: 9fans

h% cat en.c
enum{
	Fred= 1.5
};
double f = Fred;
h% 8c -S en.c
	DATA	f+0(SB)/8,$(1.50000000000000000e+00)
	GLOBL	f+0(SB),$8
	END	,

bug or quirk?
what do you think?



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [9fans] NaN, +Inf, and -Inf, constants?
  2010-02-07 10:46   ` Charles Forsyth
@ 2010-02-07 11:05     ` Charles Forsyth
  2010-02-07 20:01     ` Russ Cox
  1 sibling, 0 replies; 15+ messages in thread
From: Charles Forsyth @ 2010-02-07 11:05 UTC (permalink / raw)
  To: 9fans

i didn't even think to check earlier. even better:

enum{
	Fred= 1.5,
	Sam,
};
double f = Fred;
double g = Sam;

h% 8c -S en.c
	DATA	f+0(SB)/8,$(1.50000000000000000e+00)
	DATA	g+0(SB)/8,$(2.50000000000000000e+00)
	GLOBL	f+0(SB),$8
	GLOBL	g+0(SB),$8
	END	,

sound!



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [9fans] NaN, +Inf, and -Inf, constants?
  2010-02-06 17:14   ` Tristan Plumb
@ 2010-02-07 16:12     ` erik quanstrom
  0 siblings, 0 replies; 15+ messages in thread
From: erik quanstrom @ 2010-02-07 16:12 UTC (permalink / raw)
  To: 9fans

> On the other hand, the assignment of NaN to a double depends on the fcr.
> (And on my machine, curiously changes 0x7ff0...1 to 0x7ff8...1).

http://en.wikipedia.org/wiki/NaN explains the difference between
a signaling nan and a quiet nan (the latter).

> So if I think of enum definitions as assignment, it makes sense, mostly.

how does a non-constant enum make sense?

more to the point, in porting code, you're going to have to work
out how to deal with NaNs.  plan 9 defaults to using signaling
nans, unless you manually change the fcr.  this is good because
it avoids bad conversions.  on the other hand, your application
may require and depend on the ability to pass around nan and
±inf.

- erik



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [9fans] NaN, +Inf, and -Inf, constants?
  2010-02-07 10:46   ` Charles Forsyth
  2010-02-07 11:05     ` Charles Forsyth
@ 2010-02-07 20:01     ` Russ Cox
  2010-02-07 20:31       ` erik quanstrom
  2010-02-07 21:33       ` Charles Forsyth
  1 sibling, 2 replies; 15+ messages in thread
From: Russ Cox @ 2010-02-07 20:01 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

[-- Attachment #1: Type: text/plain, Size: 1168 bytes --]

On Sun, Feb 7, 2010 at 2:46 AM, Charles Forsyth <forsyth@terzarima.net>wrote:

> h% cat en.c
> enum{
>        Fred= 1.5
> };
> double f = Fred;
> h% 8c -S en.c
>        DATA    f+0(SB)/8,$(1.50000000000000000e+00)
>        GLOBL   f+0(SB),$8
>        END     ,
>
> bug or quirk?
> what do you think?
>

i swear i tested this before i replied...

seems like a bug to me.  the spec is pretty clear:

       6.7.2.2  Enumeration specifiers

       Syntax

       [#1]

               enum-specifier:
                       enum identifier-opt { enumerator-list }
                       enum identifier-opt { enumerator-list , }
                       enum identifier

               enumerator-list:
                       enumerator
                       enumerator-list , enumerator

               enumerator:
                       enumeration-constant
                       enumeration-constant = constant-expression

       Constraints

       [#2] The expression that defines the value of an enumeration
       constant shall be an integer constant expression that has  a
       value representable as an int.

russ

[-- Attachment #2: Type: text/html, Size: 1909 bytes --]

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [9fans] NaN, +Inf, and -Inf, constants?
  2010-02-07 20:01     ` Russ Cox
@ 2010-02-07 20:31       ` erik quanstrom
  2010-02-07 21:33       ` Charles Forsyth
  1 sibling, 0 replies; 15+ messages in thread
From: erik quanstrom @ 2010-02-07 20:31 UTC (permalink / raw)
  To: 9fans

>        [#2] The expression that defines the value of an enumeration
>        constant shall be an integer constant expression that has  a
>        value representable as an int.

the spec also doesn't allow vlong enumerations.
should we remove that ability, too?

- erik



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [9fans] NaN, +Inf, and -Inf, constants?
  2010-02-07 20:01     ` Russ Cox
  2010-02-07 20:31       ` erik quanstrom
@ 2010-02-07 21:33       ` Charles Forsyth
  2010-02-07 22:19         ` Lyndon Nerenberg (VE6BBM/VE7TFX)
  1 sibling, 1 reply; 15+ messages in thread
From: Charles Forsyth @ 2010-02-07 21:33 UTC (permalink / raw)
  To: 9fans

>seems like a bug to me.  the spec is pretty clear:

it's quite deliberate, having looked at the code.
i suspect the rationale was that, finally, C provided a way
outside the preprocessor to give symbolic names to constants.
why restrict that to int?
you can't easily get the same effect by adding #define names
to the output symbol table, because those are arbitrary text,
not guaranteed either to be constants, or to be safe for substitution
(without surrounding ())



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [9fans] NaN, +Inf, and -Inf, constants?
  2010-02-07 21:33       ` Charles Forsyth
@ 2010-02-07 22:19         ` Lyndon Nerenberg (VE6BBM/VE7TFX)
  2010-02-07 22:37           ` erik quanstrom
  2010-02-07 23:16           ` Bakul Shah
  0 siblings, 2 replies; 15+ messages in thread
From: Lyndon Nerenberg (VE6BBM/VE7TFX) @ 2010-02-07 22:19 UTC (permalink / raw)
  To: 9fans

> i suspect the rationale was that, finally, C provided a way
> outside the preprocessor to give symbolic names to constants.
> why restrict that to int?

Because enum's have been int's since their inception?

I'm sympathetic to the underlying need, but making a fundamental
type of the language suddenly become variable does not seem to
be the right way of going about this.

E.g., what is the type of:

enum {
	a = 1,
	b = 2.44000000000000000000618549L,
	c = 2.44F,
	d = "this is weird",
	e = 1LL<<62,
} foo;

How on earth do you switch() on it? And what's its sizeof()?

--lyndon




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [9fans] NaN, +Inf, and -Inf, constants?
  2010-02-07 22:19         ` Lyndon Nerenberg (VE6BBM/VE7TFX)
@ 2010-02-07 22:37           ` erik quanstrom
  2010-02-08 16:39             ` Lyndon Nerenberg (VE6BBM/VE7TFX)
  2010-02-07 23:16           ` Bakul Shah
  1 sibling, 1 reply; 15+ messages in thread
From: erik quanstrom @ 2010-02-07 22:37 UTC (permalink / raw)
  To: 9fans

> enum {
> 	a = 1,
> 	b = 2.44000000000000000000618549L,
> 	c = 2.44F,
> 	d = "this is weird",
> 	e = 1LL<<62,
> } foo;
>
> How on earth do you switch() on it? And what's its sizeof()?

why does being able to switch on any enum trump
the ability to define constants without #define?

if you try, sizeof(foo)==4, but you'll need to remove
'd' since you can't have a string.  you can't switch on
a vlong or float.  would be nice, though.

the plan 9 style is never to typedef or name enums.
(though i saw one creep into the source recently.)
so the sizeof problem would naturally never come up.
if it were an error to name or typedef an enum
containing non-int members, there would be no
problem.

- erik



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [9fans] NaN, +Inf, and -Inf, constants?
  2010-02-07 22:19         ` Lyndon Nerenberg (VE6BBM/VE7TFX)
  2010-02-07 22:37           ` erik quanstrom
@ 2010-02-07 23:16           ` Bakul Shah
  1 sibling, 0 replies; 15+ messages in thread
From: Bakul Shah @ 2010-02-07 23:16 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

On Sun, 07 Feb 2010 15:19:58 MST "Lyndon Nerenberg (VE6BBM/VE7TFX)" <lyndon@orthanc.ca>  wrote:
> > i suspect the rationale was that, finally, C provided a way
> > outside the preprocessor to give symbolic names to constants.
> > why restrict that to int?
>
> Because enum's have been int's since their inception?
>
> I'm sympathetic to the underlying need, but making a fundamental
> type of the language suddenly become variable does not seem to
> be the right way of going about this.
>
> E.g., what is the type of:
>
> enum {
> 	a = 1,
> 	b = 2.44000000000000000000618549L,
> 	c = 2.44F,
> 	d = "this is weird",
> 	e = 1LL<<62,
> } foo;

The cleanest solution would be to treat C's _static const_
"variables" as as compile time constants. I wish the standard
dictated this.



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [9fans] NaN, +Inf, and -Inf, constants?
  2010-02-07 22:37           ` erik quanstrom
@ 2010-02-08 16:39             ` Lyndon Nerenberg (VE6BBM/VE7TFX)
  2010-02-08 18:48               ` Russ Cox
  0 siblings, 1 reply; 15+ messages in thread
From: Lyndon Nerenberg (VE6BBM/VE7TFX) @ 2010-02-08 16:39 UTC (permalink / raw)
  To: 9fans

> why does being able to switch on any enum trump
> the ability to define constants without #define?

Because enum's legacy is that of a 'first class' int-like
object, which can be subject to the usual set of int-like
operations. switch() is one of those. #define isn't.

> if you try, sizeof(foo)==4, but you'll need to remove
> 'd' since you can't have a string.  you can't switch on
> a vlong or float.  would be nice, though.

Why not a string? If you can extend an enum to include floats,
why not an integer representation of a pointer?

And yes, I also think switching on vlongs would be useful.

> the plan 9 style is never to typedef or name enums.

It may not be the style for the Labs, but that's not the case for
everyone.  The compile time type checking named enums provide isn't
something I'd want to lose.

> if it were an error to name or typedef an enum
> containing non-int members, there would be no
> problem.

This could work. But I have to agree with Bakul: 'static const'
is a much better fit for the language.

--lyndon




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [9fans] NaN, +Inf, and -Inf, constants?
  2010-02-08 16:39             ` Lyndon Nerenberg (VE6BBM/VE7TFX)
@ 2010-02-08 18:48               ` Russ Cox
  0 siblings, 0 replies; 15+ messages in thread
From: Russ Cox @ 2010-02-08 18:48 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

we could spend a lot of time debating proposed
changes, but i think it's fine to leave as is.
there appears to be a good, justifiable reason
for it, and it's already implemented.

russ


^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2010-02-08 18:48 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-02-06 15:38 [9fans] NaN, +Inf, and -Inf, constants? Tristan Plumb
2010-02-06 15:57 ` erik quanstrom
2010-02-06 17:14   ` Tristan Plumb
2010-02-07 16:12     ` erik quanstrom
2010-02-07  0:06 ` Russ Cox
2010-02-07 10:46   ` Charles Forsyth
2010-02-07 11:05     ` Charles Forsyth
2010-02-07 20:01     ` Russ Cox
2010-02-07 20:31       ` erik quanstrom
2010-02-07 21:33       ` Charles Forsyth
2010-02-07 22:19         ` Lyndon Nerenberg (VE6BBM/VE7TFX)
2010-02-07 22:37           ` erik quanstrom
2010-02-08 16:39             ` Lyndon Nerenberg (VE6BBM/VE7TFX)
2010-02-08 18:48               ` Russ Cox
2010-02-07 23:16           ` Bakul Shah

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).