473,396 Members | 1,998 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.

Sqrt precision ?!

Hello!

How I can make SQRT(2) with 20 digits precision (after float point)?
Nov 14 '05 #1
11 4594
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("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
--
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.70qf\n",r);
}

Result:

1.414213562373095048801688724209698078569671875376 9480731766797379907325

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

Result:

1.414213562373095048801688724209698078569671875376 9480731766797379907325

.....1.4142135623730950488016887242096980785696718 75376948073176679737990732478462107038850387534327 6415727

Result from bc. Seems to agree.

--
"If you want to post a followup via groups.google.com, 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.com, 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
Marcin wrote:
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.
Nov 14 '05 #11
CBFalconer wrote:
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.70qf\n",r);
}

Result:

1.414213562373095048801688724209698078569671875376 9480731766797379907325


....1.41421356237309504880168872420969807856967187 53769480731766797379907324784621070388503875343276 415727

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.

Nov 14 '05 #12

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

Similar topics

10
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...
1
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
by: Marcin | last post by:
But I must use only stadnard C!
9
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> ...
13
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...
13
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
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...
30
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...
13
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...
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?
0
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,...
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
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.