473,396 Members | 2,002 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,396 software developers and data experts.

Square root program doesn't produce the right value

Can you help me? For 4, my square root funct gives 4 instead of 2;
here's the code:

#include <iostream>
#include <cstdlib>
using namespace std;

template<class T>
T Abs(T Nbr)
{
if( Nbr >= 0 )
return Nbr;
return -Nbr;
}

template<class T>
T Sqrt(T Nbr)
{
long double Number = Nbr / 2;
const long double Tolerance = 1.0e-7;
do Number = (Number + Nbr / Number) / 2;
while( Abs(Number * Number - Nbr) > Tolerance);
return Number;
}

int main()
{
cout << "Enter a number: " << endl;
long double num;
cin >> num;
cout << "Sqrt(" << num << ")= " << Sqrt(num) << endl;
system("PAUSE");
return 0;
}

Can you help me? Thanks.

Nov 10 '05 #1
2 3162
Protoman wrote:
Can you help me? For 4, my square root funct gives 4 instead of 2;
here's the code:

#include <iostream>
#include <cstdlib>
using namespace std;

template<class T>
T Abs(T Nbr)
{
if( Nbr >= 0 )
return Nbr;
return -Nbr;
}

template<class T>
T Sqrt(T Nbr)
{
long double Number = Nbr / 2;
const long double Tolerance = 1.0e-7;
do Number = (Number + Nbr / Number) / 2;
while( Abs(Number * Number - Nbr) > Tolerance);
return Number;
}

int main()
{
cout << "Enter a number: " << endl;
long double num;
cin >> num;
cout << "Sqrt(" << num << ")= " << Sqrt(num) << endl;
system("PAUSE");
return 0;
}

Can you help me? Thanks.


Hm, on my machine, the program computes "Sqrt(4)= 2" just fine. The real
problem is "Sqrt(0)= nan".

Here is a fix for that:

template<class T>
T Sqrt(T Nbr)
{
long double Number = Nbr / 2;
const long double Tolerance = 1.0e-7;
while( Abs(Number * Number - Nbr) > Tolerance) {
Number = (Number + Nbr / Number) / 2;
}
return Number;
}

Best

Kai-Uwe Bux
Nov 10 '05 #2
Protoman wrote:

Can you help me? For 4, my square root funct gives 4 instead of 2;
here's the code:


What's wrong with fireing up your debugger and stepping through the code
to see why it does that?

Figuring out problems in code and why it doesn't do what it should do
is *vital* in becomming a programmer. So start early, develop your
skills and learn how to use your tools.
--
Karl Heinz Buchegger
kb******@gascad.at
Nov 10 '05 #3

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

Similar topics

6
by: user | last post by:
when i first load index.php with arguments ie: "index.php?page=x", the $_SERVERvalue is null. I then get the session id appended to each link. If i refresh the page, I get the $_SERVER I want, but...
4
by: cplusplus | last post by:
Hello, I have newbie question. I'm stuck on this current assignment. Write a program that prompts the user for two integer values, passes the values to a function where they are multiplied...
6
by: Protoman | last post by:
I'm trying to write an encryption program, and it doesn't produce the right output; "GOOGLE" encrypted w/"GOOGLE" IS NOT "ee". Here's the code: namespace { const char vTable= {...
2
by: Clint Olsen | last post by:
Hello: I posted a thread on comp.programming awhile back asking about an algorithm I implemented on square root. The idea was to use the square root of a prime number as a convenient way to get...
4
by: Peter | last post by:
Hi I am using DateTime class and TimeOfDay.Hours attribute to return current time. It returns right value on two servers, but in one particular server it does not return right time. I checked that...
14
by: Tom.PesterDELETETHISSS | last post by:
Hi, I think this question requires an in depth understanding of how a browser cache works. I hope I can reach an expert here. I may have found a quirk in the asp.net documentation or I don't...
7
by: Robert | last post by:
I have the function below. it returns a "simpleresult" which I've also included the definition of below. In VS2005 (after upgrading the project), I get a warning indicating that Function...
32
by: priyam.trivedi | last post by:
Hi! Could anyone tell me how to find the square root of a number without using the sqrt function. I did it by using Newton's Formula. How can it be done by using the Binomial Theorem/Taylor...
2
by: muler | last post by:
Hi all, This is an excerpt from the Book "Programming Microsoft Visual C#: The Language": A function evaluates to the return value. When a reference type is returned, a function is available...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.