Forum: Ruby Marshaling Decimal types from .NET

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
John L. (Guest)
on 2006-03-31 00:11
(Received via mailing list)
Decimal numbers in .NET are 128 bit values that avoid the rounding
errors
that can arise from floating point calcs:
http://msdn.microsoft.com/library/default.asp?url=...

My question is: when marshaling a .NET decimal type to Ruby, what should
I
do? Should I marshal it to a bigdecimal?

Thanks
-John
http://www.iunknown.com
Charles Mills (Guest)
on 2006-03-31 04:34
(Received via mailing list)
John L. wrote:
>
> ------=_Part_9810_19207910.1143749376557--

I would say yes.  I expect you would have something like the following:
C#          Ruby
----        ----
float       Float
double      Float
decimal     BigDecimal

I imagine when a C# programmer uses decimal, they choose it instead of
double for a reason, making a conversion from decimal to Float (double)
all the more likely to cause rounding error.

-Charlie
John L. (Guest)
on 2006-03-31 05:10
(Received via mailing list)
Thanks, Charlie I added the new marshaling code to RubyCLR this
afternoon.
The only downside is that I have to convert the Decimal to a string
first
before I can call BigDecimal's constructor, so this definitely impacts
performance when marshaling Decimals across the interop boundary.

-John
http://www.iunknown.com



I would say yes.  I expect you would have something like the following:
Jeff C. (Guest)
on 2006-03-31 05:27
John L. wrote:
> The only downside is that I have to convert the Decimal to a string
> first
> before I can call BigDecimal's constructor, so this definitely impacts
> performance when marshaling Decimals across the interop boundary.
>

I use decimal in C# for financial calculations, because we would get
round-off errors if we used floats.

I wasn't aware of BigDecimal before - it's not part of the core in 1.82,
is it?

Thanks
Jeff
www.softiesonrails.com
John L. (Guest)
on 2006-03-31 05:34
(Received via mailing list)
I believe that BigDecimal has been included in the standard distribution
since 1.8.

I wasn't aware of BigDecimal before - it's not part of the core in 1.82,
Jeff C. (Guest)
on 2006-03-31 05:46
John L. wrote:
> I believe that BigDecimal has been included in the standard distribution
> since 1.8.
>

How do I specify a value to be of type BigDecimal instead of Float?

irb> 1.2.class
=> Float
Logan C. (Guest)
on 2006-03-31 05:52
(Received via mailing list)
On Mar 30, 2006, at 8:27 PM, Jeff C. wrote:

> I wasn't aware of BigDecimal before - it's not part of the core in
> 1.82,
> is it?

I'm pretty sure it is:
logan:/Users/logan% /usr/bin/ruby -v
ruby 1.8.2 (2004-12-25) [powerpc-darwin8.0]
logan:/Users/logan% /usr/bin/ruby -e 'require "bigdecimal"; puts
BigDecimal.new("1000022.0") + 2'
0.1000024E7
Jeff C. (Guest)
on 2006-03-31 06:03
Logan C. wrote:
> On Mar 30, 2006, at 8:27 PM, Jeff C. wrote:
>
>> I wasn't aware of BigDecimal before - it's not part of the core in
>> 1.82,
>> is it?
>
> I'm pretty sure it is:
> logan:/Users/logan% /usr/bin/ruby -v
> ruby 1.8.2 (2004-12-25) [powerpc-darwin8.0]
> logan:/Users/logan% /usr/bin/ruby -e 'require "bigdecimal"; puts
> BigDecimal.new("1000022.0") + 2'
> 0.1000024E7

AH HA!  I didn't realize I had to specifically require it (duh!).

Thanks Logan.


Jeff
www.softiesonrails.com
Jeff C. (Guest)
on 2006-03-31 06:11
I see now why I didn't know it was in 1.82.  I had always been using
this url to look up things:

http://ruby-doc.org/core/

but BigDecimal is part of the "standard API", which is documented at:
http://ruby-doc.org/stdlib/

and worse, BigDecimal is categorized as an under-documented class (it's
in italics in the alphabetical list), and they're not kidding!  Most of
the methods are undocumented.

Maybe I'll try to figure out what all the methods of BigDecimal do, and
try to update the documentation on http://ruby-doc.org/stdlib/.

Thanks again,
Jeff
Tadashi S. (Guest)
on 2006-03-31 17:21
(Received via mailing list)
Hi all,

On Fri, 31 Mar 2006 11:11:09 +0900
Jeff C. <removed_email_address@domain.invalid> wrote:

> in italics in the alphabetical list), and they're not kidding!  Most of
> the methods are undocumented.

Oh, no.  BigDecimal is (almost) fully documented in
ext/bigdecimal/bigdecimal_en.html.
But you know, it's not rdoc, so you and some people will be not
satisfied.
This topic is locked and can not be replied to.