Browse Source

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

Nicolas Cannasse 12 years ago
parent
commit
74540b7c09
3 changed files with 29 additions and 12 deletions
  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
 		If this constant is converted to an Int, e.g. through Std.int(), the
 		result is unspecified.
 		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
 		(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:
 		results when performing arithmetic operations with NaN on Windows, see:
 			https://bugs.php.net/bug.php?id=42143
 			https://bugs.php.net/bug.php?id=42143
@@ -179,6 +181,8 @@ extern class Math
 	
 	
 	/**
 	/**
 		Rounds [v] to the nearest Int value.
 		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
 		TODO: need spec
 	**/
 	**/
@@ -187,12 +191,16 @@ extern class Math
 	/**
 	/**
 		Returns the largest Int value that is not greater than [v].
 		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
 		TODO: need spec
 	**/
 	**/
 	static function floor(v:Float):Int;
 	static function floor(v:Float):Int;
 	
 	
 	/**
 	/**
 		Returns the smallest Int value that is not less than [v].
 		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
 		TODO: need spec
 	**/
 	**/
@@ -243,7 +251,7 @@ extern class Math
 		If [f] is NaN, the result is true.
 		If [f] is NaN, the result is true.
 		
 		
 		Otherwise the result is false. In particular, both POSITIVE_INFINITY and
 		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;
 	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;
 	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;
 	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 );
 		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), 0xABF41C00 );
+		eq( Std.int( 10000000000.7), 0x540BE400 );
 		
 		
-		// int/uint limit values
 		eq( Std.int( -4294967296.7), 0 );
 		eq( Std.int( -4294967296.7), 0 );
+		eq( Std.int( -4294967296.001), 0 );
 		eq( Std.int( 4294967296.7), 0 );
 		eq( Std.int( 4294967296.7), 0 );
+		eq( Std.int( 4294967296.001), 0 );
 		eq( Std.int( -4294967295.7), 1 );
 		eq( Std.int( -4294967295.7), 1 );
+		eq( Std.int( -4294967295.001), 1 );
 		eq( Std.int( 4294967295.7), -1 );
 		eq( Std.int( 4294967295.7), -1 );
+		eq( Std.int( 4294967295.001), -1 );
 
 
 		eq( Std.int( -2147483648.7), 0x80000000 );
 		eq( Std.int( -2147483648.7), 0x80000000 );
+		eq( Std.int( -2147483648.001), 0x80000000 );
 		eq( Std.int( 2147483648.7), 0x80000000 );
 		eq( Std.int( 2147483648.7), 0x80000000 );
+		eq( Std.int( 2147483648.001), 0x80000000 );
 		eq( Std.int( -2147483647.7), 0x80000001 );
 		eq( Std.int( -2147483647.7), 0x80000001 );
+		eq( Std.int( -2147483647.001), 0x80000001 );
 		eq( Std.int( 2147483647.7), 0x7FFFFFFF );
 		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.floor( -10000000000.7), 0xABF41BFF);
 		eq( Math.ceil( -10000000000.7), 0xABF41C00);
 		eq( Math.ceil( -10000000000.7), 0xABF41C00);
 		eq( Math.round( -10000000000.7), 0xABF41BFF);
 		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
 		#end