Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Explanation pls
Message
From
07/03/2011 04:50:26
 
 
General information
Forum:
ASP.NET
Category:
Other
Title:
Miscellaneous
Thread ID:
01502561
Message ID:
01502813
Views:
40
>>>>>
>>>>>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)
>
>Not much of a consenus between languages on that link ;-}
>Also hadn't occurred to me that, as per link, this wouldn't work in C#:
bool isOdd = -3 % 2 == 1;
Yeah, I struggled with that some time ago
In vfp, I had a Range object which has a Start and an End property and also a CurrentPosition in the range
You can add to and subtract from the CurrentPosition, but the new Position must always be within Start an End

if Start = -20 and End = -10
and CurrentPosition is say -15

If I add 7, then per modulo calculation, the new CurrentPosition must be -19

this works in vfp
	Start  = -20
	End  = -10
	
	CurrentPosition = -15
	
	? Start + (CurrentPosition + 7 - Start) % (End - Start + 1)  && 19
But in C#, you have to use the Mod extension method if you want that to work
/// <summary>
		/// adds a value to the currentValue in a range.  Values outside the range are mapped into the range with modulo
		/// </summary>
		/// <param name="currentValue"></param>
		/// <param name="valueToAdd"></param>
		/// <param name="rangeStart"></param>
		/// <param name="rangeEnd"></param>
		/// <returns></returns>
		public static int Add(int currentValue, int valueToAdd, int rangeStart, int rangeEnd)
		{
			return MapValueInRange(currentValue + valueToAdd, rangeStart, rangeEnd);

		}
		//______________________________________________________________________
		private static int MapValueInRange(int currentValue, int rangeStart, int rangeEnd)
		{
			// (1) convert to base zero by subtracting rangeStart
			// (2) take the mod()
			// (3) add rangeStart
			return (currentValue - rangeStart).Mod(rangeEnd - rangeStart + 1) + rangeStart;

		}
Gregory
Previous
Reply
Map
View

Click here to load this message in the networking platform