You're trying too hard...byuu wrote:Oops, NAND you're right :P
Well in that case, x^^y == !x != !y.
Code: Select all
NAND|0 1
----+---
0|1 1
1|1 0
And using your model, but simpler, XOR: x^^y = (bool)x != (bool)y
Moderator: ZSNES Mods
You're trying too hard...byuu wrote:Oops, NAND you're right :P
Well in that case, x^^y == !x != !y.
Code: Select all
NAND|0 1
----+---
0|1 1
1|1 0
Code: Select all
<jmr> bsnes has the most accurate wiki page but it takes forever to load (or something)
Code: Select all
<jmr> bsnes has the most accurate wiki page but it takes forever to load (or something)
Eh, could be worse.byuu wrote:Nevermind. I need to just kill my sense of humor already, since nobody ever follows.
Code: Select all
<adventureoflink> How about DR F
<adventureoflink> All I want/need is DR F
<grinvader> DR == snes ratio
<adventureoflink> yeah, I know
<grinvader> you can't do that with 1280x800
<grinvader> 1.6 ratio
<adventureoflink> ah
<adventureoflink> why not
<grinvader> because that's not how it works
<adventureoflink> oh ok.
<adventureoflink> So I'm stuck with DS
<adventureoflink> What, so I can't pick PSP ? :P
<grinvader> bad jokes won't help
Code: Select all
<jmr> bsnes has the most accurate wiki page but it takes forever to load (or something)
Yep, that's what I was trying but my code is returning incorrect results.gladius wrote:byuu: Oops, you are right, the way it's currently done a comparison is assumed to be a ternary expression. To fix that you can add specific handling for the ? operator, and just make it do the zero = false, non-zero = true in the same way C does it. Comparisons would evaluate to zero and one then. That should actually simplify things a bit.
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define maxlevel 12
int strmath_rdp(const char *&s, int level = 0) {
if(level == maxlevel) {
if(*s == '(') {
int result = strmath_rdp(++s, 0);
s++;
return result;
} else if(*s == '+') {
return +strmath_rdp(++s, maxlevel);
} else if(*s == '-') {
return -strmath_rdp(++s, maxlevel);
} else if(*s == '!') {
return !strmath_rdp(++s, maxlevel);
} else if(*s == '~') {
return ~strmath_rdp(++s, maxlevel);
} else if(*s >= '0' && *s <= '9') {
int num, len;
sscanf(s, "%d%n", &num, &len);
s += len;
return num;
}
}
int lhs = strmath_rdp(s, level + 1);
while(*s) {
const char *org = s;
int a = *org;
int b = *org ? *(org + 1) : 0;
switch(level) {
case 0:
if(a == '?')break;
return lhs;
case 1:
if(a == '|' && b == '|') { s++; break; }
return lhs;
case 2:
if(a == '^' && b == '^') { s++; break; }
return lhs;
case 3:
if(a == '&' && b == '&') { s++; break; }
return lhs;
case 4:
if(a == '|' && b != '|')break;
return lhs;
case 5:
if(a == '^' && b != '^')break;
return lhs;
case 6:
if(a == '&' && b != '&')break;
return lhs;
case 7:
if(a == '=' && b == '=') { s++; break; }
if(a == '!' && b == '=') { s++; break; }
return lhs;
case 8:
if(a == '<' && b == '=') { s++; break; }
if(a == '>' && b == '=') { s++; break; }
if(a == '<')break;
if(a == '>')break;
return lhs;
case 9:
if(a == '<' && b == '<') { s++; break; }
if(a == '>' && b == '>') { s++; break; }
return lhs;
case 10:
if(a == '+')break;
if(a == '-')break;
return lhs;
case 11:
if(a == '*')break;
if(a == '/')break;
if(a == '%')break;
return lhs;
}
int rhs = strmath_rdp(++s, level + 1);
a = *org;
b = *org ? *(org + 1) : 0;
if(a == '?') {
int tr = rhs;
int fr = strmath_rdp(++s, level);
lhs = (lhs) ? tr : fr;
}
else if(a == '|' && b == '|') lhs = (lhs || rhs);
else if(a == '^' && b == '^') lhs = (!lhs != !rhs);
else if(a == '&' && b == '&') lhs = (lhs && rhs);
else if(a == '|' && b != '|') lhs |= rhs;
else if(a == '^' && b != '^') lhs ^= rhs;
else if(a == '&' && b != '&') lhs &= rhs;
else if(a == '=' && b == '=') lhs = (lhs == rhs);
else if(a == '!' && b == '=') lhs = (lhs != rhs);
else if(a == '<' && b == '=') lhs = (lhs <= rhs);
else if(a == '>' && b == '=') lhs = (lhs >= rhs);
else if(a == '<' && b != '<') lhs = (lhs < rhs);
else if(a == '>' && b != '>') lhs = (lhs > rhs);
else if(a == '<' && b == '<') lhs <<= rhs;
else if(a == '>' && b == '>') lhs >>= rhs;
else if(a == '+') lhs += rhs;
else if(a == '-') lhs -= rhs;
else if(a == '*') lhs *= rhs;
else if(a == '/') lhs /= rhs;
else if(a == '%') lhs %= rhs;
}
return lhs;
}
#undef maxlevel
int strmath(const char *eval) {
return strmath_rdp(eval);
}
int main() {
printf("%10s = %5d, %5d\n", "5+3*7", strmath("5+3*7"), 5+3*7);
printf("%10s = %5d, %5d\n", "+3-2", strmath("+3-2"), +3-2);
printf("%10s = %5d, %5d\n", "0||1", strmath("0||1"), 0||1);
printf("%10s = %5d, %5d\n", "2==3", strmath("2==3"), 2==3);
printf("%10s = %5d, %5d\n", "!(3>=2)", strmath("!(3>=2)"), !(3>=2));
printf("%10s = %5d, %5d\n", "2<<4", strmath("2<<4"), 2<<4);
printf("%10s = %5d, %5d\n", "1<2?3:4", strmath("1<2?3:4"), 1<2?3:4);
printf("%10s = %5d, %5d\n", "1>2?3:4", strmath("1>2?3:4"), 1>2?3:4);
printf("%10s = %5d, %5d\n", "1^^0", strmath("1^^0"), !1!=!0);
getch();
return 0;
}
Not on my Athlon 3500+. Example:Btw, casts are in the worst case as fast as !-ing each param, since ! evaluates the param as a bool anyway in order to reverse it, heh.
Code: Select all
#define maxlevel 12
int strmath_rdp(const char *&s, int level = 0) {
if(level == maxlevel) {
if(*s == '(') {
int result = strmath_rdp(++s, 0);
s++;
return result;
} else if(*s == '+') {
return +strmath_rdp(++s, maxlevel);
} else if(*s == '-') {
return -strmath_rdp(++s, maxlevel);
} else if(*s == '!') {
return !strmath_rdp(++s, maxlevel);
} else if(*s == '~') {
return ~strmath_rdp(++s, maxlevel);
} else if(*s >= '0' && *s <= '9') {
int num, len;
sscanf(s, "%d%n", &num, &len);
s += len;
return num;
}
}
int lhs = strmath_rdp(s, level + 1);
while(*s) {
const char *org = s;
int a = *org;
int b = *org ? *(org + 1) : 0;
switch(level) {
case 0:
if(a == '?')break;
return lhs;
case 1:
if(a == '|' && b == '|') { s++; break; }
return lhs;
case 2:
if(a == '^' && b == '^') { s++; break; }
return lhs;
case 3:
if(a == '&' && b == '&') { s++; break; }
return lhs;
case 4:
if(a == '|' && b != '|')break;
return lhs;
case 5:
if(a == '^' && b != '^')break;
return lhs;
case 6:
if(a == '&' && b != '&')break;
return lhs;
case 7:
if(a == '=' && b == '=') { s++; break; }
if(a == '!' && b == '=') { s++; break; }
return lhs;
case 8:
if(a == '<' && b == '=') { s++; break; }
if(a == '>' && b == '=') { s++; break; }
if(a == '<')break;
if(a == '>')break;
return lhs;
case 9:
if(a == '<' && b == '<') { s++; break; }
if(a == '>' && b == '>') { s++; break; }
return lhs;
case 10:
if(a == '+')break;
if(a == '-')break;
return lhs;
case 11:
if(a == '*')break;
if(a == '/')break;
if(a == '%')break;
return lhs;
}
if(a == '?') {
int tr = strmath_rdp(++s, 0);
int fr = strmath_rdp(++s, 0);
lhs = (lhs) ? tr : fr;
} else {
int rhs = strmath_rdp(++s, level + 1);
if(a == '|' && b == '|') lhs = (lhs || rhs);
else if(a == '^' && b == '^') lhs = (!lhs != !rhs);
else if(a == '&' && b == '&') lhs = (lhs && rhs);
else if(a == '|' && b != '|') lhs |= rhs;
else if(a == '^' && b != '^') lhs ^= rhs;
else if(a == '&' && b != '&') lhs &= rhs;
else if(a == '=' && b == '=') lhs = (lhs == rhs);
else if(a == '!' && b == '=') lhs = (lhs != rhs);
else if(a == '<' && b == '=') lhs = (lhs <= rhs);
else if(a == '>' && b == '=') lhs = (lhs >= rhs);
else if(a == '<' && b != '<') lhs = (lhs < rhs);
else if(a == '>' && b != '>') lhs = (lhs > rhs);
else if(a == '<' && b == '<') lhs <<= rhs;
else if(a == '>' && b == '>') lhs >>= rhs;
else if(a == '+') lhs += rhs;
else if(a == '-') lhs -= rhs;
else if(a == '*') lhs *= rhs;
else if(a == '/') lhs /= rhs;
else if(a == '%') lhs %= rhs;
}
}
return lhs;
}
#undef maxlevel
int strmath(const char *eval) {
return strmath_rdp(eval);
}