From 0106f967161b2fa0dbf131a56e9c5498d1b16270 Mon Sep 17 00:00:00 2001 From: Charles Krinke Date: Wed, 16 Jul 2008 14:30:22 +0000 Subject: [PATCH] Mantis#1755. Thank you kindly, Matth for a patch that solves: When using math operators +,-,*,/ in an LSL script with an LSLFloat and an integer literal the wrong result is returned. This patch adds operators to the LSLFloat type to handle this case. --- .../Region/ScriptEngine/Common/LSL_Types.cs | 71 ++++++++++++++----- .../Region/ScriptEngine/Shared/LSL_Types.cs | 68 ++++++++++++++---- .../Common/LSL_TypesTestLSLFloat.cs | 4 +- 3 files changed, 109 insertions(+), 34 deletions(-) diff --git a/OpenSim/Region/ScriptEngine/Common/LSL_Types.cs b/OpenSim/Region/ScriptEngine/Common/LSL_Types.cs index 3dfa7116d4..c630c27ce9 100644 --- a/OpenSim/Region/ScriptEngine/Common/LSL_Types.cs +++ b/OpenSim/Region/ScriptEngine/Common/LSL_Types.cs @@ -1276,11 +1276,11 @@ namespace OpenSim.Region.ScriptEngine.Common return new LSLInteger(int.Parse(s)); } - static public implicit operator LSLInteger(double d) + static public implicit operator LSLInteger(uint u) { - return new LSLInteger(d); + return new LSLInteger(u); } - + static public bool operator ==(LSLInteger i1, LSLInteger i2) { bool ret = i1.value == i2.value; @@ -1312,6 +1312,31 @@ namespace OpenSim.Region.ScriptEngine.Common { return new LSLInteger(i1.value / i2); } + + static public LSLFloat operator +(LSLInteger i1, double f) + { + return new LSLFloat((double)i1.value + f); + } + + static public LSLFloat operator -(LSLInteger i1, double f) + { + return new LSLFloat((double)i1.value - f); + } + + static public LSLFloat operator *(LSLInteger i1, double f) + { + return new LSLFloat((double)i1.value * f); + } + + static public LSLFloat operator /(LSLInteger i1, double f) + { + return new LSLFloat((double)i1.value / f); + } + + static public LSLInteger operator -(LSLInteger i) + { + return new LSLInteger(-i.value); + } public override bool Equals(Object o) { @@ -1392,16 +1417,6 @@ namespace OpenSim.Region.ScriptEngine.Common #region Operators - static public implicit operator int(LSLFloat f) - { - return (int)f.value; - } - - static public implicit operator uint(LSLFloat f) - { - return (uint) Math.Abs(f.value); - } - static public implicit operator Boolean(LSLFloat f) { if (f.value == 0.0) @@ -1450,17 +1465,37 @@ namespace OpenSim.Region.ScriptEngine.Common f.value--; return f; } + + static public LSLFloat operator +(LSLFloat f, int i) + { + return new LSLFloat(f.value + (double)i); + } + + static public LSLFloat operator -(LSLFloat f, int i) + { + return new LSLFloat(f.value - (double)i); + } + + static public LSLFloat operator *(LSLFloat f, int i) + { + return new LSLFloat(f.value * (double)i); + } + + static public LSLFloat operator /(LSLFloat f, int i) + { + return new LSLFloat(f.value / (double)i); + } + + static public LSLFloat operator -(LSLFloat f) + { + return new LSLFloat(-f.value); + } static public implicit operator System.Double(LSLFloat f) { return f.value; } - //static public implicit operator System.Int32(LSLFloat f) - //{ - // return (int)f.value; - //} - #endregion #region Overriders diff --git a/OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs b/OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs index 7ababb4659..f71d5840ab 100644 --- a/OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs +++ b/OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs @@ -1303,10 +1303,10 @@ namespace OpenSim.Region.ScriptEngine.Shared { return new LSLInteger(int.Parse(s)); } - - static public implicit operator LSLInteger(double d) + + static public implicit operator LSLInteger(uint u) { - return new LSLInteger(d); + return new LSLInteger(u); } static public bool operator ==(LSLInteger i1, LSLInteger i2) @@ -1341,9 +1341,34 @@ namespace OpenSim.Region.ScriptEngine.Shared return new LSLInteger(i1.value / i2); } + static public LSLFloat operator +(LSLInteger i1, double f) + { + return new LSLFloat((double)i1.value + f); + } + + static public LSLFloat operator -(LSLInteger i1, double f) + { + return new LSLFloat((double)i1.value - f); + } + + static public LSLFloat operator *(LSLInteger i1, double f) + { + return new LSLFloat((double)i1.value * f); + } + + static public LSLFloat operator /(LSLInteger i1, double f) + { + return new LSLFloat((double)i1.value / f); + } + + static public LSLInteger operator -(LSLInteger i) + { + return new LSLInteger(-i.value); + } + static public LSLInteger operator &(LSLInteger i1, LSLInteger i2) { - int ret = i1.value & i2.value; + int ret = i1.value & i2.value; return ret; } @@ -1447,16 +1472,6 @@ namespace OpenSim.Region.ScriptEngine.Shared #region Operators - static public implicit operator int(LSLFloat f) - { - return (int)f.value; - } - - static public implicit operator uint(LSLFloat f) - { - return (uint) Math.Abs(f.value); - } - static public implicit operator Boolean(LSLFloat f) { if (f.value == 0.0) @@ -1518,7 +1533,32 @@ namespace OpenSim.Region.ScriptEngine.Shared f.value--; return f; } + + static public LSLFloat operator +(LSLFloat f, int i) + { + return new LSLFloat(f.value + (double)i); + } + + static public LSLFloat operator -(LSLFloat f, int i) + { + return new LSLFloat(f.value - (double)i); + } + + static public LSLFloat operator *(LSLFloat f, int i) + { + return new LSLFloat(f.value * (double)i); + } + + static public LSLFloat operator /(LSLFloat f, int i) + { + return new LSLFloat(f.value / (double)i); + } + static public LSLFloat operator -(LSLFloat f) + { + return new LSLFloat(-f.value); + } + static public implicit operator System.Double(LSLFloat f) { return f.value; diff --git a/OpenSim/Tests/OpenSim/Region/ScriptEngine/Common/LSL_TypesTestLSLFloat.cs b/OpenSim/Tests/OpenSim/Region/ScriptEngine/Common/LSL_TypesTestLSLFloat.cs index 29d0e1c0f1..130c0ecaa6 100644 --- a/OpenSim/Tests/OpenSim/Region/ScriptEngine/Common/LSL_TypesTestLSLFloat.cs +++ b/OpenSim/Tests/OpenSim/Region/ScriptEngine/Common/LSL_TypesTestLSLFloat.cs @@ -133,7 +133,7 @@ namespace OpenSim.Region.ScriptEngine.Common.Tests foreach (KeyValuePair number in numberSet) { - testNumber = new LSL_Types.LSLFloat(number.Key); + testNumber = (int)(new LSL_Types.LSLFloat(number.Key)); Assert.AreEqual(number.Value, testNumber, "Converting double " + number.Key + ", expecting int " + number.Value); } } @@ -170,7 +170,7 @@ namespace OpenSim.Region.ScriptEngine.Common.Tests foreach (KeyValuePair number in numberSet) { - testNumber = new LSL_Types.LSLFloat(number.Key); + testNumber = (uint)(new LSL_Types.LSLFloat(number.Key)); Assert.AreEqual(number.Value, testNumber, "Converting double " + number.Key + ", expecting uint " + number.Value); } }