1/* |
2 * Copyright (C) 1984-2011 Mark Nudelman |
3 * 4 * You may distribute under the terms of either the GNU General Public 5 * License or the Less License, as specified in the README file. 6 * 7 * For more information about less, or for information on how to 8 * contact the author, see the README file. 9 */ 10 --- 234 unchanged lines hidden (view full) --- 245 p = "cannot open"; 246#endif 247 len = strlen(filename) + strlen(p) + 3; 248 m = (char *) ecalloc(len, sizeof(char)); 249 SNPRINTF2(m, len, "%s: %s", filename, p); 250 return (m); 251} 252 |
253/* #define HAVE_FLOAT 0 */ 254 255 static POSITION 256muldiv(val, num, den) 257 POSITION val, num, den; 258{ 259#if HAVE_FLOAT 260 double v = (((double) val) * num) / den; 261 return ((POSITION) (v + 0.5)); 262#else 263 POSITION v = ((POSITION) val) * num; 264 265 if (v / num == val) 266 /* No overflow */ 267 return (POSITION) (v / den); 268 else 269 /* Above calculation overflows; 270 * use a method that is less precise but won't overflow. */ 271 return (POSITION) (val / (den / num)); 272#endif 273} 274 |
275/* 276 * Return the ratio of two POSITIONS, as a percentage. 277 * {{ Assumes a POSITION is a long int. }} 278 */ 279 public int 280percentage(num, den) 281 POSITION num, den; 282{ |
283 return (int) muldiv(num, (POSITION) 100, den); |
284} 285 286/* 287 * Return the specified percentage of a POSITION. 288 */ 289 public POSITION 290percent_pos(pos, percent, fraction) 291 POSITION pos; 292 int percent; 293 long fraction; 294{ 295 /* Change percent (parts per 100) to perden (parts per NUM_FRAC_DENOM). */ |
296 POSITION perden = (percent * (NUM_FRAC_DENOM / 100)) + (fraction / 100); |
297 298 if (perden == 0) 299 return (0); |
300 return (POSITION) muldiv(pos, perden, (POSITION) NUM_FRAC_DENOM); |
301} 302 303#if !HAVE_STRCHR 304/* 305 * strchr is used by regexp.c. 306 */ 307 char * 308strchr(s, c) --- 56 unchanged lines hidden --- |