00001 /* slog - fixed point scaled logarithm stuff. 00002 * 00003 * This file is copyright 2002 Jim Kent, but license is hereby 00004 * granted for all use - public, private or commercial. */ 00005 00006 #include "common.h" 00007 #include "slog.h" 00008 00009 static char const rcsid[] = "$Id: slog.c,v 1.4 2003/05/06 07:33:44 kate Exp $"; 00010 00011 double fSlogScale = 8192.0; /* Convert to fixed point by multiplying by this. */ 00012 double invSlogScale = 0.0001220703125; /* Conver back to floating point with this. */ 00013 00014 int slog(double val) 00015 /* Return scaled log. */ 00016 { 00017 return (round(fSlogScale*log(val))); 00018 } 00019 00020 int carefulSlog(double val) 00021 /* Returns scaled log that makes sure there's no int overflow. */ 00022 { 00023 if (val < 0.0000001) 00024 val = 0.0000001; 00025 return slog(val); 00026 } 00027 00028 double invSlog(int scaledLog) 00029 /* Inverse of slog. */ 00030 { 00031 return exp(scaledLog*invSlogScale); 00032 } 00033
1.5.2