Ver código fonte

[python] align `-x % -y` with other targets (#8845)

Aleksandr Kuzmenko 6 anos atrás
pai
commit
aaf91d0803

+ 24 - 2
std/python/internal/HxOverrides.hx

@@ -142,12 +142,34 @@ class HxOverrides {
 
 	@:ifFeature("binop_%")
 	static public function modf(a:Float, b:Float) {
-		return Syntax.code("float('nan') if (b == 0.0) else a % b if a >= 0 and b >= 0 else -(-a % b)");
+		if(b == 0.0) {
+			return Syntax.code("float('nan')");
+		} else if(a < 0) {
+			if(b < 0) {
+				return Syntax.code("-(-{0} % (-{1}))", a, b);
+			} else {
+				return Syntax.code("-(-{0} % {1})", a, b);
+			}
+		} else if(b < 0) {
+			return Syntax.code("{0} % (-{1})", a, b);
+		} else {
+			return Syntax.code("{0} % {1}", a, b);
+		}
 	}
 
 	@:ifFeature("binop_%")
 	static public function mod(a:Int, b:Int) {
-		return Syntax.code("a % b if a >= 0 and b >= 0 else -(-a % b)");
+		if(a < 0) {
+			if(b < 0) {
+				return Syntax.code("-(-{0} % (-{1}))", a, b);
+			} else {
+				return Syntax.code("-(-{0} % {1})", a, b);
+			}
+		} else if(b < 0) {
+			return Syntax.code("{0} % (-{1})", a, b);
+		} else {
+			return Syntax.code("{0} % {1}", a, b);
+		}
 	}
 
 	@:ifFeature("dynamic_array_read")

+ 10 - 4
tests/unit/src/unit/issues/Issue8845.hx

@@ -1,12 +1,18 @@
 package unit.issues;
 
 class Issue8845 extends unit.Test {
-	static var a = 255;
-	static var b = -8;
+	static var a:Int;
+	static var b:Int;
 
+	// This is actually unspecified: https://haxe.org/manual/expression-operators-binops.html
+	// but happens to work on all targets
 	function test() {
-		// This is actually unspecified: https://haxe.org/manual/expression-operators-binops.html
-		// but happens to work on all targets
+		a = 255;
+		b = -8;
 		eq(7, a % b);
+
+		a = -100;
+		b = -9;
+		eq(-1, a % b);
 	}
 }