Marshaling Decimal types from .NET

J

John Lam

------=_Part_9810_19207910.1143749376557
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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=3D/library/en-us/csspec/h=
tml/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

------=_Part_9810_19207910.1143749376557--
 
C

Charles Mills

John said:
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/d...y/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

------=_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
 
J

John Lam

------=_Part_13233_16576879.1143767265060
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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:
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

------=_Part_13233_16576879.1143767265060--
 
J

Jeff Cohen

John said:
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
 
J

John Lam

------=_Part_13438_11741272.1143768790168
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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,

------=_Part_13438_11741272.1143768790168--
 
J

Jeff Cohen

John said:
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
 
L

Logan Capaldo

--Apple-Mail-11-106333958
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed


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


--Apple-Mail-11-106333958--
 
J

Jeff Cohen

Logan said:
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
 
J

Jeff Cohen

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
 
T

Tadashi Saito

Hi all,

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top