Browse Source

added Math.ffloor/fceil/fround

Nicolas Cannasse 13 năm trước cách đây
mục cha
commit
0db2764170
6 tập tin đã thay đổi với 58 bổ sung22 xóa
  1. 2 0
      genswf9.ml
  2. 23 0
      std/Math.hx
  3. 8 1
      std/neko/_std/Math.hx
  4. 4 0
      std/php/_std/Math.hx
  5. 20 20
      tests/unit/TestBasetypes.hx
  6. 1 1
      tests/unit/compile.hxml

+ 2 - 0
genswf9.ml

@@ -325,6 +325,8 @@ let property ctx p t =
 		| Statics { cl_path = [], "Math" } ->
 			(match p with
 			| "POSITIVE_INFINITY" | "NEGATIVE_INFINITY" | "NaN" -> ident p, Some KFloat, false
+			| "floor" | "ceil" | "round" -> ident p, Some KInt, false
+			| "ffloor" | "fceil" | "fround" -> ident (String.sub p 1 (String.length p - 1)), None, false
 			| _ -> ident p, None, false)
 		| _ -> ident p, None, false)
 	| TInst ({ cl_kind = KExtension _ } as c,params) ->

+ 23 - 0
std/Math.hx

@@ -48,6 +48,29 @@ extern class Math
 	static function acos(v:Float):Float;
 	static function pow(v:Float,exp:Float):Float;
 	static function random() : Float;
+	
+	#if flash9
+	
+	static function ffloor( v : Float ) : Float;
+	static function fceil( v : Float ) : Float;
+	static function fround( v : Float ) : Float;
+	
+	#else
+	
+	static inline function ffloor( v : Float ) : Float {
+		return floor(v);
+	}
+
+	static inline function fceil( v : Float ) : Float {
+		return ceil(v);
+	}
+
+	static inline function fround( v : Float ) : Float {
+		return round(v);
+	}
+	
+	#end
+	
 
 	static function isFinite( f : Float ) : Bool;
 	static function isNaN( f : Float ) : Bool;

+ 8 - 1
std/neko/_std/Math.hx

@@ -47,6 +47,10 @@ import neko.Lib;
 	public static function acos( v : Float ) : Float return 0.
 	public static function pow( v : Float, exp : Float ) : Float return 0.
 
+	public static function fround( v : Float ) : Float return 0.
+	public static function ffloor( v : Float ) : Float return 0.
+	public static function fceil( v : Float ) : Float return 0.
+
 	static var __rnd;
 	static var _rand_float = Lib.load("std","random_float",1);
 	static var _rand_int = Lib.load("std","random_int",2);
@@ -77,7 +81,10 @@ import neko.Lib;
 		M.atan = Lib.load("std","math_atan",1);
 		M.asin = Lib.load("std","math_asin",1);
 		M.acos = Lib.load("std","math_acos",1);
-		M.pow = Lib.load("std","math_pow",2);
+		M.pow = Lib.load("std", "math_pow", 2);
+		M.fceil = try Lib.load("std", "math_fceil", 1) catch( e : Dynamic ) M.ceil;
+		M.ffloor = try Lib.load("std", "math_ffloor", 1) catch( e : Dynamic ) M.floor;
+		M.fround = try Lib.load("std", "math_fround", 1) catch( e : Dynamic ) M.round;
 	}
 
 }

+ 4 - 0
std/php/_std/Math.hx

@@ -46,6 +46,10 @@
 	public static function random() : Float    { return untyped __call__("mt_rand") / __call__("mt_getrandmax"); }
 	public static function isNaN(f : Float) : Bool     { return untyped __call__("is_nan", f); }
 	public static function isFinite(f : Float) : Bool  { return untyped __call__("is_finite", f); }
+	
+	public static function fround(v : Float) : Float      { return untyped __call__("floor", v + 0.5); }
+	public static function ffloor(v : Float) : Float      { return untyped __call__("floor", v); }
+	public static function fceil(v : Float) : Float       { return untyped __call__("ceil", v); }
 
 	static function __init__() : Void {
 	 	PI = untyped __php__("M_PI");

+ 20 - 20
tests/unit/TestBasetypes.hx

@@ -163,28 +163,28 @@ class TestBasetypes extends Test {
 		eq( Math.round(1.5), 2 );
 		eq( Math.round(1.2), 1 );
 
-		// overflows might occurs depending on the platform
-		unspec(function() Std.int(-10000000000.7));
-		unspec( function() Math.floor(-10000000000.7) );
-		unspec( function() Math.ceil(-10000000000.7) );
-		unspec( function() Math.round(-10000000000.7) );
-		// should still give a proper result for lower bits
-		#if java
-		eq( Std.int(-10000000000.7) & 0xFFFFFF, 0 );
-		eq( Math.floor(-10000000000.7) & 0xFFFFFF, 0 );
-		eq( Math.ceil( -10000000000.7) & 0xFFFFFF, 0 );
-		eq( Math.round(-10000000000.7) & 0xFFFFFF, 15997951 );
-		#elseif cs
-		eq( Std.int(-10000000000.7) & 0xFFFFFF, 15997952 );
-		eq( Math.floor(-10000000000.7) & 0xFFFFFF, 0 );
-		eq( Math.ceil( -10000000000.7) & 0xFFFFFF, 0 );
-		eq( Math.round(-10000000000.7) & 0xFFFFFF, 0 );
+		
+		eq( Std.int( -10000000000.7), 0xABF41C00 );
+		
+		#if (js || flash8)
+		
+		// higher Int resolution : should we fix this or not ?
+		eq( Math.floor( -10000000000.7)*1.0, -10000000001. );
+		eq( Math.ceil( -10000000000.7)*1.0, -10000000000. );
+		eq( Math.round( -10000000000.7)*1.0, -10000000001. );
+		
 		#else
-		eq( Std.int(-10000000000.7) & 0xFFFFFF, 15997952 );
-		eq( Math.floor(-10000000000.7) & 0xFFFFFF, 15997951 );
-		eq( Math.ceil( -10000000000.7) & 0xFFFFFF, 15997952 );
-		eq( Math.round(-10000000000.7) & 0xFFFFFF, 15997951 );
+		
+		eq( Math.floor( -10000000000.7), 0xABF41BFF);
+		eq( Math.ceil( -10000000000.7), 0xABF41C00);
+		eq( Math.round( -10000000000.7), 0xABF41BFF);
+	
 		#end
+
+		eq( Math.ffloor( -10000000000.7), -10000000001. );
+		eq( Math.fceil( -10000000000.7), -10000000000. );
+		eq( Math.fround( -10000000000.7), -10000000001. );
+
 	}
 
 	function testParse() {

+ 1 - 1
tests/unit/compile.hxml

@@ -61,7 +61,7 @@ unit.Test
 --next
 -main unit.Test
 -as3 as3
--cmd mxmlc -static-link-runtime-shared-libraries=true -debug as3/__main__.as --output unit9_as3.swf
+#-cmd mxmlc -static-link-runtime-shared-libraries=true -debug as3/__main__.as --output unit9_as3.swf
 
 #cpp
 --next