PennMUSH Community

Changeset 1258

Show
Ignore:
Timestamp:
09/12/08 14:40:40 (3 months ago)
Author:
shawnw
Message:

Clean up round() a bit.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • 1.8.3/branches/devel/CHANGES.183

    r1257 r1258  
    5858 * cbufferadd() - Add text to a channel buffer without broadcasting it. 
    5959   Allows for @chan/recall on an @chat that's been overridden. 
     60 * Rewrote the internals of round() to avoid some nasty kludges. [SW] 
    6061 
    6162Fixes: 
  • 1.8.3/branches/devel/config.h.in

    r1195 r1258  
    182182#undef HAVE_LOG2 
    183183 
     184#undef HAVE_ROUND 
     185 
    184186#undef HAS_CRYPT 
    185187 
  • 1.8.3/branches/devel/configure.in

    r1235 r1258  
    221221AC_FUNC_SETPGRP 
    222222fi 
    223 AC_CHECK_FUNCS([cbrt log2 getuid geteuid seteuid getpriority setpriority]) 
    224 AC_CHECK_FUNCS([socketpair sigaction sigprocmask imaxdiv valloc writev]) 
     223AC_CHECK_FUNCS([cbrt log2 round imaxdiv]) 
     224AC_CHECK_FUNCS([getuid geteuid seteuid getpriority setpriority]) 
     225AC_CHECK_FUNCS([socketpair sigaction sigprocmask valloc writev]) 
    225226AC_CHECK_FUNCS([fcntl pselect poll ppoll pollts kqueue epoll_ctl inotify_init]) 
    226227 
  • 1.8.3/branches/devel/game/txt/hlp/pennfunc.hlp

    r1255 r1258  
    34103410  
    34113411  Rounds <number> to <places> decimal places. <places> must be between 
    3412   0 and 6
     3412  0 and config(float_precision)
    34133413  
    34143414See also: ceil(), floor(), bound(), trunc() 
  • 1.8.3/branches/devel/src/funmath.c

    r1243 r1258  
    899899} 
    900900 
     901double 
     902fround(double n, int d) 
     903{ 
     904  double ex = pow(10.0, d); 
     905  return floor(n * ex + 0.5) / ex; 
     906} 
     907 
    901908/* ARGSUSED */ 
    902909FUNCTION(fun_round) 
    903910{ 
    904   char temp[BUFFER_LEN]; 
    905911  int places; 
     912  double n, rounded; 
    906913 
    907914  if (!is_number(args[0])) { 
    908915    safe_str(T(e_num), buff, bp); 
    909916    return; 
    910   } 
     917  } else 
     918    n = parse_number(args[0]); 
     919 
    911920  if (nargs == 2) { 
    912921    if (!is_integer(args[1])) { 
     
    923932    places = FLOAT_PRECISION; 
    924933 
    925   /* The 0.0000001 is a kludge because .15 gets represented as .149999... 
    926    * on many systems, and rounds down to .1. Lame. */ 
    927   sprintf(temp, "%.*f", places, parse_number(args[0]) + 0.0000001); 
    928  
    929   /* Handle the bizarre "-0" sprintf problem. */ 
    930   if (!strcmp(temp, "-0")) 
    931     safe_chr('0', buff, bp); 
     934#ifdef HAVE_ROUND 
     935  if (places == 0) 
     936    rounded = round(n); 
    932937  else 
    933     safe_str(temp, buff, bp); 
     938    rounded = fround(n, places); 
     939#else 
     940  rounded = fround(n, places); 
     941#endif 
     942 
     943  safe_number(rounded, buff, bp); 
    934944} 
    935945 
  • 1.8.3/branches/devel/test/testmath.pl

    r1121 r1258  
    4444$god->command('@config/set float_precision=10'); 
    4545test('root.5', $god, 'think root(125, 5)', '2.6265278044'); 
     46 
     47test('round.0', $god, 'think round(pi(), 0)', '3'); 
     48test('round.1', $god, 'think round(pi(), 1)', '3.1'); 
     49test('round.2', $god, 'think round(pi(), 2)', '3.14'); 
     50test('round.3', $god, 'think round(pi(), 3)', '3.142'); 
     51test('round.4', $god, 'think round(pi(), 4)', '3.1416'); 
     52test('round.5', $god, 'think round(pi(), 5)', '3.14159'); 
     53test('round.6', $god, 'think round(-[pi()], 3)', '-3.142');