Sqrt precision ?!

M

Michael Mair

Marcin said:
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("Precision: %d digits; %.*Lg\n",LDBL_DIG,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
 
G

Gregory Toomey

Marcin said:
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
 
M

Michael Mair

Gregory said:
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
 
P

pete

Marcin said:
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? */
 
J

jacob navia

Marcin said:
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.70qf\n",r);
}

Result:

1.4142135623730950488016887242096980785696718753769480731766797379907325

http://www.cs.virginia.edu/~lcc-win32
 
C

Chris Croughton

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
 
G

gooch

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.
 
C

CBFalconer

jacob said:
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.70qf\n",r);
}

Result:

1.4142135623730950488016887242096980785696718753769480731766797379907325
.....1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727

Result from bc. Seems to agree.
 
C

CBFalconer

Chris said:
.... 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.
 
M

Martin Ambuhl

Marcin said:
Hello!

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

By using an implementation for which DBL_DIG >= 21 or by using one of
the freely availble extended precision libraries. Learn to use a search
engine.
 
J

jacob navia

CBFalconer said:
....1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727

Result from bc. Seems to agree.
And is correctly rounded, as bc confirms.

I had some problems with rounding that I corrected some weeks ago.

This is one of the "standard extensions" that the
standard proposes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top