Re: Math crashes with translated programs (Rob?)
- Posted by r.schr at t-online.de Jun 13, 2002
- 394 views
Andy Serpa wrote: > > > I didn't get 0 with the interpreter! Correctly it reports: > > > > tst.ex:5 > > math function overflow error > > --> see ex.err > > > Really? Maybe it is a processor thing. Do you have an Intel? No, it's an AMD K6-2 . > ... > So I need a COMPLETE safe_power() function, it seems, that will protect > me from overflows from any possible atoms (integer or floating point, > positive or negative) used for base and exponent. Anyone? > I wouldn't use a "save_power()" function. Instead, I would check the two arguments before calling power(). May be the following function is useful for this purpose: -------------------------------------------------------- constant lim = log(3e+307) -- needed for chk_pow_args() function chk_pow_args(atom a, atom b) -- ------------ if a > 0.0 then if b*log(a) < lim then return 1 end if elsif a < 0 and integer(a) then if b*log(-a) < lim then return 1 end if end if return 0 end function -- test: atom a, b a = 0.0951 b = -300.784 if chk_pow_args(a,b) then ? power( a, b) else puts(1,"power() overflow! Abort.\n") end if -------------------------------------------------------- Have a nice day, Rolf