You are currently viewing the gmlscripts.pro static mirror. Forum access and script submissions are not available through this mirror.

Invert gmlscripts.pro

encode_real_double

encode_real_double(n)
Returns a string of raw bytes representing the given number encoded in IEEE 754 double precision format.
COPY
  1. /// encode_real_double(n)
  2. //
  3. // Returns a string of raw bytes representing the given
  4. // number encoded in IEEE 754 double precision format.
  5. //
  6. // n number, real
  7. //
  8. /// gmlscripts.pro/license
  9. {
  10. var n, str, c, byte, E, M;
  11. n = argument0;
  12. if (n == 0) {
  13. return string_repeat(chr(0),8);
  14. }
  15. byte[0] = 0;
  16. byte[7] = 0;
  17. if (n < 0) {
  18. n *= -1;
  19. byte[7] = byte[7] | $80;
  20. }
  21. E = floor(log2(n));
  22. M = n / power(2,E) - 1;
  23. E += 1023;
  24. var i;
  25. i = 0;
  26. while (i < 11) {
  27. if (i < 4) {
  28. byte[6] = byte[6] | ((E & (1<<i)) << 4);
  29. } else {
  30. byte[7] = byte[7] | ((E & (1<<i)) >> 4);
  31. }
  32. i += 1;
  33. }
  34. i = 51;
  35. while (i >= 0) {
  36. M *= 2;
  37. if (M >= 1) {
  38. byte[i div 8] = byte[i div 8] | (1<<(i mod 8));
  39. M -= 1;
  40. }
  41. i -= 1;
  42. }
  43. str = "";
  44. for (i = 7; i >= 0; i -= 1) {
  45. str += chr(byte[i]);
  46. }
  47. return str;
  48. }

Contributors: Yourself

GitHub: View · Commits · Blame · Raw