473,416 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Trouble calculating 5th root of a given number using C

Hi Friends

Please help me to write a C program to find the 5th (fifth) root of a given number.
Ex:(1) Input : 32
Output : 5th root of 32 is 2

Ex:(1) Input : 243
Output : 5th root of 243 is 3

Click here : www.c4swimmers.esmartguy.com to Test Your C Programming Strengths.
Waiting for your reply.

Regards
Kishor
Nov 14 '05 #1
13 12688

"Kishor" <na***********@rediffmail.com> wrote in message
news:84*************************@posting.google.co m...
Hi Friends

Please help me to write a C program to find the 5th (fifth) root of a given number. Ex:(1) Input : 32
Output : 5th root of 32 is 2

Ex:(1) Input : 243
Output : 5th root of 243 is 3

Click here : www.c4swimmers.esmartguy.com to Test Your C Programming Strengths.

Waiting for your reply.

Regards
Kishor


Is one allowed to use the standard math.h ?
If so, then
double result = pow(32, 1/5);
should give you 2.0
Allan
Nov 14 '05 #2
nrk
Allan Bruce wrote:

"Kishor" <na***********@rediffmail.com> wrote in message
news:84*************************@posting.google.co m...
Hi Friends

Please help me to write a C program to find the 5th (fifth) root of a given number.
Ex:(1) Input : 32
Output : 5th root of 32 is 2

Ex:(1) Input : 243
Output : 5th root of 243 is 3

Click here : www.c4swimmers.esmartguy.com to Test Your C Programming

Strengths.

I ran some of the code there through the DS9K, and now there are nasal
demons all over the place :-)

Waiting for your reply.

Regards
Kishor


Is one allowed to use the standard math.h ?
If so, then
double result = pow(32, 1/5);
should give you 2.0


Fortunately, it gives me 1.0 as expected :-) Perhaps you meant this?:

double result = pow(32, 1/5.);

-nrk.
Allan


--
Remove devnull for email
Nov 14 '05 #3
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:bv**********@news.freedom2surf.net...
"Kishor" <na***********@rediffmail.com> wrote in message
news:84*************************@posting.google.co m...
Hi Friends

Please help me to write a C program to find the 5th (fifth) root of a

given number.
Ex:(1) Input : 32
Output : 5th root of 32 is 2

Ex:(1) Input : 243
Output : 5th root of 243 is 3

Click here : www.c4swimmers.esmartguy.com to Test Your C Programming

Strengths.


Waiting for your reply.

Regards
Kishor


Is one allowed to use the standard math.h ?
If so, then
double result = pow(32, 1/5);
should give you 2.0


Or 1.0 at the very least. Try 1.0/5.0.

This approach is good enough for a homework assignment,
but not industrial strength.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Nov 14 '05 #4
P.J. Plauger writes:
Or 1.0 at the very least. Try 1.0/5.0.

This approach is good enough for a homework assignment,
but not industrial strength.


I take it for granted that you are right. But what would you recommend for
someone who does not wish to make this a life's work?
Nov 14 '05 #5
Kishor wrote:
Hi Friends

Please help me to write a C program to find the 5th (fifth) root of a given number.
Ex:(1) Input : 32
Output : 5th root of 32 is 2

Ex:(1) Input : 243
Output : 5th root of 243 is 3


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <float.h>

inline double root(double x)
{
return pow(x, 0.2);
}
inline double original(double x)
{
return pow(x, 5.);
}

int main(void)
{
size_t pass;
const size_t maxpass = 10;
double x;
const double upperlimit = 1000.;
srand((unsigned) time(0));
for (pass = 0; pass < maxpass; pass++) {
x = upperlimit * rand() / (1. + RAND_MAX);
printf("number: %.*g\n", DBL_DIG, x);
printf("5th root: %.*g\n", DBL_DIG, x = root(x));
printf("root to 5th power: %.*g\n\n", DBL_DIG, original(x));
}
return 0;
}

number: 349.126742687076
5th root: 3.22549685893672
root to 5th power: 349.126742687076

number: 933.513863012195
5th root: 3.92666773917324
root to 5th power: 933.513863012195

number: 337.094322778285
5th root: 3.20295094809955
root to 5th power: 337.094322778285

number: 921.04862164706
5th root: 3.91612469036527
root to 5th power: 921.04862164706

number: 193.956033326685
5th root: 2.86774584184895
root to 5th power: 193.956033326685

number: 536.526418756694
5th root: 3.51494247913713
root to 5th power: 536.526418756694

number: 769.721171818674
5th root: 3.77804117237769
root to 5th power: 769.721171818674

number: 245.490406174213
5th root: 3.00612409691297
root to 5th power: 245.490406174213

number: 818.889678921551
5th root: 3.82512013459173
root to 5th power: 818.889678921551

number: 422.267477028072
5th root: 3.35056098160768
root to 5th power: 422.267477028072

--
Martin Ambuhl
Nov 14 '05 #6
Allan Bruce wrote:

Is one allowed to use the standard math.h ?
If so, then
double result = pow(32, 1/5);
should give you 2.0


Bullshit. It yields 1.0

--
Martin Ambuhl
Nov 14 '05 #7
On Mon, 02 Feb 2004 18:45:09 GMT, Martin Ambuhl <ma*****@earthlink.net> wrote:
Allan Bruce wrote:

Is one allowed to use the standard math.h ?
If so, then
double result = pow(32, 1/5);
should give you 2.0


Bullshit. It yields 1.0


Forgive me if I misunderstand, but could someone explain to me what value 32^0
is? If I read the statement
double result = pow(32, 1/5);

correctly, that's what the statement is computing.

--
Lew Pitcher
IT Consultant, Enterprise Technology Solutions
Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employers')
Nov 14 '05 #8
Lew Pitcher <Le*********@td.com> spoke thus:
Forgive me if I misunderstand, but could someone explain to me what value 32^0
is? If I read the statement
double result = pow(32, 1/5);

correctly, that's what the statement is computing.


You're reading it correctly. 32 (or any number other than 0) raised
to the zero power gives 1.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #9

"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:VU*****************@newsread3.news.atl.earthl ink.net...
Allan Bruce wrote:

Is one allowed to use the standard math.h ?
If so, then
double result = pow(32, 1/5);
should give you 2.0


Bullshit. It yields 1.0


sorry it should have read
double result = pow(32.0, 1/5.0);

Nov 14 '05 #10
Christopher Benson-Manica wrote:
32 (or any number other than 0) raised to the zero power gives 1.


http://mathworld.wolfram.com/ExponentLaws.html

The definition 0^0=1 is sometimes used to simplify
formulas, but it should be kept in mind that this
equality is a definition and not a fundamental
mathematical truth (Knuth 1992; Knuth 1997, p. 56).

<g>

Nov 14 '05 #11
Grumble wrote:
Christopher Benson-Manica wrote:
32 (or any number other than 0) raised to the zero power gives 1.

http://mathworld.wolfram.com/ExponentLaws.html

The definition 0^0=1 is sometimes used to simplify
formulas, but it should be kept in mind that this
equality is a definition and not a fundamental
mathematical truth


That's quite a weird statement, if you think about it. I know it's not
your statement but Mathworld's, but still.

To my surprise (and to get on-topic), C99 doesn't really say what
pow(0.0, 0.0) should be:

"The pow functions compute x raised to the power y. A domain error
occurs if x is finite and negative and y is finite and not an integer
value. A domain error may occur if x is zero and y is less than or equal
to zero. A range error may occur."
(Knuth 1992; Knuth 1997, p. 56).


Knuth Vol 1, third edition, seems to be giving more info on binomials
than I could ever care to know on page 56, but nothing about exponentials.

Best regards,

Sidney

Nov 14 '05 #12
Sidney Cadot <si****@jigsaw.nl> wrote:

To my surprise (and to get on-topic), C99 doesn't really say what
pow(0.0, 0.0) should be:


It does in Annex F (for IEEE floating-point).

-Larry Jones

Summer vacation started! I can't be sick! -- Calvin
Nov 14 '05 #13
la************@ugsplm.com wrote:
Sidney Cadot <si****@jigsaw.nl> wrote:
To my surprise (and to get on-topic), C99 doesn't really say what
pow(0.0, 0.0) should be:

It does in Annex F (for IEEE floating-point).


So I see! F.9.4.4:

"pow(x,±0) returns 1 for any x, even a NaN."

The NaN clause is a surprise, to me at least.

Thanks and regards,

Sidney

Nov 14 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
8
by: Ricky Romaya | last post by:
Hi, I'm working on a file upload script. I need to calculate the CRC32 of the file(s) which are successfully uploaded. How can I do this? PHP only have CRC32 function for strings. However, the...
8
by: ali | last post by:
Hi, I'm trying to work on a recursive function that will give me root valuefor a given number. What i mean by root value is, if given 13, the answer is 1+3 = 4. If given 65, the answer is 2,...
4
by: 21novembre | last post by:
Hi all, I got a quite strange problem when I tried to setup a database backup shell. I put it this way: "bin/mysqldump --opt --user=xxx --password=xxx DB > DB.bak" However, error 1045 came to...
14
by: Protoman | last post by:
How do I write a function to calculate the nth root of a number; I'm sick of using pow() with a fractional exponent to do the job. Got any ideas on how to do this? Thanks for the help!!!
4
by: sathyashrayan | last post by:
(This is not a home work question) Dear group, I want a program to find one number between a set of natural number.A program to guess a number in between a Natural number set.This should be a...
3
by: weston | last post by:
I'm making a foray into trying to create custom vertical scrollbars and sliders, and thought I had a basic idea how to do it, but seem to be having some trouble with the implementation. My...
6
by: Gonzalo Monzón | last post by:
Hi all! I have been translating some Python custom C extension code into Python, as I need these modules to be portable and run on a PocketPC without the need of compile (for the purpose its a...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.