From: Gary Wright on

On Oct 28, 2009, at 5:14 PM, Marnen Laibow-Koser wrote:
>> 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.

I spoke to quickly and had in mind what you just
suggested, that there are numbers that don't terminate
in either base.

But my main point was that people become aware of
these issues via the conversion problem rather than
via the nuances of IEEE floating point arithmetic.

Gary Wright

From: Rajinder Yadav on
Aldric Giacomoni wrote:
> I found this blog entry:
> http://raveendran.wordpress.com/2009/06/29/ruby-big-decimal/
> Which shows the following..
>
> irb(main):011:0> require 'bigdecimal'
> => true
> irb(main):012:0> x = BigDecimal("123.6") - BigDecimal("123")
> => #<BigDecimal:28c5b84,'0.6E0',4(16)>
> irb(main):013:0> puts x.to_f
> 0.6
> => nil
> irb(main):014:0> puts f=123.6 - 123
> 0.599999999999994
> => nil
> irb(main):015:0> 123.6 - 123
> => 0.599999999999994
> irb(main):016:0> 123.6 - 123.0
> => 0.599999999999994
> irb(main):017:0>
>
> That's a little strange.. Isn't it?

I've been following this thread and am wondering if there are other numerical
classes for Ruby other than BigDecimal?

--
Kind Regards,
Rajinder Yadav

http://DevMentor.org

Do Good! - Share Freely, Enrich and Empower people to Transform their lives.

From: Marnen Laibow-Koser on
Rajinder Yadav wrote:
[...]
> 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?

>
> --
> Kind Regards,
> Rajinder Yadav
>
> http://DevMentor.org
>
> Do Good! - Share Freely, Enrich and Empower people to Transform their
> lives.
Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen(a)marnen.org
--
Posted via http://www.ruby-forum.com/.

From: Rajinder Yadav on
Marnen Laibow-Koser wrote:
> Rajinder Yadav wrote:
> [...]
>> 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.


> Best,
> --
> Marnen Laibow-Koser
> http://www.marnen.org
> marnen(a)marnen.org


--
Kind Regards,
Rajinder Yadav

http://DevMentor.org

Do Good! - Share Freely, Enrich and Empower people to Transform their lives.

From: Christopher Dicely on
On Wed, Oct 28, 2009 at 12:30 PM, Marnen Laibow-Koser <marnen(a)marnen.org> wrote:
> Robert Klemme wrote:
>> On 28.10.2009 19:21, Matthew K. Williams wrote:
>>>> I bash my head against the wall? :)
>>>
>>> Pamphlet -> http://en.wikipedia.org/wiki/IEEE_754-2008
>>>
>>> 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.)