473,804 Members | 4,128 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sqrt precision ?!

Hello!

How I can make SQRT(2) with 20 digits precision (after float point)?
Nov 14 '05 #1
11 4646
Marcin wrote:
Hello!

How I can make SQRT(2) with 20 digits precision (after float point)?


You probably mean sqrt(2.0) or sqrtl(2.0L); if DBL_DIG or LDBL_DIG is
smaller than 21 (1.[20Digits] makes 21 digits), you can either look
for a large floating point implementation, write one yourself or
store the value in a string.
#include <math.h>
#include <float.h>
#include <stdio.h>

int main (void)
{
long double r;

r = sqrtl(2.0L);
printf("Precisi on: %d digits; %.*Lg\n",LDBL_D IG,LDBL_DIG,r);

return 0;
}

I am not sure whether C89 standard libraries provide sqrtl()
(in this case, you would have to go for double r, sqrt(2.0), %.*g and
DBL_DIG).

Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #2
Marcin wrote:
Hello!

How I can make SQRT(2) with 20 digits precision (after float point)?


Newtons algorithm with double precision floating point, if the inbuilt one
is insufficient.

gtoomey
Nov 14 '05 #3
Gregory Toomey wrote:
Marcin wrote:
Hello!

How I can make SQRT(2) with 20 digits precision (after float point)?

Newtons algorithm with double precision floating point, if the inbuilt one
is insufficient.


The problem is that the OP wants 21 (decimal) digits precision which
implies that we need 70 binary digits precision.
This in turn means that you average system's double will not suffice.
If you are lucky, long double will do. Otherwise, you will have to
use either non-standard stuff or make your own floating point data type.
Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #4
Marcin wrote:

Hello!

How I can make SQRT(2) with 20 digits precision (after float point)?


/* sq_rtl will get you all of the pecision of type long double. */

#include <errno.h>
#include <math.h>

long double sq_rtl(long double x);

long double sq_rtl(long double x)
{
long n;
long double a, b;

if (x > 0) {
for (n = 0; x > 2; x /= 4) {
++n;
}
while (0.5 > x) {
--n;
x *= 4;
}
a = x;
b = (1 + x) / 2;
do {
x = b;
b = (a / x + x) / 2;
} while (x > b);
while (n > 0) {
x *= 2;
--n;
}
while (0 > n) {
x /= 2;
++n;
}
} else {
if (0 > x) {
errno = EDOM;
x = -HUGE_VAL;
}
}
return x;
}

/* This wasn't a question about how to use printf, was it? */

--
pete
Nov 14 '05 #5
Marcin wrote:
Hello!

How I can make SQRT(2) with 20 digits precision (after float point)?


Lcc-win32 offers a higher precision floating point
(100 digits precision)

Using that you can write:

#include <stdio.h>
#include <qfloat.h>
int main(void)
{
qfloat q = 2;
qfloat r = sqrt(q);
printf("%80.70q f\n",r);
}

Result:

1.4142135623730 950488016887242 096980785696718 753769480731766 797379907325

http://www.cs.virginia.edu/~lcc-win32
Nov 14 '05 #6
On 27 Jan 2005 01:55:11 -0800, Marcin
<mj***@pf.pl> wrote:
How I can make SQRT(2) with 20 digits precision (after float point)?


If your platform doesn't allow 21 digits of precision (x86 long double
is only 19 digits) then you will have to do it either with implementing
your own "big number" routines or use a large precision library like
gmp.

<OT>
When I was at school we had mechanical adding machines which were
cranked with a handle, with 10 digits plus overflow on each. We once
calculated sqrt(2) to 100 places, using 10 machines and doing the
'carry' manually from one to the next, it took us around 10 weeks of
lunch breaks. We called that 'fun'...

But it did teach me the techniques for doing large precision arithmetic
using smaller registers, which has been valuable since. Do they still
even teach "long division" in schools?
</OT>

Chris C
Nov 14 '05 #7
But it did teach me the techniques for doing large precision arithmetic using smaller registers, which has been valuable since. Do they still even teach "long division" in schools?


Surre they show the kids how to push the correct button on the
calculator.

Nov 14 '05 #8
jacob navia wrote:
Marcin wrote:

How I can make SQRT(2) with 20 digits precision (after float point)?


Lcc-win32 offers a higher precision floating point
(100 digits precision)

Using that you can write:

#include <stdio.h>
#include <qfloat.h>
int main(void)
{
qfloat q = 2;
qfloat r = sqrt(q);
printf("%80.70q f\n",r);
}

Result:

1.4142135623730 950488016887242 096980785696718 753769480731766 797379907325

.....1.41421356 237309504880168 872420969807856 967187537694807 317667973799073 247846210703885 038753432764157 27

Result from bc. Seems to agree.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #9
Chris Croughton wrote:
.... snip ...
But it did teach me the techniques for doing large precision
arithmetic using smaller registers, which has been valuable since.
Do they still even teach "long division" in schools?


I wonder if they teach addition. I can wow most of the present
generation by simply adding a column of numbers. Meanwhile they
are making entry errors on a 4 function calculator.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #10

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

Similar topics

10
7328
by: pauldepstein | last post by:
I am writing a program which will use the ceiling and floor of square roots. If a positive integer is an exact square (and is not overly huge), does sqrt return an integer-valued real? Or might the sqrt function tell me that, for example, sqrt(1024) = 31.99999999999999999999999 You might say "try it". The problem is that I can't try infinitely
1
310
by: Marcin | last post by:
> Hello! > > How I can make SQRT(2) with 20 digits precision (after float point)? But I have to write this without additional header files, and lib's :(
13
1692
by: Marcin | last post by:
But I must use only stadnard C!
9
1759
by: Keith | last post by:
The bits get twiddled every now and then. Using gcc 3.2 and 3.4. To build: cc -o test test.c -lm ---------------- 8< test.c ------------------------ #include <stdio.h> #include <math.h> double func(double);
13
3778
by: Michael McNeil Forbes | last post by:
I would like to write a module that provides some mathematical functions on top of those defined in math, cmath etc. but I would like to make it work with "any" type that overloads the math functions. Thus, I would like to write: module_f.py ---- def f(x): """ Compute sqrt of x """
13
4972
by: siggi | last post by:
Hi all, this is a newbie question on : Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) on win32 PC with WinXP In http://www.python.org/doc/2.3.5/lib/module-math.html I read:
17
3204
by: pyramid | last post by:
Hello I am working on one of my lab for this week, which calculates the approximate value of pi. Listed below is the actual problem, which I am posting here, so that you can see the different requirements for the program. The program is simple. I used the ‘for loop’ to define the number of increments, then set up a formula to calculate the pi. My codes compile with no problem, but the result is incorrect. One of the test data I...
30
4049
by: copx | last post by:
I am writing a program which uses sqrt() a lot. A historically very slow function but I read on CPUs with SSE support this is actually fast. Problem: C's sqrt() (unlike C++ sqrt()) is defined to work on doubles only, and early versions of SSE did not support double precision. The CPU requirement "something with at least first generation SEE support" (everything from P3/Athlon XP and up) is acceptable for me. However, requiring a CPU which...
13
10556
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, Why does Math.Sqrt() only accept a double as a parameter? I would think it would be just as happy with a decimal (or int, or float, or ....). I can easily convert back and forth, but I am interested in what is going on behind the scenes and if there is some aspect of decimals that keep them from being used in this calculation. Thanks! Ethan Ethan Strauss Ph.D.
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10332
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10077
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6853
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.