fork(1) download
  1. using System;
  2. using System.Runtime.InteropServices;
  3.  
  4. [StructLayout(LayoutKind.Explicit)]
  5. struct Float64
  6. {
  7. public Float64(double x) { ULong = 0; Double = x; }
  8. public static implicit operator Float64(double x) { return new Float64(x); }
  9. public static implicit operator double(Float64 x) { return x.Double; }
  10.  
  11. [FieldOffset(0)] public double Double;
  12. [FieldOffset(0)] public ulong ULong;
  13.  
  14. public ulong Mantissa
  15. {
  16. get { return ULong & 0xFFFFFFFFFFFFF; }
  17. set { ULong = ULong & ~0xFFFFFFFFFFFFFULL | value & 0xFFFFFFFFFFFFFULL; }
  18. }
  19.  
  20. public uint Exp
  21. {
  22. get { return (uint)((ULong >> 52) & 0x7FF); }
  23. set { ULong = ULong & 0x800FFFFFFFFFFFFFULL | ((ulong)(value & 0x7FF) << 52); }
  24. }
  25.  
  26. public uint Sign
  27. {
  28. get { return (uint)(ULong >> 63); }
  29. set { ULong = ULong & 0x7FFFFFFFFFFFFFFFULL | ((ulong)value << 63); }
  30. }
  31. }
  32.  
  33. public class Test
  34. {
  35. private static void Print(Float64 x)
  36. {
  37. Console.WriteLine("{0,7} {1:X16} s={4} m={2:X13} e={3:X3}", x.Double, x.ULong, x.Mantissa, x.Exp, x.Sign);
  38. }
  39.  
  40. public static void Main()
  41. {
  42. foreach (var d in new double [] { 0,1,-1,2,-2,3,-3,3.25,-3.25 })
  43. Print(d);
  44.  
  45. Float64 x = 1;
  46. Print(x);
  47. ++x.Exp; Print(x); // 2
  48. x.Sign = 1; Print(x); // -2
  49. x.Mantissa += 1ULL << 51; Print(x); // -3
  50. --x.Exp; Print(x); // -1.5
  51. }
  52. }
Success #stdin #stdout 0s 131520KB
stdin
Standard input is empty
stdout
      0 0000000000000000 s=0 m=0000000000000 e=000
      1 3FF0000000000000 s=0 m=0000000000000 e=3FF
     -1 BFF0000000000000 s=1 m=0000000000000 e=3FF
      2 4000000000000000 s=0 m=0000000000000 e=400
     -2 C000000000000000 s=1 m=0000000000000 e=400
      3 4008000000000000 s=0 m=8000000000000 e=400
     -3 C008000000000000 s=1 m=8000000000000 e=400
   3.25 400A000000000000 s=0 m=A000000000000 e=400
  -3.25 C00A000000000000 s=1 m=A000000000000 e=400
      1 3FF0000000000000 s=0 m=0000000000000 e=3FF
      2 4000000000000000 s=0 m=0000000000000 e=400
     -2 C000000000000000 s=1 m=0000000000000 e=400
     -3 C008000000000000 s=1 m=8000000000000 e=400
   -1.5 BFF8000000000000 s=1 m=8000000000000 e=3FF