1. power overflow
- Posted by tone.skoda at siol.net Jun 02, 2002
- 449 views
I get this error message: C:\MyProj\Euphoria\TSLibrary\TSLibrary_Formula_Finder.e:232 in function power_() math function overflow error a = 24 b = 576 where power (a,b) is executed How can I check parameters a and b and not execute power() if they are too big (especially b)? I guess one way is to write my own version of power()?
2. Re: power overflow
- Posted by Derek Parnell <ddparnell at bigpond.com> Jun 02, 2002
- 420 views
Try Antoine's BIG NUMBER library. ----- Original Message ----- From: <tone.skoda at siol.net> To: "EUforum" <EUforum at topica.com> Subject: power overflow > > I get this error message: > > C:\MyProj\Euphoria\TSLibrary\TSLibrary_Formula_Finder.e:232 in function > power_() > math function overflow error > a = 24 > b = 576 > > > where power (a,b) is executed > > How can I check parameters a and b and not execute power() if they are too > big (especially b)? > I guess one way is to write my own version of power()? > > > >
3. Re: power overflow
- Posted by jluethje at gmx.de Jun 02, 2002
- 426 views
Hello tone, you wrote: > I get this error message: > C:\MyProj\Euphoria\TSLibrary\TSLibrary_Formula_Finder.e:232 in function > power_() > math function overflow error > a = 24 > b = 576 > where power (a,b) is executed > How can I check parameters a and b and not execute power() if they are too > big (especially b)? > I guess one way is to write my own version of power()? This is a snippet from my private file math.e: ------------------------------------------------------------------------ global function ln (object x) -- Euphoria's built-in log() function -- (= logarithm for base e = natural logarithm) return log(x) end function without warning global function log (atom b, object x) -- general log() function -- logarithm for base b and number (or sequence) x -- Note: built-in routine log() redefined -- in : b: positive real number, != 1 -- x: positive real number (or sequence of those numbers) -- out: real number -- (x = 1 --> function returns 0 for any base b) return ln(x)/ln(b) end function with warning ------------------------------------------------------------------------ Given this general log() function, y = power(a, b) is equivalent to b = log(a, y) . When we know the maximum numeric value, that Euphoria can handle (y_max), then we have: b_max = log(a, y_max) . After refman_2.htm, y_max = +1e300, i.e. ------------------------- b_max = log(a, +1e300) ------------------------- When a = 24 (as in your example above), b cannot be more than b_max = log(24, +1e300) = 217.36 . So you can see, that b = 576 is much too big. Now we check the calculation: y = power(24, 217.36) = 1.006272255e+300 OK Best regards, Juergen
4. Re: power overflow
- Posted by jluethje at gmx.de Jun 02, 2002
- 431 views
Ooops, I forgot to mention the 3rd function. >> I get this error message: >> C:\MyProj\Euphoria\TSLibrary\TSLibrary_Formula_Finder.e:232 in function >> power_() >> math function overflow error >> a = 24 >> b = 576 >> where power (a,b) is executed >> How can I check parameters a and b and not execute power() if they are too >> big (especially b)? >> I guess one way is to write my own version of power()? > This is a snippet from my private file math.e: > ------------------------------------------------------------------------ > global function ln (object x) > -- Euphoria's built-in log() function > -- (= logarithm for base e = natural logarithm) > return log(x) > end function > without warning > global function log (atom b, object x) > -- general log() function > -- logarithm for base b and number (or sequence) x > -- Note: built-in routine log() redefined > -- in : b: positive real number, != 1 > -- x: positive real number (or sequence of those numbers) > -- out: real number -- (x = 1 -->> function returns 0 for any base b) > return ln(x)/ln(b) > end function > with warning > ------------------------------------------------------------------------ > Given this general log() function, > y = power(a, b) is equivalent to > b = log(a, y) . > When we know the maximum numeric value, that Euphoria can handle > (y_max), then we have: > b_max = log(a, y_max) . > After refman_2.htm, y_max = +1e300, i.e. > ------------------------- > b_max = log(a, +1e300) > ------------------------- > When a = 24 (as in your example above), b cannot be more than > b_max = log(24, +1e300) = 217.36 . > So you can see, that b = 576 is much too big. > Now we check the calculation: > y = power(24, 217.36) = 1.006272255e+300 > OK The 3rd function in this context is: a = power(y, 1/b) . This means, a_max = power(y_max, 1/b) -------------------------- a_max = power(+1e300, 1/b) -------------------------- When b = 576 (as in your example above), a cannot be more than a_max = power(+1e300, 1/576) = 3.32 . So you can see, that a = 24 is much too big. Checking the calculation gives: y = power(3.32, 576) = 1.498084165e+300 That's OK, and we see, that +1e300 is only *roughly* the biggest number, that Euphoria can handle. Best regards, Juergen
5. Re: power overflow
- Posted by tone.skoda at siol.net Jun 03, 2002
- 443 views
Thanks, here is general function I wrote safe_power(): global constant LOWEST_ATOM = -1e300 global constant HIGHEST_ATOM = +1e300 global function log_general (atom b, object x) -- general log() function -- logarithm for base b and number (or sequence) x -- in : b: positive real number, != 1 -- x: positive real number (or sequence of those numbers) -- out: real number -- (x = 1 --> function returns 0 for any base b) return log (x) / log (b) end function --/* -- safe_power [Created on 3. June 2002, 17:59] -- The 'safe_power' function does same thing as Euphoria builtin 'power()' -- with this difference: if return value of 'power()' is too big -- interpreter crashes with error mesage math overflow. -- If in this function returned value would be too big -- then error 'HIGHEST_ATOM' is returned. -- -- PARAMETERS -- 'a' -- Same as in built in 'power()'. -- 'b' -- Same as in built in 'power()'. -- -- RETURN VALUES -- The value of power(), or error value 'HIGHEST_ATOM' --*/ global function safe_power (atom a, atom b) atom b_max --// if 'b' is bigger or equal than this then overflow atom a_max --// if 'a' is bigger or equal than this then overflow if a = 0 or b = 0 then --// log (0) crashes return power (a ,b) end if b_max = log_general (a, HIGHEST_ATOM) if b >= b_max then return HIGHEST_ATOM end if a_max = power (HIGHEST_ATOM, 1 / b) if a >= a_max then return HIGHEST_ATOM end if return power (a, b) end function ----- Original Message ----- From: <jluethje at gmx.de> To: "EUforum" <EUforum at topica.com> ... > Best regards, > Juergen
6. Re: power overflow
- Posted by jluethje at gmx.de Jun 03, 2002
- 425 views
Hi tone, nice idea to write this function. With two changes, it really will be safe, I think. > Thanks, here is general function I wrote safe_power(): > global constant LOWEST_ATOM = -1e300 > global constant HIGHEST_ATOM = +1e300 > global function log_general (atom b, object x) > -- general log() function > -- logarithm for base b and number (or sequence) x > -- in : b: positive real number, != 1 > -- x: positive real number (or sequence of those numbers) > -- out: real number -- (x = 1 -->> function returns 0 for any base b) > return log (x) / log (b) > end function > --/* > -- safe_power [Created on 3. June 2002, 17:59] > -- The 'safe_power' function does same thing as Euphoria builtin 'power()' > -- with this difference: if return value of 'power()' is too big > -- interpreter crashes with error mesage math overflow. > -- If in this function returned value would be too big > -- then error 'HIGHEST_ATOM' is returned. > -- > -- PARAMETERS > -- 'a' > -- Same as in built in 'power()'. > -- 'b' > -- Same as in built in 'power()'. > -- > -- RETURN VALUES > -- The value of power(), or error value 'HIGHEST_ATOM' > --*/ > global function safe_power (atom a, atom b) > atom b_max --// if 'b' is bigger or equal than this then overflow > atom a_max --// if 'a' is bigger or equal than this then overflow -- Additionally: if a = 0 and b = 0 then return "power(0,0) is not defined" end if > if a = 0 or b = 0 then --// log (0) crashes -- Change previous line -- (see my comments in global function log_general() above): if a <= 0 or b <= 0 or a = 1 then > return power (a ,b) > end if > b_max = log_general (a, HIGHEST_ATOM) > if b >= b_max then > return HIGHEST_ATOM > end if > a_max = power (HIGHEST_ATOM, 1 / b) > if a >= a_max then > return HIGHEST_ATOM > end if > return power (a, b) > end function For more information about logarithms, see http://mathworld.wolfram.com/Logarithm.html Best regards, Juergen
7. Re: power overflow
- Posted by a.tammer at hetnet.nl Jun 03, 2002
- 412 views
Just a question on power(0,0), I thought any number raised to power 0 was 1, is this the only exceprion on that rule? antoine tammer
8. Re: power overflow
- Posted by dm31 at uow.edu.au Jun 03, 2002
- 434 views
|Just a question on power(0,0), |I thought any number raised to power 0 was 1, |is this the only exceprion on that rule? Yes, Otherwise you could prove really stupid stuff like infinity - 1 = 0
9. Re: power overflow
- Posted by Kat <gertie at PELL.NET> Jun 03, 2002
- 419 views
On 4 Jun 2002, at 7:27, dm31 at uow.edu.au wrote: > > |Just a question on power(0,0), > |I thought any number raised to power 0 was 1, > |is this the only exceprion on that rule? > > Yes, > Otherwise you could prove really stupid stuff like > > infinity - 1 = 0 Or infinity + 1 = -infinity Kat
10. Re: power overflow
- Posted by jluethje at gmx.de Jun 03, 2002
- 434 views
Hi tone, I had a closer look at your function safe_power(). It isn't necessary to use both functions b_max = log_general (a, HIGHEST_ATOM) a_max = power (HIGHEST_ATOM, 1 / b) Using one of them is sufficient. The second one is simpler, I think. I wrote: <snip> >> global function safe_power (atom a, atom b) >> atom b_max --// if 'b' is bigger or equal than this then overflow >> atom a_max --// if 'a' is bigger or equal than this then overflow > -- Additionally: > if a = 0 and b = 0 then > return "power(0,0) is not defined" > end if >> if a = 0 or b = 0 then --// log (0) crashes > -- Change previous line > -- (see my comments in global function log_general() above): > if a <= 0 or b <= 0 or a = 1 then >> return power (a ,b) >> end if >> b_max = log_general (a, HIGHEST_ATOM) >> if b >= b_max then >> return HIGHEST_ATOM >> end if >> a_max = power (HIGHEST_ATOM, 1 / b) >> if a >= a_max then >> return HIGHEST_ATOM >> end if >> return power (a, b) >> end function > For more information about logarithms, see > http://mathworld.wolfram.com/Logarithm.html My proposal for function safe_power(): ----------------------------------------------------------------->8--- global constant HIGHEST_ATOM = +1e300 function abs (atom x) if x < 0 then x = -x end if return x end function global function safe_power (atom a, atom b) atom a_max --// if 'a' is bigger or equal than this then overflow if b = 0 then if a != 0 then return 1 end if return "power(0,0) is not defined" end if if b > 1 then -- if b < 1, then the overflow can be here! a_max = power (HIGHEST_ATOM, 1 / b) if abs(a) >= a_max then return HIGHEST_ATOM end if end if return power (a, b) end function ----------------------------------------------------------------->8--- Regards, Juergen
11. Re: power overflow
- Posted by jluethje at gmx.de Jun 03, 2002
- 442 views
Hello Antoine, you wrote: > Just a question on power(0,0), > I thought any number raised to power 0 was 1, Yes, and 0 raised to any power is 0. So one cannot decide, if power(0,0) should be 0 or 1. > is this the only exceprion on that rule? Yes. > antoine tammer Regards, Juergen
12. Re: power overflow
- Posted by rolf.schroeder at desy.de Jun 04, 2002
- 426 views
a.tammer at hetnet.nl wrote: > > Just a question on power(0,0), > I thought any number raised to power 0 was 1, > is this the only exceprion on that rule? Hi Antoine, mathematically this is not generally defined (there are limit conditions known which approach 0 or 1 depending on the special case). Any non zero number raised to zero is equal 1, that's clear. One may define: power(0,0) := 1. But there are cases where power(0,0) := 0 would be more suitable. So: the best is NOT do define it arbitrary and let instead the programmer define what he wants. Have a nice day, Rolf
13. Re: power overflow
- Posted by Martin Stachon <martin.stachon at worldonline.cz> Jun 04, 2002
- 423 views
antoine tammer wrote: > Just a question on power(0,0), > I thought any number raised to power 0 was 1, > is this the only exceprion on that rule? power(x,0) = power(x,-1)*power(x,1)=(1/x)*(x/1)=x/x=1 but for 0 : power(0,0) = power(0,-1)*power(0,1)=(1/0)*(0/1)=0/0 In the math I've been taught so far, division by zero is not defined. Martin
14. Re: power overflow
- Posted by a.tammer at hetnet.nl Jun 05, 2002
- 455 views
Hi, There's one way, to solve the problems on this issue, I guess. If you use 'Anomaly' as Carl White calls it, it's an FPvalue, and a strange one too, it's 0 AND ! 0 I think Carl will allow me to give you it's exact specs. Specs 'Anomaly': FP64 63 0binary positions, last position =3D 1 last=3D LSD [not the dope, dope-heads ] EUrs A
15. Re: power overflow
- Posted by "Carl W." <euphoria at cyreksoft.yorks.com> Jun 05, 2002
- 425 views
a.tammer at hetnet.nl wrote: > There's one way, to solve the problems on this issue, I guess. > If you use 'Anomaly' as Carl White calls it, it's an FPvalue, > and a strange one too, it's 0 AND ! 0 > I think Carl will allow me to give you it's exact specs. I called it 'Anomaly' before I looked into what value it actually represents, because it really is an anomalous number in the way that Euphoria handles it. [Note that some other languages have the similar problems] A better name for 'Anomaly' would be something like 'float64_minimum' or 'tiniest_atom' since it's true value is power(2, -1074). Surprisingly, the power() function actually generates it: constant X = power(2, -1073), -- Anomaly-like, but not the smallest possible. Anomaly = power(2, -1074), -- Smallest representable IEEE-64bit atom. Y = power(2, -1075), -- Too small to represent. Zero = 0 ? X -- prints 0 ? X = Zero -- prints 0, so it *looks*like* X = 0 and != 0 ? Anomaly -- prints 0 ? Anomaly = Zero -- prints 0, so it *looks*like* Anomaly = 0 and != 0 ? Y -- prints 0 ? Y = Zero -- prints 1 - Too small so is identical to 0. Also, if you try dividing by Anomaly (or 'X'), you'll get a 'Divide by Zero' error even though Anomaly is really greater than Zero. This is down to the fact that 1/power(2,-1074) = power(2,1074) which is too big to fit in an atom or a float64. > Specs 'Anomaly': FP64 63 0binary positions, last position = 1 > last= LSD [not the dope, dope-heads ] I was going to suggest using float64_to_atom({0,0,0,0,0,0,0,1}) [or is that float64_to_atom({1,0,0,0,0,0,0,0})?], but using power() doesn't require an include and it's easier to remember which way round to put the numbers. ;) Carl -- [ Carl R White -=- aka -=- Cyrek the Illogical ] [ () E-mail...: cyrek{}cyreksoft.yorks.com ] [ /\ URL......: http://www.cyreksoft.yorks.com ]
16. Re: power overflow
- Posted by petelomax at blueyonder.co.uk Jun 05, 2002
- 445 views
T24gTW9uLCAzIEp1biAyMDAyIDAwOjIwOjQzICswMjAwLCBqbHVldGhqZUBnbXguZGUgd3JvdGU6 DQoNCjxTbmlwPg0KSSBqdXN0IGhhZDoNCiBpZiBiPjEgYW5kIGxvZyhhKSA+IGxvZygxZTMwMCkv YiB0aGVuIGl0IHdpbGwgb3ZlcmZsb3cuDQoNClBldGUNCg==
17. Re: power overflow
- Posted by jluethje at gmx.de Jun 06, 2002
- 427 views
Hello Pete, you wrote: > <Snip> > I just had: > if b>1 and log(a) > log(1e300)/b then it will overflow. > Pete I agree, but this condition is not sufficient if 'a' is negative. If e.g. a = -50, and b = 500, then power(a, b) will overflow, but -------------------------------------------------------------- atom a, b a = -50 b = 500 if b > 1 and log(abs(a)) > log(1e300)/b then puts(1, "overflow") else ? power(a, b) end if -------------------------------------------------------------- will not print "overflow", but there will be an error with log(). So in my opinion, it should be something like this: -------------------------------------------------------------- function abs (atom x) if x < 0 then x = -x end if return x end function atom a, b a = -50 b = 500 if b > 1 and log(abs(a)) > log(1e300)/b then puts(1, "overflow") else ? power(a, b) end if -------------------------------------------------------------- What I previously wrote, was (simplified): -------------------------------------------------------------- if b > 1 and abs(a) > power(1e300, 1/b) then puts(1, "overflow") else ? power(a, b) end if -------------------------------------------------------------- So you found the logarithm of my function. Best regards, Juergen
18. Re: power overflow
- Posted by jluethje at gmx.de Jun 06, 2002
- 461 views
I wrote: > Hello Pete, > you wrote: >> <Snip> >> I just had: >> if b>1 and log(a) > log(1e300)/b then it will overflow. >> Pete > I agree, but this condition is not sufficient if 'a' is negative. > If e.g. a = -50, and b = 500, then power(a, b) will overflow, but > -------------------------------------------------------------- > atom a, b > a = -50 > b = 500 > if b > 1 and log(abs(a)) > log(1e300)/b then There is a typo, sorry. The previous line should be: > if b > 1 and log(a) > log(1e300)/b then > puts(1, "overflow") > else > ? power(a, b) > end if > -------------------------------------------------------------- > will not print "overflow", but there will be an error with log(). > So in my opinion, it should be something like this: > -------------------------------------------------------------- > function abs (atom x) > if x < 0 then x = -x end if > return x > end function > atom a, b > a = -50 > b = 500 > if b > 1 and log(abs(a)) > log(1e300)/b then > puts(1, "overflow") > else > ? power(a, b) > end if > -------------------------------------------------------------- > What I previously wrote, was (simplified): > -------------------------------------------------------------- > if b > 1 and abs(a) > power(1e300, 1/b) then > puts(1, "overflow") > else > ? power(a, b) > end if > -------------------------------------------------------------- > So you found the logarithm of my function. > Best regards, > Juergen