浏览代码

relaxed Std.int/Math.floor/ceil/round specification : only specified in Int32 range, unspecified outside

Nicolas Cannasse 12 年之前
父节点
当前提交
74540b7c09
共有 3 个文件被更改,包括 29 次插入12 次删除
  1. 9 1
      std/Math.hx
  2. 2 2
      std/Std.hx
  3. 18 9
      tests/unit/TestBasetypes.hx

+ 9 - 1
std/Math.hx

@@ -67,6 +67,8 @@ extern class Math
 		If this constant is converted to an Int, e.g. through Std.int(), the
 		result is unspecified.
 		
+		In order to test if a value is NaN, you should use Math.isNaN() function.
+		
 		(Php) In PHP versions prior to 5.3.1 VC 9 there may be unexpected
 		results when performing arithmetic operations with NaN on Windows, see:
 			https://bugs.php.net/bug.php?id=42143
@@ -179,6 +181,8 @@ extern class Math
 	
 	/**
 		Rounds [v] to the nearest Int value.
+
+		If v is outside of the signed Int32 range, or is NaN, NEGATIVE_INFINITY or POSITIVE_INFINITY, the result is unspecified.
 		
 		TODO: need spec
 	**/
@@ -187,12 +191,16 @@ extern class Math
 	/**
 		Returns the largest Int value that is not greater than [v].
 		
+		If v is outside of the signed Int32 range, or is NaN, NEGATIVE_INFINITY or POSITIVE_INFINITY, the result is unspecified.		
+		
 		TODO: need spec
 	**/
 	static function floor(v:Float):Int;
 	
 	/**
 		Returns the smallest Int value that is not less than [v].
+
+		If v is outside of the signed Int32 range, or is NaN, NEGATIVE_INFINITY or POSITIVE_INFINITY, the result is unspecified.
 		
 		TODO: need spec
 	**/
@@ -243,7 +251,7 @@ extern class Math
 		If [f] is NaN, the result is true.
 		
 		Otherwise the result is false. In particular, both POSITIVE_INFINITY and
-		NEGATIVE_INFINITY are not considered invalid numbers.
+		NEGATIVE_INFINITY are not considered NaN.
 	**/
 	static function isNaN( f : Float ) : Bool;
 

+ 2 - 2
std/Std.hx

@@ -54,9 +54,9 @@ extern class Std {
 	public static function string( s : Dynamic ) : String;
 
 	/**
-		Converts a Float to an Int, rounded down.
+		Converts a Float to an Int, rounded towards 0.
 
-		If x is NaN, NEGATIVE_INFINITY or POSITIVE_INFINITY, the result is unspecified.
+		If x is outside of the signed Int32 range, or is NaN, NEGATIVE_INFINITY or POSITIVE_INFINITY, the result is unspecified.
 	**/
 	public static function int( x : Float ) : Int;
 

+ 18 - 9
tests/unit/TestBasetypes.hx

@@ -161,31 +161,40 @@ class TestBasetypes extends Test {
 		eq( Math.round(1.2), 1 );
 
 
+		// we don't specify Std.int and other to-int conversions when outside the Int range
+
+		#if (js || flash)
 		eq( Std.int( -10000000000.7), 0xABF41C00 );
+		eq( Std.int( 10000000000.7), 0x540BE400 );
 		
-		// int/uint limit values
 		eq( Std.int( -4294967296.7), 0 );
+		eq( Std.int( -4294967296.001), 0 );
 		eq( Std.int( 4294967296.7), 0 );
+		eq( Std.int( 4294967296.001), 0 );
 		eq( Std.int( -4294967295.7), 1 );
+		eq( Std.int( -4294967295.001), 1 );
 		eq( Std.int( 4294967295.7), -1 );
+		eq( Std.int( 4294967295.001), -1 );
 
 		eq( Std.int( -2147483648.7), 0x80000000 );
+		eq( Std.int( -2147483648.001), 0x80000000 );
 		eq( Std.int( 2147483648.7), 0x80000000 );
+		eq( Std.int( 2147483648.001), 0x80000000 );
 		eq( Std.int( -2147483647.7), 0x80000001 );
+		eq( Std.int( -2147483647.001), 0x80000001 );
 		eq( Std.int( 2147483647.7), 0x7FFFFFFF );
+		eq( Std.int( 2147483647.001), 0x7FFFFFFF );
 		
-		#if (js || flash8 || as3 || php)
-
-		// 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
 
+		#if flash9
 		eq( Math.floor( -10000000000.7), 0xABF41BFF);
 		eq( Math.ceil( -10000000000.7), 0xABF41C00);
 		eq( Math.round( -10000000000.7), 0xABF41BFF);
+		#else
+		eq( Math.floor( -10000000000.7)*1.0, -10000000001. );
+		eq( Math.ceil( -10000000000.7)*1.0, -10000000000. );
+		eq( Math.round( -10000000000.7)*1.0, -10000000001. );
+		#end
 
 		#end