Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > BigDecimal bug?

Reply
Thread Tools

BigDecimal bug?

 
 
Mark Somerville
Guest
Posts: n/a
 
      03-13-2007
Hello,

Can anyone tell me if this is a bug? It certainly seems like it to me.

>> BigDecimal.new("-.31").to_s

=> "0.31"

$ ruby -v
ruby 1.8.5 (2006-12-04 patchlevel 2) [x86_64-linux]

I have tried altering the BigDecimal initialisation code, without success.
Does anyone have any clues as to how to workaround this issue without
pre-processing the "-.31" string? I'd think I could alter the "new" call to
alter the malformed string a little.

Thanks a lot in advance,

Mark

 
Reply With Quote
 
 
 
 
Mark Somerville
Guest
Posts: n/a
 
      03-13-2007
On Tue, Mar 13, 2007 at 08:08:07PM +0900, Mark Somerville wrote:
> I have tried altering the BigDecimal initialisation code, without success.
> Does anyone have any clues as to how to workaround this issue without
> pre-processing the "-.31" string? I'd think I could alter the "new" call to
> alter the malformed string a little.


I've managed to get a workaround going now (I realise this is a poor
substitute for fixing the poorly formed decimals), but I don't understand why I need
to wrap the alias_method inside class << self. Can anyone explain it to me?

class BigDecimal

#why do I need to wrap this in class << self ???
class << self
alias_method 'original_new', 'new'
end

def self.new(value)
value = "-0.#{$1}" if value =~ /^-\.(.+)$/
return original_new(value)
end

end

Cheers,

Mark

 
Reply With Quote
 
 
 
 
Stefano Crocco
Guest
Posts: n/a
 
      03-13-2007
Alle marted=EC 13 marzo 2007, Mark Somerville ha scritto:
> I don't understand why I need
> to wrap the alias_method inside class << self. Can anyone explain it to m=

e?

alias_method works for instance methods (i.e, a method of instances of clas=
s=20
BigDecimal). You're aliasing a class method (new), that is an instance meth=
od=20
of the class BigDecimal. To do so, you should enter a scope where self is n=
ot=20
the class BigDecimal, but BigDecimal's class. This way, new is seen as an=20
instance method (i.e. a method of instances of the class of BigDecimal, i.e=
=2E=20
of BigDecimal). This is what you do with the class << self construct.

I hope this explaination makes sense (if it doesn't, tell me and I'll try t=
o=20
clarify)

Stefano

 
Reply With Quote
 
Daniel Berger
Guest
Posts: n/a
 
      03-13-2007


On Mar 13, 5:08 am, Mark Somerville <(E-Mail Removed)> wrote:
> Hello,
>
> Can anyone tell me if this is a bug? It certainly seems like it to me.
>
> >> BigDecimal.new("-.31").to_s

>
> => "0.31"
>
> $ ruby -v
> ruby 1.8.5 (2006-12-04 patchlevel 2) [x86_64-linux]


Looks like a bug to me. I think it's in the VpToString() function
being called by the BigDecimal_to_s() function. I'm not positive,
though, as the code is long and ugly.

Please submit a bug report to ruby-core.

Thanks,

Dan


 
Reply With Quote
 
Mark Somerville
Guest
Posts: n/a
 
      03-13-2007
On Tue, Mar 13, 2007 at 08:51:02PM +0900, Stefano Crocco wrote:
> Alle marted́ 13 marzo 2007, Mark Somerville ha scritto:
> > I don't understand why I need
> > to wrap the alias_method inside class << self. Can anyone explain it to me?

>
> alias_method works for instance methods (i.e, a method of instances of class
> BigDecimal). You're aliasing a class method (new), that is an instance method
> of the class BigDecimal. To do so, you should enter a scope where self is not
> the class BigDecimal, but BigDecimal's class. This way, new is seen as an
> instance method (i.e. a method of instances of the class of BigDecimal, i.e.
> of BigDecimal). This is what you do with the class << self construct.
>
> I hope this explaination makes sense (if it doesn't, tell me and I'll try to
> clarify)


Thanks a lot Stefano, that explanation was exactly what I needed.

Mark

 
Reply With Quote
 
Mark Somerville
Guest
Posts: n/a
 
      03-13-2007
On Wed, Mar 14, 2007 at 01:31:32AM +0900, Daniel Berger wrote:
> On Mar 13, 5:08 am, Mark Somerville <(E-Mail Removed)> wrote:
> > Hello,
> >
> > Can anyone tell me if this is a bug? It certainly seems like it to me.
> >
> > >> BigDecimal.new("-.31").to_s

> >
> > => "0.31"
> >
> > $ ruby -v
> > ruby 1.8.5 (2006-12-04 patchlevel 2) [x86_64-linux]

>
> Looks like a bug to me. I think it's in the VpToString() function
> being called by the BigDecimal_to_s() function. I'm not positive,
> though, as the code is long and ugly.
>
> Please submit a bug report to ruby-core.


Sure, done.

Mark

 
Reply With Quote
 
Shigeo Kobayashi
Guest
Posts: n/a
 
      03-14-2007
Subject: Re: BigDecimal bug?


> Can anyone tell me if this is a bug? It certainly seems like it to me.
> BigDecimal.new("-.31").to_s
> => "0.31"

Yes,could you apply the patch bellow:
---------------------------------------------------------------------------------
$ ruby/ruby-1.8.6/ext/bigdecimal
$ diff -up bigdecimal.c.old bigdecimal.c
--- bigdecimal.c.old 2007-03-14 10:21:30.015625000 +0900
+++ bigdecimal.c 2007-03-14 12:26:18.625000000 +0900
@@ -3921,7 +3921,7 @@ VpCtoV(Real *a, const char *int_chr, U_L
/* get integer part */
i = 0;
sign = 1;
- if(ni > 0) {
+ if(ni >= 0) {
if(int_chr[0] == '-') {
sign = -1;
++i;
---------------------------------------------------------------------------------

Matz, could you please apply the patch and commit the source
with the change log ?
Somewhat like:
Wed Mar 14 12:30:00 2007 Shigeo Kobayashi <(E-Mail Removed)>

* ext/bigdecimal/bigdecimal.c: BigDecimal("-.31") is now
treated as ("-0.31") not as ("0.31").

Thank you in advance.

Shigeo Kobayashi
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BigDecimal(String) vs. BigDecimal(double) [Floating-point arithmetics] Stanimir Stamenkov Java 4 07-18-2008 10:49 AM
BigDecimal to power of BigDecimal czarnysfetr@gmail.com Java 3 02-05-2007 02:49 PM
BigDecimal: Sun? IBM? wald Java 4 12-10-2004 12:22 AM
Problems with XMLEncoder and BigDecimal Cory Musselman Java 0 10-30-2003 04:40 PM
Problem with Double to BigDecimal Conversion Forrest Hump Java 4 08-22-2003 03:53 PM



Advertisments