Re: FAQ 4.2 Why is int() broken?



Peter J. Holzer wrote:
On 2008-07-28 06:12, Jürgen Exner <jurgenex@xxxxxxxxxxx> wrote:
"szr" <szrRE@xxxxxxxxxxxxxxx> wrote:
Peter J. Holzer wrote:
Please get it into your head that extra precision *does* *not*
solve this problem. To express 1/10 in binary you need an infinite
number of digits, just like you need an infinite number of digits
to express 1/3 in decimal.

Maybe you meant to write something else, as 1/10 is 0.1 and does not
require an infinite number of digits;

Yes, it does.

It doesn't, but I didn't write that. I wrote it takes an infinite
number of *binary* digits.

In binary, 1/10 is 0.00011001100110011...


For some reason I thought that applied to decimal expresses that
repeated infinately, like 1/3 => .3333..., not decimals with a fixed
number of dibits, which 1/0 => 0.1 is:

$ perl -e 'my $x = 1/10; print unpack("b64", pack("d", $x)), "\n"'
0101100110011001100110011001100110011001100110011001110111111100

$ perl -e 'my $x = 1/2; print unpack("b64", pack("d", $x)), "\n"'
0000000000000000000000000000000000000000000000000000011111111100

$ perl -e 'my $x = 1/4; print unpack("b64", pack("d", $x)), "\n"'
0000000000000000000000000000000000000000000000000000101111111100

$ perl -e 'my $x = 1/8; print unpack("b64", pack("d", $x)), "\n"'
0000000000000000000000000000000000000000000000000000001111111100

I guess you never know when something you've over looked or something
you thought you knew can surprise you. :-)

it needs just one decimal digit

Irrelevant because your typical computer does not use decimal numbers
but binary numbers, just like Peter said.

Actually, I didn't write what "a typical computer" uses, just what
happens when a binary system is used (which is what perl uses on most
(all?) platforms - COBOL uses normally uses decimal).

Even among different types of computers floating point calculations are
not all done the same. For instance, I have a graphing calculator, which
is essentially tiny computer with a 6 MHz cpu and yet it can do many
floating point calculations more accurately than my dual core cpu
desktop. Granted, it's a calculator, but I have often wondered why it
seems to be able to handle such calculations better than a CPU that's
over 400 times faster.

Secondly, for expressions like 1/3, yes, the floating point
(decimal) form cannot be fully expressed in binary. However, in the
form of a rational number, it can be stored in an accurate binary
form,

No, it cannot. Neither as a decimal number nor as a binary number.

Actually it can, at least if you translate the mathematical definition
of a rational number into the straightforward implementation of
storing two integers: (1, 3) in this case.


Yes this is what I meant.

as you're
really storing integers - the numerator and the denominator -

That is not a number but an expresssion. Mathematically they may be
the same. However for actual computations they are not as any
introduction to Computer Numerics will tell you.

It isn't much different from a floating point number, which is also
stored as two integers (m, e) where the value is m*2^(e-b).


Good to know.

although I'm not sure if that would be faster or slower than actual
floating point calculations; I've never done the numbers, and I'm
sure it may vary depending on the processor, but I have seen
libraries,
such as in C or C++, that use rational instead of floating point
numbers.

There is also one in Perl and I already mentioned it: Math::BigRat
resp. bigrat. That almost certainly is slow, because it not only uses
rational numbers, it also uses arbitrary precision. (But I've never
really used it so I don't know).

Ah yes, I forgot about that one.

If what you're after is accurate math, than representing your
numbers as rational (n/d) seems to be a rather trivial way of
achieving that; the math part most people learned in basic
arithmetic. I would think the hardest part would be writing an
efficient reduction algorithm, to, for example, turn 10/18 into 5/9.

Smallest common demoninator, rather simple.

I said it would have been the hardest part, not that it was actually
difficult :-)

Yup, was probably my third or fourth BASIC program. Used as an example
to introduce GOTO ;-).

I think we've all had t owrite such algorithums at some point :-)

--
szr


.



Relevant Pages

  • Re: FAQ 4.2 Why is int() broken?
    ... To express 1/10 in binary you need an infinite ... number of digits, just like you need an infinite number of digits ... Even among different types of computers floating point calculations are ... is essentially tiny computer with a 6 MHz cpu and yet it can do many ...
    (comp.lang.perl.misc)
  • Re: FAQ 4.2 Why is int() broken?
    ... digits, just like you need an infinite number of digits to express 1/3 ... Irrelevant because your typical computer does not use decimal numbers ... It isn't much different from a floating point number, ...
    (comp.lang.perl.misc)
  • Re: Rounding errors
    ... >>number of random digits following the point of rounding, ... > an infinite number of digits right of decimal. ... multiplication nor what the number of digits in the calculations was. ...
    (comp.lang.cobol)
  • Re: Rounding issue
    ... Floating point numbers are stored as binary numbers. ... with a limited number of digits, ... denominator being the product of a power of 2 and a power ... And the more calculations you do the larger the error can ...
    (comp.lang.perl.misc)
  • Re: Precision
    ... Randy Poe wrote: ... >> What I want is to be able to do floating point calculations on ... >> that are 24 digits long. ...
    (comp.soft-sys.matlab)