aboutsummaryrefslogtreecommitdiff
path: root/lmathlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2013-06-25 11:02:18 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2013-06-25 11:02:18 -0300
commit80cdf39d0ed1803825ecbbc81b64ab501b0f4b7c (patch)
tree253ebc5ea9ef380342dec07846d68378112c940e /lmathlib.c
parent45f3797a5b3ac126fb8ada48492fbca4be39912b (diff)
downloadlua-80cdf39d0ed1803825ecbbc81b64ab501b0f4b7c.tar.gz
lua-80cdf39d0ed1803825ecbbc81b64ab501b0f4b7c.tar.bz2
lua-80cdf39d0ed1803825ecbbc81b64ab501b0f4b7c.zip
avoid using 'modf' in the implementation of 'math.modf', to avoid
problems with 'double*'. (When using 'float' as lua_Number, 'float*' is not compatible with 'double*'...)
Diffstat (limited to 'lmathlib.c')
-rw-r--r--lmathlib.c22
1 files changed, 12 insertions, 10 deletions
diff --git a/lmathlib.c b/lmathlib.c
index 58565bfe..aa3fc550 100644
--- a/lmathlib.c
+++ b/lmathlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmathlib.c,v 1.86 2013/05/26 13:45:24 roberto Exp roberto $ 2** $Id: lmathlib.c,v 1.87 2013/06/13 19:32:52 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*/
@@ -22,12 +22,6 @@
22#define RADIANS_PER_DEGREE ((lua_Number)(PI/180.0)) 22#define RADIANS_PER_DEGREE ((lua_Number)(PI/180.0))
23 23
24 24
25/* types for lua_Number pointers subject to 'l_mathop' changes */
26typedef float l_pnumf;
27typedef double l_pnum;
28typedef long double l_pnuml;
29
30
31static int math_abs (lua_State *L) { 25static int math_abs (lua_State *L) {
32 lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1))); 26 lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1)));
33 return 1; 27 return 1;
@@ -100,14 +94,22 @@ static int math_fmod (lua_State *L) {
100 return 1; 94 return 1;
101} 95}
102 96
97/*
98** next function does not use 'modf', avoiding problems with 'double*'
99** (which is not compatible with 'float*') when lua_Number is not
100** 'double'.
101*/
103static int math_modf (lua_State *L) { 102static int math_modf (lua_State *L) {
104 l_mathop(l_pnum) ip; 103 lua_Number n = luaL_checknumber(L, 1);
105 lua_Number fp = l_mathop(modf)(luaL_checknumber(L, 1), &ip); 104 /* integer part (rounds toward zero) */
105 lua_Number ip = (n < 0) ? -l_mathop(floor)(-n) : l_mathop(floor)(n);
106 lua_pushnumber(L, ip); 106 lua_pushnumber(L, ip);
107 lua_pushnumber(L, fp); 107 /* fractionary part (test handles inf/-inf) */
108 lua_pushnumber(L, (n == ip) ? 0.0 : (n - ip));
108 return 2; 109 return 2;
109} 110}
110 111
112
111static int math_sqrt (lua_State *L) { 113static int math_sqrt (lua_State *L) {
112 lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1))); 114 lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1)));
113 return 1; 115 return 1;