D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3577 - Wrong precedence for opPow
Summary: Wrong precedence for opPow
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Windows
: P2 major
Assignee: No Owner
URL:
Keywords: patch, wrong-code
Depends on:
Blocks:
 
Reported: 2009-12-05 07:40 UTC by David Simcha
Modified: 2015-06-09 05:13 UTC (History)
2 users (show)

See Also:


Attachments
Patch against svn 304 (4.76 KB, patch)
2009-12-21 15:34 UTC, Don
Details | Diff

Note You need to log in before you can comment on or make changes to this issue.
Description David Simcha 2009-12-05 07:40:18 UTC
opPow does not follow mathematically correct precendence.

import std.stdio;

void main() {
    writeln( 2 * 2.0L ^^ 3 + 1);  // 65
    writeln( 2.0L ^^ 3 * 2 + 1);  // 17
}
Comment 1 Don 2009-12-05 11:31:16 UTC
PATCH: To give it higher precedence than *, but lower than unary operators, so that both of the examples return 17, just create a parsePowExp() function in parse.c.

----
Expression *Parser::parseMulExp()
{   Expression *e;
    Expression *e2;
    Loc loc = this->loc;

    e = parsePowExp();
    while (1)
    {
	switch (token.value)
	{
	    case TOKmul: nextToken(); e2 = parsePowExp(); e = new MulExp(loc,e,e2); continue;
	    case TOKdiv: nextToken(); e2 = parsePowExp(); e = new DivExp(loc,e,e2); continue;
	    case TOKmod: nextToken(); e2 = parsePowExp(); e = new ModExp(loc,e,e2); continue;
	    default:
		break;
	}
	break;
    }
    return e;
}

Expression *Parser::parsePowExp()
{   Expression *e;
    Expression *e2;
    Loc loc = this->loc;

    e = parseUnaryExp();
    while (1)
    {
	switch (token.value)
	{
	    case TOKpow: nextToken(); e2 = parseUnaryExp(); e = new PowExp(loc,e,e2); continue;
	    default:
		break;
	}
	break;
    }
    return e;
}
Comment 2 Don 2009-12-05 12:56:29 UTC
To make it right-associative, change the key line in parsePowExp into:
---
	    case TOKpow: nextToken(); e2 = parsePowExp(); e = new PowExp(loc,e,e2); continue;
Comment 3 Don 2009-12-21 15:34:16 UTC
Created attachment 529 [details]
Patch against svn 304

This fixes most of the issues with ^^. It's still necessary to import std.math, but pretty much everything else works.
Comment 4 Don 2009-12-21 15:36:28 UTC
TEST CASES FOR TEST SUITE (Note: doesn't include any of the runtime tests)
-----------------------------
// Tests for ^^
// TODO: These tests should not require import std.math.
import std.math;

// Test float ^^ int
static assert( 27.0 ^^ 5 == 27.0 * 27.0 * 27.0 * 27.0 * 27.0); 

// Check the typing rules.
static assert( is (typeof(2.0^^7) == double));
static assert( is (typeof(7^^3) == int));

static assert( is (typeof(7L^^3) == long));
static assert( is (typeof(7^^3L) == long));
enum short POW_SHORT_1=3;
enum short POW_SHORT_3=7;
static assert( is (typeof(POW_SHORT_1 * POW_SHORT_1) == typeof(POW_SHORT_1*POW_SHORT_1)));

static assert( is (typeof(7.0^^3) == double));
static assert( is (typeof(7.0L^^3) == real));
static assert( is (typeof(7.0f^^3) == float));
static assert( is (typeof(POW_SHORT_1^^3.1) == double));
static assert( is (typeof(POW_SHORT_1^^3.1f) == float));
static assert( is (typeof(2.1f ^^ POW_SHORT_1) == float));
static assert( is (typeof(7.0f^^3.1) == double));
static assert( is (typeof(7.0^^3.1f) == double));
static assert( is (typeof(7.0f^^3.1f) == float));
static assert( is (typeof(7.0f^^3.1L) == real));
static assert( is (typeof(7.0L^^3.1f) == real));
// Check typing for special cases
static assert( is (typeof(7.0f^^2) == float));
static assert( is (typeof(7.0f^^1.0) == double));
static assert( is (typeof(1^^0.5f) == float));
static assert( is (typeof(7^^0.5f) == float));
static assert( is (typeof(3L^^0.5) == double));

static assert(POW_SHORT_1 ^^ 2 == 9);
static assert(4.0 ^^ POW_SHORT_1 == 4.0*4.0*4.0);

// ^^ has higher precedence than multiply
static assert( 2 * 2 ^^ 3 + 1 == 17);
static assert( 2 ^^ 3 * 2 + 1 == 17); 
// ^^ has higher precedence than negate
static assert( -2 ^^ 3 * 2 - 1 == -17);

// ^^ is right associative
static assert( 2 ^^ 3 ^^ 2 == 2 ^^ 9);
static assert( 2.0 ^^ -3 ^^ 2 == 2.0 ^^ -9);

// 1 ^^ n is always 1, even if n is negative
static assert( 1 ^^ -5 == 1);

// -1 ^^ n gets transformed into  n & 1 ? -1 : 1
// even if n is negative
static assert( (-1) ^^ -5 == -1);
static assert( (-1) ^^ -4 == 1);
static assert( (-1) ^^ 0 == 1);

// Other integers raised to negative powers create an error
static assert( !is(typeof(2 ^^ -5)));
static assert( !is(typeof((-2) ^^ -4)));
Comment 5 Walter Bright 2009-12-31 11:20:41 UTC
Fixed dmd 2.038