Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Explanation pls
Message
From
07/03/2011 02:03:00
 
 
To
06/03/2011 17:26:32
General information
Forum:
ASP.NET
Category:
Other
Title:
Miscellaneous
Thread ID:
01502561
Message ID:
01502805
Views:
57
>>>
>>>So... the ToString() value actually does exceed the max.
>>>
>>>That was fun.
>>
>>No it wasn't ! I had to buy a bigger monitor :-}
>
>LOL it was a long number.
>
>in finishing off that large number lib, I ran into this:
>
>C#
> ? -1000 % 3 = -1
>
>VFP
> ? -1000 % 3 = 2
>
>Can anyone explain that one?

Yes - see http://en.wikipedia.org/wiki/Modulo_operation

vfp : the modulo result has the same sign as the divisor( here 3)
C# the modulo result has the same sign as the dividend( here -1000)


I've made myself a couple of extension methods to handle that

If you want C# to return the same as VFP, use Mod extension method
	public static partial class ExtensionMethods_Arithmetic
	{
		// http://en.wikipedia.org/wiki/Modulo_operation

		//______________________________________________________________________
		/// <summary>
		/// Performs a Remainder calculation where the result has the sign of dividend
		/// </summary>
		/// <param name="dividend"></param>
		/// <param name="divisor"></param>
		/// <returns></returns>
		public static Int32 Rem(this Int32 dividend, Int32 divisor)
		{
			return (dividend % divisor);
		}
		//______________________________________________________________________
		/// <summary>
		/// Performs a Remainder calculation where the result has the sign of dividend
		/// </summary>
		/// <param name="dividend"></param>
		/// <param name="divisor"></param>
		/// <returns></returns>
		public static Int64 Rem(this Int64 dividend, Int64 divisor)
		{
			return (dividend % divisor);
		}
		//______________________________________________________________________
	}
and
	public static partial class ExtensionMethods_Arithmetic
	{
		// http://en.wikipedia.org/wiki/Modulo_operation

		//______________________________________________________________________
		/// <summary>
		/// Performs a Modulo calculation where the result has the sign of divisor
		/// </summary>
		/// <param name="dividend"></param>
		/// <param name="divisor"></param>
		/// <returns></returns>
		public static Int32 Mod(this Int32 dividend, Int32 divisor)
		{
			return dividend.Rem(divisor)
					+ (Math.Sign(dividend) * Math.Sign(divisor) >= 0 ? 0 : divisor);
	
		}
		//______________________________________________________________________
		/// <summary>
		/// Performs a Modulo calculation where the result has the sign of divisor
		/// </summary>
		/// <param name="dividend"></param>
		/// <param name="divisor"></param>
		/// <returns></returns>
		public static Int64 Mod(this Int64 dividend, Int64 divisor)
		{
			return dividend.Rem(divisor)
					+ (Math.Sign(dividend) * Math.Sign(divisor) >= 0 ? 0 : divisor);
		}
		//______________________________________________________________________
	}
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform