1. 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()?

new topic     » topic index » view message » categorize

2. Re: power overflow

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()?
>
>
>
>

new topic     » goto parent     » topic index » view message » categorize

3. Re: power overflow

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 smile

Best regards,
   Juergen

new topic     » goto parent     » topic index » view message » categorize

4. Re: power overflow

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 smile


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

new topic     » goto parent     » topic index » view message » categorize

5. Re: power overflow

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

new topic     » goto parent     » topic index » view message » categorize

6. Re: power overflow

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

new topic     » goto parent     » topic index » view message » categorize

7. Re: power overflow

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

new topic     » goto parent     » topic index » view message » categorize

8. Re: power overflow

|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

new topic     » goto parent     » topic index » view message » categorize

9. Re: power overflow

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

new topic     » goto parent     » topic index » view message » categorize

10. Re: power overflow

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

new topic     » goto parent     » topic index » view message » categorize

11. Re: power overflow

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

new topic     » goto parent     » topic index » view message » categorize

12. Re: power overflow

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

new topic     » goto parent     » topic index » view message » categorize

13. Re: power overflow

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

new topic     » goto parent     » topic index » view message » categorize

14. Re: power overflow

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 blink]

EUrs A

new topic     » goto parent     » topic index » view message » categorize

15. Re: power overflow

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 blink]

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 ]

new topic     » goto parent     » topic index » view message » categorize

16. Re: power overflow

T24gTW9uLCAzIEp1biAyMDAyIDAwOjIwOjQzICswMjAwLCBqbHVldGhqZUBnbXguZGUgd3JvdGU6
DQoNCjxTbmlwPg0KSSBqdXN0IGhhZDoNCiBpZiBiPjEgYW5kIGxvZyhhKSA+IGxvZygxZTMwMCkv
YiB0aGVuIGl0IHdpbGwgb3ZlcmZsb3cuDQoNClBldGUNCg==

new topic     » goto parent     » topic index » view message » categorize

17. Re: power overflow

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

new topic     » goto parent     » topic index » view message » categorize

18. Re: power overflow

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

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu