diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-02-18 10:06:05 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-02-18 10:06:05 -0300 |
| commit | 897573983977e2bdfe405bd056abccc1db1e0f8d (patch) | |
| tree | 0b48c758d574a24cadd113493e1b9da648d8cc55 | |
| parent | d07abcc6c7064040c15444a7727c39c707130f60 (diff) | |
| download | lua-897573983977e2bdfe405bd056abccc1db1e0f8d.tar.gz lua-897573983977e2bdfe405bd056abccc1db1e0f8d.tar.bz2 lua-897573983977e2bdfe405bd056abccc1db1e0f8d.zip | |
better precision for log(x, 10)
| -rw-r--r-- | lmathlib.c | 14 |
1 files changed, 10 insertions, 4 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lmathlib.c,v 1.69 2007/03/27 12:37:00 roberto Exp roberto $ | 2 | ** $Id: lmathlib.c,v 1.70 2007/06/21 13:48:04 roberto Exp roberto $ |
| 3 | ** Standard mathematical library | 3 | ** Standard mathematical library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -112,9 +112,15 @@ static int math_pow (lua_State *L) { | |||
| 112 | } | 112 | } |
| 113 | 113 | ||
| 114 | static int math_log (lua_State *L) { | 114 | static int math_log (lua_State *L) { |
| 115 | lua_Number res = log(luaL_checknumber(L, 1)); | 115 | lua_Number x = luaL_checknumber(L, 1); |
| 116 | if (!lua_isnoneornil(L, 2)) | 116 | lua_Number res; |
| 117 | res /= log(luaL_checknumber(L, 2)); | 117 | if (lua_isnoneornil(L, 2)) |
| 118 | res = log(x); | ||
| 119 | else { | ||
| 120 | lua_Number base = luaL_checknumber(L, 2); | ||
| 121 | if (base == 10.0) res = log10(x); | ||
| 122 | else res = log(x)/log(base); | ||
| 123 | } | ||
| 118 | lua_pushnumber(L, res); | 124 | lua_pushnumber(L, res); |
| 119 | return 1; | 125 | return 1; |
| 120 | } | 126 | } |
