From ef2c087c1b5f157e006680422011860653f7f763 Mon Sep 17 00:00:00 2001 From: Ian Farmer Date: Mon, 2 Jun 2008 04:18:42 +0000 Subject: [PATCH] Number fixes. (1) Change how LDouble computes hashCode to match standard Lua. (2) Change how toJavaString deals with NaN, infinity, and -0. (3) Move 0 * -2 case to end of mathlib test case so that problematic tests are towards the end. --- src/core/org/luaj/vm/LDouble.java | 17 +++++++++++++---- src/test/res/mathlib.lua | 4 ++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/core/org/luaj/vm/LDouble.java b/src/core/org/luaj/vm/LDouble.java index e3474633..bc2de543 100644 --- a/src/core/org/luaj/vm/LDouble.java +++ b/src/core/org/luaj/vm/LDouble.java @@ -35,14 +35,23 @@ public class LDouble extends LNumber { } public int hashCode() { - return (int) m_value; + if ( m_value == 0 ) { + return 0; + } else { + long bits = Double.doubleToLongBits( m_value ); + return ( (int) bits >> 32 ) + ( (int) bits ); + } } - + public String toJavaString() { if ( Double.isNaN(m_value) ) - return "-1.#IND"; + return "nan"; if ( Double.isInfinite(m_value) ) - return (m_value>0? "1.#INF": "-1.#INF"); + return (m_value>0? "inf": "-inf"); + if ( m_value == 0.0 ) { + long bits = Double.doubleToLongBits( m_value ); + return ( bits >> 63 == 0 ) ? "0" : "-0"; + } long l = (long) m_value; if ( (m_value == (double) l) && (m_value <= Long.MAX_VALUE) && (m_value >= Long.MIN_VALUE) ) { return Long.toString( l ); diff --git a/src/test/res/mathlib.lua b/src/test/res/mathlib.lua index b3989378..2a38da17 100644 --- a/src/test/res/mathlib.lua +++ b/src/test/res/mathlib.lua @@ -17,7 +17,6 @@ function binops( a, b ) return '--' end print( pcall( binops, 2, 0 ) ) -print( pcall( binops, -2, 0 ) ) print( pcall( binops, 2.5, 0 ) ) print( pcall( binops, -2.5, 0 ) ) print( pcall( binops, 2, 1 ) ) @@ -41,9 +40,10 @@ print( pcall( binops, 2.25, 0 ) ) print( pcall( binops, 2.25, 2 ) ) print( pcall( binops, 2.25, .5 ) ) print( pcall( binops, 2.25, 2.5 ) ) - +print( pcall( binops, -2, 0 ) ) -- random tests +print("Random tests") print( math.random(5,10) ) print( math.random(5,10) ) print( math.random(5,10) )