[comp.bugs.sys5] pow

mark@cogent.UUCP (Captain Neptune) (05/18/87)

I am running an HP 9000/840 running HP-UX and I have a problem doing some math.
I am trying to use pow(3M) to raise a positive integer value to a negative
integer value.  For example, the 12th root of 2 --> 2 ^ (-12) --> pow(2,-12).

I keep getting "DOMAIN error" even though the manual page doesn't seem to
cite this case as an illegal one.  It would be illegal if, say, the first
argument was zero, for instance.  Pow(3M) is found on the manual page for
"exp(3M)".

Any ideas why this is happening?  Have I made some fundamental blunder?
Has pow(3M) made some fundamental blunder?
-- 
+----------------------------------------------------------------------------+
|     Mark Steven Jeghers: the terrorist smuggling CIA weapons to Libya      |
|                                                                            |
|     {ihnp4,cbosgd,lll-lcc,lll-crg}|{dual,ptsfa}!cogent!mark                |
|                                                                            |
|     Standard Disclaimer:  Contents may have settled during shipment.       |
+----------------------------------------------------------------------------+

ubi@sri-unix.ARPA (Ron Ueberschaer) (05/18/87)

> [problem with doing]  pow(2, -12)
When passing integer arguments to pow(), or any other
math function requiring floating points (double, actually),
be sure that you either cast the variable, e.g.

	pow((double) i, (double) j)    ,

or explicitely type the decimal point, e.g.

	pow(2., -12.)    .

While most compilers handle implicit type conversion in
*expressions*, arguments to *functions* require more care.

The particular problem you are having bit me once before,
although I didn't get a useful runtime error message, just
the old "(core dumped)."  I don't think the negative exponent
is the problem.  A negative mantissa, however, will not work,
even if the exponent is integer.

			Hope this helps,

			Ron Ueberschaer		
			SRI International 	Menlo Park, CA
			...!{hplabs,rutgers}!sri-unix!ubi
			ubi@sri-unix.UUCP

tj@mks.UUCP (05/18/87)

In article <226@cogent.UUCP>, mark@cogent.UUCP (Captain Neptune) writes:
> 
> I am running an HP 9000/840 running HP-UX and I have a problem doing some math.
> I am trying to use pow(3M) to raise a positive integer value to a negative
> integer value.  For example, the 12th root of 2 --> 2 ^ (-12) --> pow(2,-12).
> 
> I keep getting "DOMAIN error" even though the manual page doesn't seem to
> cite this case as an illegal one.  It would be illegal if, say, the first
> argument was zero, for instance.  Pow(3M) is found on the manual page for
> "exp(3M)".

My guess is that you have neglected ensure that the arguments passed to pow
are doubles. try pow(2.0, -12.0);
BTW the 12th root of 2 is 2^(1/12) i.e. pow(2.0, 1.0/12.0);

V7 and BSD man page say that pow gives domain error when 2nd argument
is negative and non-integral. This is wrong. SYSV man page correctly
states that domain error results when 1st arg is negative and 2nd is
non-integral.

-- 
     ll  // // ,~/~~\'   T. J. Thompson {decvax,ihnp4,seismo}!watmath!mks!tj
    /ll/// //l' `\\\     Mortice Kern Systems Inc.
   / l //_// ll\___/     43 Bridgeport Rd. E., Waterloo, ON, Can. N2J 2J4
O_/                      (519)884-2251

gertler@mtuxo.UUCP (d.gertler) (05/18/87)

+ In article <226@cogent.UUCP> mark@cogent.UUCP (Captain Neptune) writes:
+    [...]
+ integer value.  For example, the 12th root of 2 --> 2 ^ (-12) --> pow(2,-12).
+    [...]
+   Have I made some fundamental blunder?

Yes.  2^(-12) is *NOT* the 12th root of 2.  It is the 12th power of 1/2.
[As for the matter at hand, your pow(3M) problem, I have no answer.  Sorry,
but I couldn't let this one go by.]

worley@dana.UUCP (John Worley) (05/19/87)

> I am trying to use pow(3M) to raise a positive integer value to a negative
> integer value.  For example, the 12th root of 2 --> 2 ^ (-12) --> pow(2,-12).
> 
> Any ideas why this is happening?  Have I made some fundamental blunder?
> Has pow(3M) made some fundamental blunder?

    You have made two fundamental blunders.  First, the arguments to pow() are
doubles, so the correct way to all the function is:

	pow(2.0, -12.0);

   The second blunder is that 2 ^ -12 == 1 / (2 ^ 12), NOT 12th-root 2!  The
code for that would be:

	pow(2.0, 1.0 / 12.0);

    I suggest using 2^-12 would generate a useless musical scale (the only
practical application of 12-th root 2 I know about).

					John Worley
					hplabs!dana!worley