Ticket #114: pow-expr2.patch
File pow-expr2.patch, 4.7 KB (added by hopscc, 16 years ago) |
---|
-
Source/BackEndClr/SharpGenerator.cobra
4080 4080 on 'STAR_EQUALS' 4081 4081 op = '*=' 4082 4082 on 'STARSTAR_EQUALS' 4083 #op = 'CobraLangInternal.CobraImp.PowerToEquals('4084 op = 'System.Math.Pow('4083 op = 'CobraLangInternal.CobraImp.PowerTo(' 4084 #op = 'System.Math.Pow(' 4085 4085 on 'SLASH_EQUALS' 4086 4086 op = '/=' # TODO: finish this 4087 4087 on 'SLASHSLASH_EQUALS' … … 4091 4091 else 4092 4092 throw FallThroughException(_op) 4093 4093 assert op.length 4094 if op.length==2 4094 if op.length==2 # One of C# augAssign operators 4095 4095 _left.writeSharpDef(sw) 4096 4096 sw.write(op) 4097 4097 _right.writeSharpDef(sw) 4098 else 4098 else # method call assumed to return value 4099 _left.writeSharpDef(sw) 4100 sw.write(" = " ) 4099 4101 sw.write(op) # ex: 'CobraLangInternal.CobraImp.Foo(' 4100 4102 _left.writeSharpDef(sw) 4101 4103 sw.write(sep) … … 4233 4235 on 'STAR' 4234 4236 op = '*' 4235 4237 on 'STARSTAR' 4236 #op = 'CobraLangInternal.CobraImp.PowerTo('4237 op = 'System.Math.Pow('4238 if _left.isKindOf(intType) and _right.isKindOf(intType)4239 pre = '(' + .compiler.intType.sharpRef + ')'4238 op = 'CobraLangInternal.CobraImp.PowerTo(' 4239 #op = 'System.Math.Pow(' 4240 #if _left.isKindOf(intType) and _right.isKindOf(intType) 4241 # pre = '(' + .compiler.intType.sharpRef + ')' 4240 4242 on 'SLASH' 4241 4243 op = '/' 4242 4244 if _left.isKindOf(intType) and _right.isKindOf(intType) -
Source/Cobra.Lang/Native.cs
1103 1103 return a / b; 1104 1104 } 1105 1105 1106 static public int PowerTo(int a, int b) { 1107 // return (int) Math.Pow(a, b) 1108 // below is about 25x faster than calling Math.Pow 1109 if (b == 0) 1110 return 1; 1111 int r = 0; 1112 if ( b > 0 ) { 1113 r = a; 1114 while (b > 1) { 1115 r = r * a; 1116 b = b - 1; 1117 } 1118 } 1119 return r; 1120 } 1121 1122 static public Double PowerTo(Double a, Double b) { 1123 return System.Math.Pow(a, b); 1124 } 1125 1106 1126 static public string RunAndCaptureAllOutput(object process) { 1107 1127 // CC: change to extension method on Process class 1108 1128 System.Diagnostics.Process proc = (System.Diagnostics.Process)process; -
Tests/100-basics/930-pow-expr.cobra
1 use System.Diagnostics 2 1 3 class ToThePower 2 4 3 def main 5 def main is shared 6 .powExpr 7 .powAugAssign 8 # .time 9 10 def powExpr is shared 4 11 for i, answer in [[2, 100], [3, 1_000], [4, 10_000], [5, 100_000]] 5 12 x = 10 ** i 6 13 assert x inherits int # .warning. The expression is always of type "int" … … 18 25 19 26 f = 1.1f ** 3f # 1.331 20 27 assert f >= 1.331f and f < 1.3311f 28 29 assert 2 ** 0 == 1 30 assert 2 ** 1 == 2 31 32 assert 2f ** 0f == 1f 33 assert 2f ** 1f == 2f 34 35 assert 2f ** 0 == 1f 36 assert 2f ** 1 == 2f 37 assert 2f ** 2 == 4f 38 39 assert 2f ** -2 == 0.25f 40 assert 2 ** -2 == 0 # shld this throw instead ? 41 42 def powAugAssign is shared 43 for i, answer in [[2, 100], [3, 1_000], [4, 10_000], [5, 100_000]] 44 x = 10 45 x **= i 46 assert x == answer 47 48 for j, answerf in [[2f, 9f], [3f, 27f], [4f, 81f], [5f, 243f]] 49 z = 3f 50 z **= j 51 assert z == answerf 52 53 #int raised int 54 y=2 55 y **= 0 56 assert y == 1 57 y **= 1 58 assert y == 1 59 y=2 60 y **= 1 61 assert y == 2 62 63 # float raised float 64 f = 2f 65 f **= 0f 66 assert f == 1f 67 f **= 1f 68 assert f == 1f 69 70 f = 2f 71 f **= 1f 72 assert f == 2f 73 f **= 2f 74 assert f == 4f 75 76 # float raised int 77 f = 2f 78 f **= 0 79 assert f == 1f 80 81 f = 2f 82 f **= 1 83 assert f == 2f 84 f **= 2 85 assert f == 4f 86 87 # {float, int} raised negatives 88 f = 2f 89 f **= -2 90 assert f == 0.25f 91 92 y = 2 93 y **= -2 94 assert y == 0 95 96 def time is shared 97 count= 1_000_000 98 sw = Stopwatch() 99 sw.start 100 for i in count 101 c = 10 ** 3 102 sw.stop 103 t1 = sw.elapsedMilliseconds 104 print t1/count 105 sw.reset 106 107 sw.start 108 for i in count 109 c1 = 10f ** 3f 110 sw.stop 111 t1 = sw.elapsedMilliseconds 112 print t1/count 113 sw.reset 114 115 /# 116 sw.start 117 for i in count 118 c2 = (10 to float) ** (3 to float) 119 sw.stop 120 t1 = sw.elapsedMilliseconds 121 print t1/count 122 #/ 123 CobraCore.noOp(c) 124 CobraCore.noOp(c1)