# how do I get more numbers past the decimal? - Page 4

•  Subject
• Author
• Posted on

## Re: how do I get more numbers past the decimal?

Doug Miller wrote:

Yea, sorry - I started out to say one thing, but changed it.  Didn't get
it all changed, though :-)

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: how do I get more numbers past the decimal?

There are a variety of alternatives to 32-bit floating point where
more precision is needed, depending on the application:
- 64-bit IEEE doubles
- 80-bit IEEE long doubles (but some compilers do not implement this, and
use 64-bit doubles for the type "long double".
- 64-bit integers
- much bigger than 64-bit integers

IEEE 32-bit floats have so little precision (7 digits), you really
need an excuse to justify not using doubles (or better) everywhere.
An IEEE float is OK for computing completion percentage (e.g. "16.1%
complete".)  Not a lot else.  You don't gain much speed using floats.
You do gain storage.  C tends to promote floats to doubles, do the
calculations, and then demote to float again, and there aren't even
any float (vs. double) trig functions in C90.

## Re: how do I get more numbers past the decimal?

Try something like:
<?php
\$a = 1.0/3.0;
printf("%70.40g\n", \$a);
?>

which on my system (AMD Athalon) comes out as:

0.3333333333333333148296162562473909929395

I'm getting 16 digits correct, which suggests that PHP is using
IEEE doubles (64 bit, 15 digits), not IEEE floats (32 bit, 7 digits)
and not IEEE long doubles (on some platforms, 80 bit, 18 digits).

significant digits, but the actual value shouldn't be.

The value of the numbers is kept at the machine precision, which
is at least on my system with IEEE floating-point hardware, 15
digits.  Calculations such as subtracting near-equal numbers can
reduce the precision you actually have significantly, or to absolutely
nothing.

For purposes other than testing floating point precision, you
shouldn't print so many digits because you won't have that many
significant digits, and the stuff beyond that is garbage.  1.0/3.0
in exact math is supposed to come out
0.3333333333333333333333333333333333333333  but you see above what
PHP (well, really, it's the CPU hardware on my system) does to it.
Rarely do you have 12 significant digits in the result of a
calculation, even if you think you *need* that much.

Note that most decimal numbers cannot be represented exactly in binary
floating point.  0.1 gets mangled just as badly as 1.0/3.0 above.

## Re: how do I get more numbers past the decimal?

On Aug 1, 6:37=A0pm, gordonb.ub...@burditt.org (Gordon Burditt) wrote:

That is very clever. I like this trick.

Anyway, I tried it and I got the same as you:

0.3333333333333333148296162562473909929395

## Re: how do I get more numbers past the decimal?

On Aug 1, 6:37=A0pm, gordonb.ub...@burditt.org (Gordon Burditt) wrote:

That is interesting about the display value being shortened. When
you've got a calculation that keeps looping, thousands of times, and
the value of the last loop is fed in as the starting point of the next
iteration, then what is value of the variable that is actually used?
The 12 digits of the display value? The 15 digits of the IEEE floating-
point hardware?

## Re: how do I get more numbers past the decimal?

Printing a variable doesn't change its value, unless you do something
like printing its value with limited decimal places, then read it
back in (I have seen that technique used for quick-and-dirty rounding,
but it's nearly impossible to do this sort of thing *by accident*).

Print the value to as many decimal places as you want using printf()
(PHP doesn't seem to allow more than 40, but you won't get anywhere
near that anyway) to see what's really in it.

That value can be changed in php.ini, but I recommend simply
formatting the value with printf() to however many places you want.
Most people use printf() to *REDUCE* the number of decimal places
printed (e.g. limiting the value to whole cents) because they know
the extra digits are just garbage, and printing a bill with
BALANCE DUE: \$0.0000726363548594 is downright silly.

Test it yourself.  Compute a number with a known repeating decimal
expansions (I used 1.0/3.0 as an example), print it with 40 digits
of precision, and see how many digits you get accurately.  Try it
with several fractions.  I think you'll get the hardware precision.

You *ALWAYS* need to worry about whether you've got enough precision
in floating point formulas.  In Chaos Theory, you may get serious
differences using one billion digits vs. using one billion and one
digits.

## Re: how do I get more numbers past the decimal?

On Aug 6, 12:42=A0am, gordonb.rh...@burditt.org (Gordon Burditt) wrote:

I appreciate your advice. What I get from this, and from the rest of
the thread, is that PHP is as good as any other language I might use
for exploring this subject. The important questions have more to do
with the math, and its implications, than with the mechanics of PHP
versus some other computer language. All the high level languages, I
gather, are simply masking the same underlying dynamics, and worrying
about the precision of those underlying dynamics is something I'll
face in any just about any language I might use.

## Re: how do I get more numbers past the decimal?

On Aug 1, 6:37=A0pm, gordonb.ub...@burditt.org (Gordon Burditt) wrote:

Sure, okay. I suppose that is why there are some big math packages
that specialize in this. I assume they try to ameliorate some of the
mangling that goes on.

## Re: how do I get more numbers past the decimal?

That's one reason.  Some of them deal with problems that wouldn't be
done in floating point anyway, like very-large-integer calculations
used in RSA encryption.

Note that your input data often doesn't come anywhere near close
to having 15 digits of precision, so the mangling is hidden by the