Marshaling Decimal types from .NET


#1

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=/library/en-us/csspec/html/vclrfcsharpspec_4_1_7.asp

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


#2

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


#3

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:


#4

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


#5

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,


#6

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


#7

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


#8

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


#9

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


#10

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.