Advertisement
Guest User

Corruption of float's in Xamarin.Android 4.7.4

a guest
Apr 27th, 2013
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. /// <summary>
  2. /// This code illustrates a problem when executing Queue<float>.Dequeue() when run on the
  3. /// default MonoForAndroid API-10 simulator configured by Xamarin Studio.
  4. ///
  5. /// The value dequeue should be 1.0f, but upon inspection it is 0.
  6. ///
  7. /// This occurs on my Android device as well (a brand new, but super cheap androd 2.3 phone)
  8. /// </summary>
  9. using System;
  10.  
  11. using Android.App;
  12. using Android.Content;
  13. using Android.Runtime;
  14. using Android.Views;
  15. using Android.Widget;
  16. using Android.OS;
  17.  
  18. using System.Diagnostics;
  19. using System.Collections;
  20. using System.Collections.Generic;
  21. using System.Runtime.Remoting.Metadata.W3cXsd2001;
  22.  
  23. namespace FloatCorruption
  24. {
  25. public class CorruptedFloatException : Exception
  26. {
  27. }
  28. [Activity (Label = "FloatCorruption", MainLauncher = true)]
  29. public class Activity1 : Activity
  30. {
  31. Queue<float> _EvilFloat = new Queue<float>();
  32. Queue _NonGeneric = new Queue();
  33. Queue<double> _GenericDouble = new Queue<double>();
  34.  
  35. protected override void OnCreate (Bundle bundle)
  36. {
  37. base.OnCreate (bundle);
  38.  
  39. // Set our view from the "main" layout resource
  40. SetContentView (new View(this));
  41.  
  42. // ok with non generic
  43. _NonGeneric.Enqueue(1f);
  44. if((float)_NonGeneric.Dequeue() != 1.0f)
  45. throw new CorruptedFloatException();
  46.  
  47. _GenericDouble.Enqueue(1f);
  48. if(_GenericDouble.Dequeue() != 1.0)
  49. throw new CorruptedFloatException();
  50.  
  51. // broken with float
  52. _EvilFloat.Enqueue(1f);
  53. var result = _EvilFloat.Dequeue();
  54. var result_raw = new SoapHexBinary(BitConverter.GetBytes(result)).ToString();
  55. Console.WriteLine("result = {0}, as hex = {1}", result, result_raw);
  56. if(result != 1.0f)
  57. throw new CorruptedFloatException();
  58. }
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement