Advertisement
kenan238

infinite gml float precision glitch (at the cost of performance)

Jun 16th, 2024 (edited)
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function BigFloat (nbr, decimal) constructor
  2. {
  3.     self.integer = ""
  4.     self.decimal = ""
  5.    
  6.     if typeof(nbr) == "number" && is_undefined(decimal)
  7.     {
  8.         var int = floor(nbr)
  9.         self.decimal = string(nbr - int)
  10.         self.integer = string(int)
  11.     }
  12.     else
  13.     {
  14.         self.integer = string(nbr)
  15.        
  16.         if !is_undefined(decimal)
  17.             self.decimal = string(decimal)
  18.     }
  19.    
  20.     static AddStrings = function (a, b, carry = 0)
  21.     {
  22.         var result = "";
  23.        
  24.         while string_length(a) < string_length(b)
  25.             a = "0" + a
  26.         while string_length(b) < string_length(a)
  27.             b = "0" + b
  28.        
  29.         // add from right to left
  30.         for (var i = string_length(a); i > 0; i--)
  31.         {
  32.             var d1 = real(string_char_at(a, i))
  33.             var d2 = real(string_char_at(b, i))
  34.             var sum = d1 + d2 + carry;
  35.             carry = floor(sum / 10);
  36.             result = string(sum % 10) + result;
  37.         }
  38.        
  39.         return
  40.         {
  41.             result,
  42.             carry
  43.         }
  44.     }
  45.    
  46.     static Add = function (b)
  47.     {
  48.         if typeof(b) == "number"
  49.             b = new BigFloat(b)
  50.            
  51.         var d1 = self.decimal;
  52.         var d2 = b.decimal;
  53.        
  54.         var i1 = self.integer;
  55.         var i2 = b.integer;
  56.        
  57.         // pad with zeros
  58.         while string_length(d1) < string_length(d2)
  59.             d1 += "0"
  60.         while string_length(d2) < string_length(d1)
  61.             d2 += "0"
  62.        
  63.         // add decimal parts from right to left
  64.         var d_res = self.AddStrings(d1, d2)
  65.        
  66.         var decimal_part = d_res.result
  67.        
  68.         // now for the integer parts, with the carry from the previous decimal addition
  69.         // pad them with LEADING zeros
  70.         while string_length(i1) < string_length(i2)
  71.             i1 = "0" + i1
  72.         while string_length(i2) < string_length(i1)
  73.             i2 = "0" + i2
  74.        
  75.         var i_res = self.AddStrings(i1, i2, d_res.carry)
  76.        
  77.         var integer_part = i_res.result
  78.         if i_res.carry > 0
  79.             integer_part = string(i_res.carry) + integer_part
  80.        
  81.         return new BigFloat(integer_part, decimal_part)
  82.     }
  83.    
  84.     static ToString = function ()
  85.     {
  86.         return self.integer + "." + self.decimal
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement