# BigDecimal bug?

**URL:** <https://www.ruby-forum.com/t/bigdecimal-bug/88663>\
**Category:** Ruby\
**Created:** [March 13, 2007, 12:08pm UTC](https://www.ruby-forum.com/t/bigdecimal-bug/88663 "2007-03-13T12:08:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mslagell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mslagell](https://www.ruby-forum.com/u/mslagell)\
**Post date:** [March 13, 2007, 12:08pm UTC](https://www.ruby-forum.com/t/bigdecimal-bug/88663/1 "2007-03-13T12:08:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mslagell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mslagell](https://www.ruby-forum.com/u/mslagell)\
**Post date:** [March 13, 2007, 12:45pm UTC](https://www.ruby-forum.com/t/bigdecimal-bug/88663/2 "2007-03-13T12:45:58Z")

</div>

On Tue, Mar 13, 2007 at 08:08:07PM +0900, Mark S. 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

---

<div class="post-metadata">

**Author:** ![mslagell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mslagell](https://www.ruby-forum.com/u/mslagell)\
**Post date:** [March 13, 2007, 12:51pm UTC](https://www.ruby-forum.com/t/bigdecimal-bug/88663/3 "2007-03-13T12:51:59Z")

</div>

Alle martedì 13 marzo 2007, Mark S. 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)

Stefano

---

<div class="post-metadata">

**Author:** ![mslagell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mslagell](https://www.ruby-forum.com/u/mslagell)\
**Post date:** [March 13, 2007, 5:31pm UTC](https://www.ruby-forum.com/t/bigdecimal-bug/88663/4 "2007-03-13T17:31:55Z")

</div>

On Mar 13, 5:08 am, Mark S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![mslagell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mslagell](https://www.ruby-forum.com/u/mslagell)\
**Post date:** [March 13, 2007, 6:25pm UTC](https://www.ruby-forum.com/t/bigdecimal-bug/88663/5 "2007-03-13T18:25:06Z")

</div>

On Tue, Mar 13, 2007 at 08:51:02PM +0900, Stefano C. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![mslagell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mslagell](https://www.ruby-forum.com/u/mslagell)\
**Post date:** [March 13, 2007, 6:35pm UTC](https://www.ruby-forum.com/t/bigdecimal-bug/88663/6 "2007-03-13T18:35:08Z")

</div>

On Wed, Mar 14, 2007 at 01:31:32AM +0900, Daniel B. wrote:

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

---

<div class="post-metadata">

**Author:** ![mslagell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mslagell](https://www.ruby-forum.com/u/mslagell)\
**Post date:** [March 14, 2007, 4:34am UTC](https://www.ruby-forum.com/t/bigdecimal-bug/88663/7 "2007-03-14T04:34:29Z")

</div>

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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

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

Thank you in advance.

Shigeo Kobayashi  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mslagell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mslagell](https://www.ruby-forum.com/u/mslagell)\
**Post date:** [March 14, 2007, 7:26am UTC](https://www.ruby-forum.com/t/bigdecimal-bug/88663/8 "2007-03-14T07:26:40Z")

</div>

Hi,

In message “Re: BigDecimal bug?”  
on Wed, 14 Mar 2007 12:34:08 +0900, “Shigeo Kobayashi”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
|  
| \* ext/bigdecimal/bigdecimal.c: BigDecimal(“-.31”) is now  
| treated as (“-0.31”) not as (“0.31”).

Done.

```
          matz.

```
