Sporkinator

Round(x)

Dec 20th, 2011
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*  round() without the loss of accuracy with huge numbers
  2.    
  3.     Test code:
  4.    
  5.     show_message(round(600851475143)); //WRONG
  6.     show_message(Round(600851475143)); //RIGHT
  7.    
  8.     This script also avoids the GM bug where round(0.5) = 0
  9.     Now, Round(0.5) = 1 like it should.
  10. */
  11. var frc;
  12. frc = frac(argument0);
  13. if (frc >= 0.5)
  14.     return argument0 - frc + 1;
  15. else if (frc <= -0.5)
  16.     return argument0 - frc - 1;
  17. else
  18.     return argument0 - frc;
Advertisement
Add Comment
Please, Sign In to add comment