NULL, ctx) == 0);
        TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
 
+       /* The expression is a constant 0.0 without needing to evaluate EVENT1. */
+       expr__ctx_clear(ctx);
+       TEST_ASSERT_VAL("find ids",
+                       expr__find_ids("0 & EVENT1 > 0", NULL, ctx) == 0);
+       TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
+       expr__ctx_clear(ctx);
+       TEST_ASSERT_VAL("find ids",
+                       expr__find_ids("EVENT1 > 0 & 0", NULL, ctx) == 0);
+       TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
+       expr__ctx_clear(ctx);
+       TEST_ASSERT_VAL("find ids",
+                       expr__find_ids("1 & EVENT1 > 0", NULL, ctx) == 0);
+       TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
+       TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
+       expr__ctx_clear(ctx);
+       TEST_ASSERT_VAL("find ids",
+                       expr__find_ids("EVENT1 > 0 & 1", NULL, ctx) == 0);
+       TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
+       TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
+
+       /* The expression is a constant 1.0 without needing to evaluate EVENT1. */
+       expr__ctx_clear(ctx);
+       TEST_ASSERT_VAL("find ids",
+                       expr__find_ids("1 | EVENT1 > 0", NULL, ctx) == 0);
+       TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
+       expr__ctx_clear(ctx);
+       TEST_ASSERT_VAL("find ids",
+                       expr__find_ids("EVENT1 > 0 | 1", NULL, ctx) == 0);
+       TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
+       expr__ctx_clear(ctx);
+       TEST_ASSERT_VAL("find ids",
+                       expr__find_ids("0 | EVENT1 > 0", NULL, ctx) == 0);
+       TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
+       TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
+       expr__ctx_clear(ctx);
+       TEST_ASSERT_VAL("find ids",
+                       expr__find_ids("EVENT1 > 0 | 0", NULL, ctx) == 0);
+       TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
+       TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
+
        /* Test toplogy constants appear well ordered. */
        expr__ctx_clear(ctx);
        TEST_ASSERT_VAL("#num_cpus", expr__parse(&num_cpus, ctx, "#num_cpus") == 0);
 
  * constant value using OP. Its invariant that there are no ids.  If computing
  * ids for non-constants union the set of IDs that must be computed.
  */
-#define BINARY_LONG_OP(RESULT, OP, LHS, RHS)                           \
-       if (!compute_ids || (is_const(LHS.val) && is_const(RHS.val))) { \
-               assert(LHS.ids == NULL);                                \
-               assert(RHS.ids == NULL);                                \
-               if (isnan(LHS.val) || isnan(RHS.val)) {                 \
-                       RESULT.val = NAN;                               \
-               } else {                                                \
-                       RESULT.val = (long)LHS.val OP (long)RHS.val;    \
-               }                                                       \
-               RESULT.ids = NULL;                                      \
-       } else {                                                        \
-               RESULT = union_expr(LHS, RHS);                          \
-       }
-
 #define BINARY_OP(RESULT, OP, LHS, RHS)                                        \
        if (!compute_ids || (is_const(LHS.val) && is_const(RHS.val))) { \
                assert(LHS.ids == NULL);                                \
 }
 | ID                           { $$ = handle_id(ctx, $1, compute_ids, /*source_count=*/false); }
 | SOURCE_COUNT '(' ID ')'      { $$ = handle_id(ctx, $3, compute_ids, /*source_count=*/true); }
-| expr '|' expr { BINARY_LONG_OP($$, |, $1, $3); }
-| expr '&' expr { BINARY_LONG_OP($$, &, $1, $3); }
-| expr '^' expr { BINARY_LONG_OP($$, ^, $1, $3); }
+| expr '|' expr
+{
+       if (is_const($1.val) && is_const($3.val)) {
+               assert($1.ids == NULL);
+               assert($3.ids == NULL);
+               $$.ids = NULL;
+               $$.val = (fpclassify($1.val) == FP_ZERO && fpclassify($3.val) == FP_ZERO) ? 0 : 1;
+       } else if (is_const($1.val)) {
+               assert($1.ids == NULL);
+               if (fpclassify($1.val) == FP_ZERO) {
+                       $$ = $3;
+               } else {
+                       $$.val = 1;
+                       $$.ids = NULL;
+                       ids__free($3.ids);
+               }
+       } else if (is_const($3.val)) {
+               assert($3.ids == NULL);
+               if (fpclassify($3.val) == FP_ZERO) {
+                       $$ = $1;
+               } else {
+                       $$.val = 1;
+                       $$.ids = NULL;
+                       ids__free($1.ids);
+               }
+       } else {
+               $$ = union_expr($1, $3);
+       }
+}
+| expr '&' expr
+{
+       if (is_const($1.val) && is_const($3.val)) {
+               assert($1.ids == NULL);
+               assert($3.ids == NULL);
+               $$.val = (fpclassify($1.val) != FP_ZERO && fpclassify($3.val) != FP_ZERO) ? 1 : 0;
+               $$.ids = NULL;
+       } else if (is_const($1.val)) {
+               assert($1.ids == NULL);
+               if (fpclassify($1.val) != FP_ZERO) {
+                       $$ = $3;
+               } else {
+                       $$.val = 0;
+                       $$.ids = NULL;
+                       ids__free($3.ids);
+               }
+       } else if (is_const($3.val)) {
+               assert($3.ids == NULL);
+               if (fpclassify($3.val) != FP_ZERO) {
+                       $$ = $1;
+               } else {
+                       $$.val = 0;
+                       $$.ids = NULL;
+                       ids__free($1.ids);
+               }
+       } else {
+               $$ = union_expr($1, $3);
+       }
+}
+| expr '^' expr
+{
+       if (is_const($1.val) && is_const($3.val)) {
+               assert($1.ids == NULL);
+               assert($3.ids == NULL);
+               $$.val = (fpclassify($1.val) == FP_ZERO) != (fpclassify($3.val) == FP_ZERO) ? 1 : 0;
+               $$.ids = NULL;
+       } else {
+               $$ = union_expr($1, $3);
+       }
+}
 | expr '<' expr { BINARY_OP($$, <, $1, $3); }
 | expr '>' expr { BINARY_OP($$, >, $1, $3); }
 | expr '+' expr { BINARY_OP($$, +, $1, $3); }