I've been following this thread and am wondering if there are other
numerical
classes for Ruby other than BigDecimal?
Well, I mentioned Rational. There's also Bignum, but that's
transparent, so it doesn't need to be called explicitly as BigDecimal
does. Check the standard library docs.
Popcorn, well, it's kinda hard to transmit over the wire.
As a rule of thumb, if you really care about the decimals, either use BigDecimal or integers (and keep track of where the decimal should be -- this is common for $$$$). Unfortunately, this is not limited to ruby, either -- C, Java, and a host of other languages all are subject.
I've been following this thread and am wondering if there are other numerical
classes for Ruby other than BigDecimal?
Well, I mentioned Rational. There's also Bignum, but that's transparent, so it doesn't need to be called explicitly as BigDecimal does. Check the standard library docs.
Were you looking for something specific?
Hi Marnen,
thanks for the reply. I am aware of Bignum and understand how Ruby does the translation transparently from Fixnum. I was just seeking more knowledge outside my current understanding of Ruby.
I had imagined there might be a class Currency =), again just wanting to know what's available to me through Ruby.
Popcorn, well, it's kinda hard to transmit over the wire.
As a rule of thumb, if you really care about the decimals, either use
BigDecimal or integers (and keep track of where the decimal should be --
this is common for $$$$). Unfortunately, this is not limited to ruby,
either -- C, Java, and a host of other languages all are subject.
Matt
Thanks. Both my head and the wall are safe for now. As one may have
surmised, I don't deal with floating point values much, but this is
frustrating. I do see that there are a couple of people who wrote gems
on Rubyforge to handle this issue
--
Posted via http://www.ruby-forum.com/\.
Popcorn, well, it's kinda hard to transmit over the wire.
Easy to do with a modern email client - just needs support for POP3 and a working firewall (for the heat).
As a rule of thumb, if you really care about the decimals, either use BigDecimal or integers (and keep track of where the decimal should be -- this is common for $$$$). Unfortunately, this is not limited to ruby, either -- C, Java, and a host of other languages all are subject.
Absolutely: this is a common issue in *all* programming languages which are not systems for symbolic math (like Mathematica) because they do not work with real numbers but just rational numbers.
Popcorn, well, it's kinda hard to transmit over the wire.
Easy to do with a modern email client - just needs support for POP3 and
a working firewall (for the heat).
LOL!
As a rule of thumb, if you really care about the decimals, either use
BigDecimal or integers (and keep track of where the decimal should be --
this is common for $$$$). Unfortunately, this is not limited to ruby,
either -- C, Java, and a host of other languages all are subject.
Absolutely: this is a common issue in *all* programming languages which
are not systems for symbolic math (like Mathematica) because they do not
work with real numbers but just rational numbers.
That is not the issue here -- after all, BigDecimal does precise
arithmetic, but only with rational numbers. The issue is rather that
IEEE 754 does an inadequate job of representing arbitrary rational
numbers, and the small errors are accumulated and magnified in
calculations.
I'd like to emphasize the fact that it is a very specific
representation problem that most often leads to a thread such
as this. That problem is a misunderstanding about the nature
of converting between a base 10 literal and a base 2 internal
value.
Many people don't realize that floating point literals written
in base 10 (such as 123.6) may not have an exact finite
representation when converted to base 2 and similarly a finite
base 2 floating point value may not have a finite representation
in base 10.
In the original post the floating point subtraction in the
expression (123.6 - 123.0) is handled just fine. The problem
is that 123.6 can't be represented exactly as a base 2 floating
point value so the subtraction that actually gets done is
On Oct 28, 2009, at 3:30 PM, Marnen Laibow-Koser wrote:
That is not the issue here -- after all, BigDecimal does precise
arithmetic, but only with rational numbers. The issue is rather that
IEEE 754 does an inadequate job of representing arbitrary rational
numbers, and the small errors are accumulated and magnified in
calculations.
Popcorn, well, it's kinda hard to transmit over the wire.
Easy to do with a modern email client - just needs support for POP3 and
a working firewall (for the heat).
LOL!
As a rule of thumb, if you really care about the decimals, either use
BigDecimal or integers (and keep track of where the decimal should be --
this is common for $$$$). Unfortunately, this is not limited to ruby,
either -- C, Java, and a host of other languages all are subject.
Absolutely: this is a common issue in *all* programming languages which
are not systems for symbolic math (like Mathematica) because they do not
work with real numbers but just rational numbers.
That is not the issue here -- after all, BigDecimal does precise
arithmetic, but only with rational numbers.
BigDecimal actually works with decimal numbers, which are a subset of
rational numbers; Rational does precise math with rational numbers.
The issue is rather that IEEE 754 does an inadequate job of representing
arbitrary rational numbers, and the small errors are accumulated and
magnified in calculations.
The bigger issue is that Ruby -- like most general purpose programming
languages, though there are exceptions like Scheme -- makes IEEE 754
floating point the easiest non-integer data type to use, rather than
using exact numbers by default and using inexact numbers only when
explicitly called for (or when an operation that produces inexact
results is used.)
···
On Wed, Oct 28, 2009 at 12:30 PM, Marnen Laibow-Koser <marnen@marnen.org> wrote:
The problem is that the 754 representation has finite precision. I
suppose you can call that "inadequate", but I don't see a good way
around it ... 0.6 is still infinite in binary. BigDecimal will get
you the right answer (unless you run out of memory), but when the
significant figures pile up it tends to get you there very slowly. All
the various arbitrary precision formats suffer badly performance wise.
It's also worth noting that most floating point hardware is not
anywhere close to 754 compliant even though most FPUs do use the
standard number formats (at least for single and double precision).
George
···
On Wed, 28 Oct 2009 14:30:21 -0500, Marnen Laibow-Koser <marnen@marnen.org> wrote:
Popcorn, well, it's kinda hard to transmit over the wire.
Easy to do with a modern email client - just needs support for POP3 and
a working firewall (for the heat).
LOL!
As a rule of thumb, if you really care about the decimals, either use
BigDecimal or integers (and keep track of where the decimal should be --
this is common for $$$$). Unfortunately, this is not limited to ruby,
either -- C, Java, and a host of other languages all are subject.
Absolutely: this is a common issue in *all* programming languages which
are not systems for symbolic math (like Mathematica) because they do not
work with real numbers but just rational numbers.
That is not the issue here -- after all, BigDecimal does precise
arithmetic, but only with rational numbers. The issue is rather that
IEEE 754 does an inadequate job of representing arbitrary rational
numbers, and the small errors are accumulated and magnified in
calculations.
Many people don't realize that floating point literals written
in base 10 (such as 123.6) may not have an exact finite
representation when converted to base 2
Right. 0.6 in binary has a repeating decimal -- 0.1001 repeating or
something like that.
and similarly a finite
base 2 floating point value may not have a finite representation
in base 10.
[...]
I think not. Every number of the form 1/(2^n) has a terminating decimal
in base 10. Am I wrong?
The problems, of course, arise with numbers like 1/3, which doesn't
terminate in either base. This is what the Rational class is good for.
That is not the issue here -- after all, BigDecimal does precise
arithmetic, but only with rational numbers.
BigDecimal actually works with decimal numbers, which are a subset of
rational numbers; Rational does precise math with rational numbers.
You're quite right, and I realized that about 10 seconds after I posted.
The issue is rather that IEEE 754 does an inadequate job of representing
arbitrary rational numbers, and the small errors are accumulated and
magnified in calculations.
The bigger issue is that Ruby -- like most general purpose programming
languages, though there are exceptions like Scheme -- makes IEEE 754
floating point the easiest non-integer data type to use, rather than
using exact numbers by default and using inexact numbers only when
explicitly called for (or when an operation that produces inexact
results is used.)
Yup. At least Ruby took a step in the right direction by making Bignum
transparent; it would be lovely if some future version did likewise with
BigDecimal.
Best,
···
On Wed, Oct 28, 2009 at 12:30 PM, Marnen Laibow-Koser > <marnen@marnen.org> wrote:
Interesting! I wasn't aware of that. Why is that? Do they just leave out operations or are HW vendors actually cutting corners and digressing from the prescribed algorithms / results?
Kind regards
robert
···
On 10/31/2009 07:35 AM, George Neuner wrote:
It's also worth noting that most floating point hardware is not
anywhere close to 754 compliant even though most FPUs do use the
standard number formats (at least for single and double precision).
Popcorn, well, it's kinda hard to transmit over the wire.
Easy to do with a modern email client - just needs support for POP3 and
a working firewall (for the heat).
LOL!
As a rule of thumb, if you really care about the decimals, either use
BigDecimal or integers (and keep track of where the decimal should be --
this is common for $$$$). Unfortunately, this is not limited to ruby,
either -- C, Java, and a host of other languages all are subject.
Absolutely: this is a common issue in *all* programming languages which
are not systems for symbolic math (like Mathematica) because they do not
work with real numbers but just rational numbers.
That is not the issue here -- after all, BigDecimal does precise
arithmetic, but only with rational numbers. The issue is rather that
IEEE 754 does an inadequate job of representing arbitrary rational
numbers, and the small errors are accumulated and magnified in
calculations.
The problem is that the 754 representation has finite precision.
Well, the problem isn't that. The problem is that the IEEE 754 (1985)
provides only binary floating point representations, when many common
problem domains deal almost exclusively with values that have finite
(and short) exact representations in base 10, which may or may not
have finite representations in base 2. IEEE 754 (2008) addresses this
with decimal floating point representations and operations. As
IEEE-754 (2008) is implemented more widely, it will be less likely
that arbitrary precision decimal libraries with poor performance will
be needed to do simple tasks that don't require many (base-10) digits
of precision, but do require precise calculations with base-10
numbers.
It's also worth noting that most floating point hardware is not
anywhere close to 754 compliant even though most FPUs do use the
standard number formats (at least for single and double precision).
AFAIK, neither IEEE 754 (1985) nor IEEE 754 (2008) requires that an
implementation be pure-hardware,and essentially-complete
implementations of IEEE 754 (1985) existed before it was a standard,
and complete implementations of both IEEE 754 (1985) and IEEE 754
(2008) exist now, including both pure-hardware and hardware+software
implementations of both.
···
On Fri, Oct 30, 2009 at 11:40 PM, George Neuner <gneuner2@comcast.net> wrote:
On Wed, 28 Oct 2009 14:30:21 -0500, Marnen Laibow-Koser > <marnen@marnen.org> wrote: