floating point precision problem

S

syntax

hi,
i need to get high presion float numbers.

say, i need pi = 22/7.0 = 3.142857....(upto 80 digits)

is it possible ?

does gcc/g++ compiler can give such type of high precision??

plz GIVE A SMALL CODE HOW CAN I ACHIEVE THAT ? which way, i have to
write the code??


Question2: AT most how much precision can gcc/g++ AND VC++ Give?

can i write

double double double double x = 22/7.0 ? ... so that it could hold
highly precise result.
 
J

Julian V. Noble

syntax said:
hi,
i need to get high presion float numbers.

say, i need pi = 22/7.0 = 3.142857....(upto 80 digits)

is it possible ?

does gcc/g++ compiler can give such type of high precision??

plz GIVE A SMALL CODE HOW CAN I ACHIEVE THAT ? which way, i have to
write the code??

Question2: AT most how much precision can gcc/g++ AND VC++ Give?

can i write

double double double double x = 22/7.0 ? ... so that it could hold
highly precise result.

As noted previously in this group, several high-precision libraries in
standard C have been written and are available for download.

A Google search of "high precision arithmetic in C" yields

http://crd.lbl.gov/~dhbailey/mpdist/mpdist.html

among about 126,000 hits.

--
Julian V. Noble
Professor Emeritus of Physics
(e-mail address removed)
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"God is not willing to do everything, and thereby take away our free wiil
and that share of glory that rightfully belongs to ourselves."


-- N. Machiavelli, "The Prince".
 
M

Malcolm

syntax said:
i need to get high presion float numbers.
Generally C compilers will restrict themselves to types supported in
hardware, which are usually 32, 64 and sometimes 80 bits.
If the hardware has high-precison doubles, such as 128 bits, the compiler is
likely to provide an extension such as long long double. However such
hardware is not in common use.

A double is enough precision for almost all uses. If you need more precision
you need to emulate floating point in software (libraries are available to
do just that).
If you want to calculate PI to great precison, there are better algorithms
available that don't rely on arbitrary-precison floating point arithmetic.
 
J

jacob navia

syntax said:
hi,
i need to get high presion float numbers.

say, i need pi = 22/7.0 = 3.142857....(upto 80 digits)

is it possible ?

does gcc/g++ compiler can give such type of high precision??

plz GIVE A SMALL CODE HOW CAN I ACHIEVE THAT ? which way, i have to
write the code??


Question2: AT most how much precision can gcc/g++ AND VC++ Give?

can i write

double double double double x = 22/7.0 ? ... so that it could hold
highly precise result.

Use lcc-win32.
For instance:
#include <qfloat.h>
int main(void)
{
qfloat p = 2;
printf("%1.80qf\n",sqrt(p));
return 0;
}
will produce:
1.41421356237309504880168872420969807856967187537694807317667973799073
247846210704
Qfloats have a precision of 350 bits, approx 100 digits.

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

P.J. Plauger

Generally C compilers will restrict themselves to types supported in
hardware, which are usually 32, 64 and sometimes 80 bits.
If the hardware has high-precison doubles, such as 128 bits, the compiler is
likely to provide an extension such as long long double. However such
hardware is not in common use.

Sun Sparcs are in pretty common use. They have 128-bit floating-point
format. And every compiler I know calls it long double.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
S

Sidney Cadot

syntax said:
hi,
i need to get high presion float numbers.

Could you give some background about what you need these for? Chances
are, your reasons for wanting this are not sound.
say, i need pi = 22/7.0 = 3.142857....(upto 80 digits)

is it possible ?

Yes, but not in standard C using any compiler I am aware of.
does gcc/g++ compiler can give such type of high precision??

C (and C++) compilers provide floating-point types that generally map
onto types supported by the hardware, so you should not be asking about
a particular compiler, but about a particular hardware platform.

I do not know any hardware platform that supports over 128 bits of
precision for floating point values.
plz GIVE A SMALL CODE HOW CAN I ACHIEVE THAT ? which way, i have to
write the code??

PLEASE DON'T SHOUT. Thank you.

In standard C, you can't. You might want to look at add-on libraries
(e.g., GMP, the GNU Multi-Precision Library). Or perhaps you don't even
want C ("bc" is nice for small high-precision numerical algorithms).
Question2: AT most how much precision can gcc/g++ AND VC++ Give?

Depends on the hardware. Usual sizes for floating point types are 32,
64, 80, and 128 bits. This is a long way short of the 80 digits you
claim you need.
can i write

double double double double x = 22/7.0 ? ... so that it could hold
highly precise result.

No.

Best regards,

Sidney
 
K

Keith Thompson

hi,
i need to get high presion float numbers.

say, i need pi = 22/7.0 = 3.142857....(upto 80 digits)

is it possible ?

<OT>
I hope you're aware that 22/7.0, however precisely you compute it, is
not a very accurate representation of the mathematical constant pi.
22/7.0 is 3.(142857)* (that's a repeating decimal, 3.142857142857...);
pi is 3.1415926535.... (The notation for a repeating decimal is
something I just made up.)
Question2: AT most how much precision can gcc/g++ AND VC++ Give?

can i write

double double double double x = 22/7.0 ? ... so that it could hold
highly precise result.

There are only three predefined floating-point types in standard C:
float, double, and long double. A compiler could provide, say,
"long long double" as an extension, but I don't know of any system
with direct hardware support for more than three sizes. Another
poster mentioned a "qfloat" type provided by one particular compiler,
but of course that's non-portable (which isn't necessarily a reason
not to use it).

If you want a more portable solution, there are a number of
extended-precision numeric libraries floating (sic) around. Probably
the best known is gmp, the GNU MP library, available from gnu.org.
 
M

Mark McIntyre

Sun Sparcs are in pretty common use.

for some definitions of common !
I've not seen too many of them in kids bedrooms, internet cafes, etc.
They have 128-bit floating-point
format. And every compiler I know calls it long double.

Doesn't intel have a similar longish double? Hardware specific
question of course, offtopic here.
 
C

CBFalconer

Keith said:
<OT>
I hope you're aware that 22/7.0, however precisely you compute it, is
not a very accurate representation of the mathematical constant pi.
22/7.0 is 3.(142857)* (that's a repeating decimal, 3.142857142857...);
pi is 3.1415926535.... (The notation for a repeating decimal is
something I just made up.)
</OT>

No it isn't (just made up). AGMTA. However pi is not a repeating
decimal :) Some of the best rational approximations I know for
pi are:

22 / 7
355 / 113
833719 / 265381
1146408 / 364913
4272943 / 1360120
5419351 / 1725033
63885804 / 20335483
74724506 / 23785549
80132847 / 25510582

with the error decreasing by about a factor of 4 or more for
those. They assume a correct value is 3.141492653589793.
 
S

syntax

thanks jacob navia

win lcc-win32 is working....

some of you are suggesting to use GMP library. i dont know even
whetehr my compiler has that library..my compiler is gcc/g++ 2.98
version.

in order to see whether GMP is there
i simply wrote $ ls /usr/lib/
and i found something like this (along with other libraries)

libgmp.a libgmp.so
libgmp.so.3
libgmp.so.3.1.1

do you think my compiler has GMP support?



if so can you plz give me code to calculate sqrt(2) upto 80
digits??.. so that i am be convinced that my GMP library works well.



i am working through telnet..Red Hat linux.7.0.1
does Higher version of linux( Red hat linux 9 ) supports GMP library
in-built??
 
T

Tim Prince

Mark McIntyre said:
for some definitions of common !
I've not seen too many of them in kids bedrooms, internet cafes, etc.


Doesn't intel have a similar longish double? Hardware specific
question of course, offtopic here.
Itanium supports a possible 128-bit long double IEEE format by combinations
of operations. The native long double supported by single
add/subtract/multiply operations (no divide) is 80 bits, as stored in
memory, plus 2 bits in register to protect against range overflow. As
measured by sizeof, the 80 bit long double may take up 128 bits in memory,
when padded for proper address alignment.
Some people claim that the parallel data types (4 floats or 2 doubles)
constitute a 128-bit type when thinking of AMD, but not when thinking of
Intel, where they came from.
 
K

Keith Thompson

CBFalconer said:
No it isn't (just made up). AGMTA.
"AGMTA"?

However pi is not a repeating decimal :)

Right, but the OP said that pi = 22/7.0, so maybe he's not talking
about the usual mathematical constant.
 
K

Keith Thompson

Please don't top-post. The convention here is to write your response
following any quoted text, and to delete anything not relevant to your
response.

A: Because it makes it more difficult to follow the discussion.
Q: Why is top-posting bad?

extended-precision numeric libraries floating (sic) around. Probably
the best known is gmp, the GNU MP library, available from gnu.org.
thanks jacob navia

win lcc-win32 is working....

some of you are suggesting to use GMP library. i dont know even
whetehr my compiler has that library..my compiler is gcc/g++ 2.98
version. [...]
do you think my compiler has GMP support?

Probably, but the question isn't topical here, since it really isn't a
question about the C programming language. (Incidentally, gmp is a
library, so it isn't tied to the compiler; even if you don't have it,
you can download it, install it, and use it with your compiler;
instructions for doing so are also off-topic.)

If your system has gmp, it probably has documentation for it.

<OT>Try "info gmp".</OT>
 
R

Rob Thorpe

thanks jacob navia

win lcc-win32 is working....

some of you are suggesting to use GMP library. i dont know even
whetehr my compiler has that library..my compiler is gcc/g++ 2.98
version.

in order to see whether GMP is there
i simply wrote $ ls /usr/lib/
and i found something like this (along with other libraries)

libgmp.a libgmp.so
libgmp.so.3
libgmp.so.3.1.1

do you think my compiler has GMP support?

Yes. But this is getting out of the realms of being a C question.

#include <gmp.h>

compile with gcc -lgmp

read the manual
http://www.swox.com/gmp/manual/

try it, and direct questions to (e-mail address removed)
 
M

Malcolm

syntax said:
if so can you plz give me code to calculate sqrt(2) upto 80
digits??.. so that i am be convinced that my GMP library works well.
I'll tell you how to do it (the accepted method is a bit more sophisticated
than this, but this will work and you can understand it.

Have two floating-point variables, low, set to zero, and high, set to 2.0.
Obviously the square root of two must be between them.

Let's make a guess - halfway between (ie 1.0). Test by multiplying 1.0 by
1.0, and you will get the answer 1.0. This is below 2, so set low to 1.0.
Now make another guess - 1.5. 1.5 * 1.5 is above 2.0, so set high to 1.5. We
have now narrowed the root down to between 1.0 and 1.5.

Continue doing this until high and low are the same.

Actually there is a technical difficulty here in that it is just possible
that high and low may always be 1 bit off because of the way your
floating-point package works. So you could terminate when the difference
goes to less than some tiny value.
 
M

Malcolm

P.J. Plauger said:
Sun Sparcs are in pretty common use. They have 128-bit floating-
point format. And every compiler I know calls it long double.
So what do they use for 80 bits?
 
P

Peter Nilsson

Keith Thompson said:

I've seen it used as: Acting General Manager's Travel Allowance.

Perhaps it means Astute Googling Manifests Technical Answers?

Sadly, all I could find was a reference to All Goa Milk Traders Association.
Jayant Katkar was elected President by the way... <g>
 
T

Tim Prince

Malcolm said:
So what do they use for 80 bits?
I don't think anyone has been motivated to tackle the support of 2 different
long double floating point types in a C compiler. Platforms which could
support that somewhat efficiently (e.g. Itanium) are rare enough that it
surely wouldn't be portable.
C allows long double to be anything from the same as double on up, with no
mandate for it for it to resemble one of the optional types defined by IEEE
standards.
Microsoft appears to be ruling out entirely the possibility of long double
differing from double in a large part of their sphere of influence.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top