Bug 30165 - Math.Floor does not always return a correct value
Summary: Math.Floor does not always return a correct value
Status: NEW
Alias: None
Product: Runtime
Classification: Mono
Component: General (show other bugs)
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-05-16 13:00 UTC by manuel
Modified: 2015-05-18 11:59 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report for Bug 30165 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
NEW

Description manuel 2015-05-16 13:00:16 UTC
In the following code Math.Floor once in a while (with very low probability) produces an incorrect value ending up with an out of range exception while accessing an array. We did correct this issue by replacing it by a cast to byte.
If first though it was an indirect issue caused by using pointers directly, but it seems it is not. This issue happens in both iOS and Android
 
		public static Color GetMostFrequentColorInRGB(RGBA_Bitmap bitmap, RGBA_Bitmap hsvMask){
			int countBytes = bitmap.Width * bitmap.Height;
			int classes = 20;
			float dx = 256 / (float)classes;

			var histogram = new uint[classes, classes, classes];


			PixelData* pBase = bitmap.PixelAt (0, 0);
			HSV* pBaseMask = (HSV*)hsvMask.PixelAt (0, 0);

			for (int i = 0; i < countBytes; i++) {


				if (pBaseMask->s > 50 && pBaseMask->v > 100) {

					int r = (int)Math.Floor(pBase->red / dx);
					int g = (int)Math.Floor(pBase->green / dx);
					int b = (int)Math.Floor(pBase->blue / dx);

					histogram [r, g, b]++;
				}

				pBase++;
				pBaseMask++;
			}

			GC.KeepAlive(bitmap);  
			GC.KeepAlive(hsvMask);  

			return MostFrequent (histogram, classes);

		}

		private static int MostFrequent(int[] histogram){
			int mostFrequentCount = histogram[0];
			int mostFrequent = 0;

			for (int i = 1; i < histogram.Length; i++) {
				if (mostFrequentCount < histogram [i]) {
					mostFrequentCount = histogram [i];
					mostFrequent = i;
				}
			}

			return mostFrequent;
		}



Solution was

		public static Color GetMostFrequentColorInRGB(RGBA_Bitmap bitmap, RGBA_Bitmap hsvMask){
			int countBytes = bitmap.Width * bitmap.Height;
			int classes = 20;
			float dx = 256 / (float)classes;

			var histogram = new uint[classes, classes, classes];


			PixelData* pBase = bitmap.PixelAt (0, 0);
			HSV* pBaseMask = (HSV*)hsvMask.PixelAt (0, 0);

			for (int i = 0; i < countBytes; i++) {


				if (pBaseMask->s > 50 && pBaseMask->v > 100) {

					int r = (int)((byte)pBase->red / dx);
					int g = (int)((byte)pBase->green / dx);
 					int b = (int)((byte)pBase->blue / dx);

					histogram [r, g, b]++;
				}

				pBase++;
				pBaseMask++;
			}

			GC.KeepAlive(bitmap);  
			GC.KeepAlive(hsvMask);  

			return MostFrequent (histogram, classes);

		}
Comment 1 manuel 2015-05-18 11:59:35 UTC
sorry, what solved was direct cast to int instead of using Floor

Just for reference 

	public struct PixelData
	{
		public byte red;
		public byte green;
		public byte blue;
		public byte a;

	}


	public struct HSV
	{
		public byte h;
		public byte s;
		public byte v;
		public byte a;

	}

and MostFrequent looks like

		private static Color MostFrequent(uint[,,] histogram, int classes){
			int mostFreqR = 0;
			int mostFreqG = 0;
			int mostFreqB = 0;

			uint mostFrequent = 0;


			for (int r = 0; r < classes; r++) 
				for(int g = 0; g<classes; g++)
					for(int b = 0; b<classes; b++){
						if (mostFrequent < histogram [r, g, b]) {
							mostFrequent = histogram [r, g, b];
							mostFreqR = r;
							mostFreqG = g;
							mostFreqB = b;

						}

			}

			int dx = (int)(256 / (double)classes);

			return Color.FromArgb((int)((mostFreqR) * dx), (int)((mostFreqG ) * dx), (int)((mostFreqB) * dx));
		}