Decimal builtin?

Hello all,

I am curious what people use to represent decimals when with no rounding
error? I have a need for this and have come up against a brick wall. I was
hoping for something that was similar to BigDecimal in Java - does Ruby have
anything like this?

Many thanks in advance for any replies received.


Signed,
Holden Glova

I have had this problem for years until I started use Ruby. Fundamentally,
there are at least 6 number fields: N < Z < D < Q < R < C (natural <
integer < decimal < rational < real < complex). Computers cannot deal with
real and complex numbers => round-off errors. However, Ruby lets you work
with infinite precision Bignum and since decimal numbers are contained
within the rational number field you can safely use rational numbers to
store decimal information. I simply equipped the Rational class with a
#to_s method which writes the number to a string and reads it back again to
check that no information has been lost. The string representation of my
Rational is therefore Fixnum, Bignum, Float or Rational depending on how
many digits are needed.

Tore Haug-Warberg

···

At 17:08 24.06.03 +0900, Holden Glova wrote:

I am curious what people use to represent decimals when with no rounding
error? I have a need for this and have come up against a brick wall. I was
hoping for something that was similar to BigDecimal in Java - does Ruby have
anything like this?

See the following:

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-
8&selm=5.1.0.14.2.20021210134040.02102738%40zcard04k.ca.nortel.com

There are some other relevant threads, but I couldn’t find them.

The most common approach is to do everything in integers with a
sufficiently large scaling factor and convert to decimal for display
purposes. The size of the scaling factor depends on the currency your
working with and the maximum expected amounts. You just figure out
whether you lose any cents above an amount your program might encounter.

Regards,

Mark Wilson

···

On Tuesday, June 24, 2003, at 04:08 AM, Holden Glova wrote:

[snip]

I am curious what people use to represent decimals when with no
rounding
error? I have a need for this and have come up against a brick wall. I
was
hoping for something that was similar to BigDecimal in Java - does
Ruby have
anything like this?
[snip]

Hi!

  • Holden Glova; 2003-06-24, 13:55 UTC:

I am curious what people use to represent decimals when with no
rounding error?

The answer to your question is 355/113 (^_^)

Suppose you want to multiply a and b where

a = 2.718281828
b = 3.141592654

a and b can be written in this way:

a = 2.718281828 = 2718281828 * 1E-9
b = 3.141592654 = 3141592654 * 1E-9

Multiplication is done in that way:

a * b = 2.718281828 * 3.141592654
= 2718281828 * 3141592654 * 1E-18
= 8539734222346491512 * 1E-18
= 8.539734222346491512

Any computation that involves decimals can be done in that way - it
does not work for PI or 1/3 but that’s a different story.

When manually doing multiplications you actually use the above
scheme:

2718281828 * 3141592654 (*)
8154845484
2718281828
10873127312
2718281828
13591409140
24464536452
5436563656
16309690968
13591409140
10873127312

···
8539734222346491512

(*) Actually the decimal points are present but they don’t fit into
this ASCII art.

After the multiplication you count the digits that follow each
decimal point and add them. This gives the position where to put it
in the result.

If you want a more detailed documentation on that kind of arithmetics
all you have to do is finding a good FORTH tutorial.

(^_^) 355.0/113.0 = 3.141592654

When it comes to divisions the problem is slightly more involved.

If a computer does computations that need to be precise to a certain
base 10 number of digits they must be integer computations.

Why that? Because there are decimal numbers with a finite number of
digits that cannot correctly be represented as a binary number with a
finite number of digits. Proof:

ruby -e ‘puts 0.1 * 0.1 - 0.01’
1.734723476e-18

Value may differ from platform to platform (especially if it preceeds
IEEE 754).

Gis,

Josef ‘Jupp’ Schugt

Holden Glova dsafari@paradise.net.nz wrote in message news:200306242007.59017.dsafari@paradise.net.nz

I am curious what people use to represent decimals when with no rounding
error? I have a need for this and have come up against a brick wall. I was
hoping for something that was similar to BigDecimal in Java - does Ruby have
anything like this?

Shigeo Kobayashi’s BigFloat module (and also its successor BigDecimal)
does arbitrary-precision arithmetic, and does it internally in decimal, so
you avoid the “penny math” errors from converting decimal to binary.
It’s useful, though a bit short on advanced functionality (I had
to write my own logarithm method). You can get the BigFloat from
the RAA (http://raa.ruby-lang.org/) and I think BigDecimal is still
sitting somewhere in the Ruby development CVs.

                         Regards, Bret