Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5430
Number of posts: 16951

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
negative decimal powers of negative numbers Posted by myth.12887 on 6 Jun 2007 at 2:03 AM
i need to calcualte something like
-12.567 ^ -0.256

a normal pow(-12.567,-0.256) doesnt work obviously. I can still make it pow(-12.567,-0.256) if i use a recoprocal, bnut still the pow statement doesnt work,...

my question is that is -12.567^-0.256==(-1^-0.256)*(12.567^-0.256) ???
and is -1 raised to any negative decimal power equla to -1 ???


is this is the case then i can use the absolute value and calculate it
Report
Re: negative decimal powers of negative numbers Posted by Jonathan on 6 Jun 2007 at 2:26 AM
Hi,

: i need to calcualte something like
: -12.567 ^ -0.256
:
: a normal pow(-12.567,-0.256) doesnt work obviously. I can still make
: it pow(-12.567,-0.256) if i use a recoprocal, bnut still the pow
: statement doesnt work,...
So, from the POW docs:

If "x" and "y" are both zero, or if "x" is non-positive and "y" is not an integer, "pow" return -HUGE_VAL and sets "errno" to EDOM.


The second part applies there and the "and" means we only need to deal with one of the conditions, so it would appear we can solve the problem by writing:

double z = -pow(12.567, -0.256);


Instead. Which works. Also this gives the same answer:

double z = -1.0 / pow(12.567, 0.256);


Which uses the reciprocal property you mention.

: my question is that is -12.567^-0.256==(-1^-0.256)*(12.567^-0.256)
: ???
Can't see how.

: and is -1 raised to any negative decimal power equla to -1 ???
No. What is -1 ^ -2? It is 1 / (-1 ^ 2) = 1 / (-1 * -1) = 1.

Jonathan
###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
Report
Re: negative decimal powers of negative numbers Posted by myth.12887 on 6 Jun 2007 at 2:38 AM

is -12.567^-0.256==-(12.567^-0.256)

(note the brackets)

now
-4^-3=-1*pow(4,-3)
because -1^-3 is an integer and is equal to -1
but -1^0.5 is complex and i dont know how this can be defined in c
Report
Re: negative decimal powers of negative numbers Posted by Jonathan on 6 Jun 2007 at 2:54 AM
Grrr...I need more coffee...

: is -12.567^-0.256==-(12.567^-0.256)
:
: (note the brackets)
:
Ah, I see what you're getting at. And no, I'm pretty sure it isn't. So I think what I just suggested was wrong. D'oh.

: now
: -4^-3=-1*pow(4,-3)
: because -1^-3 is an integer and is equal to -1
Yes, but -4^-4 != -1*pow(4,-4)

: but -1^0.5 is complex and i dont know how this can be defined in c
:
Yup, 'cus that's doing the square root of -1. And roots of negative numbers are complex, so in your example it's going to be complex too. Man am I rusty.

C99 added support for complex numbers, though that makes your program only work where you have C99 support. Check out complex.h and related things, anyway.

Jonathan
###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
Report
Re: negative decimal powers of negative numbers Posted by stober on 6 Jun 2007 at 2:54 AM
:
: is -12.567^-0.256==-(12.567^-0.256)
:
: (note the brackets)
:
: now
: -4^-3=-1*pow(4,-3)
: because -1^-3 is an integer and is equal to -1
: but -1^0.5 is complex and i dont know how this can be defined in c
:

http://en.wikipedia.org/wiki/Exponentiation

wikipedia has a good explaination. A ^ -N is equal to 1 / (A ^ N). In C/C++ terms: 1.0 / pow(a,n);


=============================================
never lie -- the government doesn't like the competition. (Author unknown)
Report
Re: negative decimal powers of negative numbers Posted by BitByBit_Thor on 6 Jun 2007 at 8:49 AM
But the entire problem is that:
x^y is undefined when x is negative and y is a non-integer number.

However you could make some personal rule for that and then code it...

Best Regards,
Richard

The way I see it... Well, it's all pretty blurry



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.