# BigDecimal#sqrt

**URL:** <https://www.ruby-forum.com/t/bigdecimal-sqrt/50933>\
**Category:** Ruby\
**Created:** [December 22, 2005, 11:06pm UTC](https://www.ruby-forum.com/t/bigdecimal-sqrt/50933 "2005-12-22T23:06:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [December 22, 2005, 11:06pm UTC](https://www.ruby-forum.com/t/bigdecimal-sqrt/50933/1 "2005-12-22T23:06:29Z")

</div>

I’m trying to understand the argument to BigDecimal#sqrt. I figured  
it was for precision and it does seem to affect it, but I can’t  
understand the exactly relationship. Can anyone tell me what the  
argument controls and how?

Thanks.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [December 23, 2005, 1:27pm UTC](https://www.ruby-forum.com/t/bigdecimal-sqrt/50933/2 "2005-12-23T13:27:56Z")

</div>

yeah, it looks right.  
interesting format variation:

minfieldwidth = 22  
precision = 16  
sprintf “%\*.\*f”, minfieldwidth, precision,  
BigDecimal.new(“66543”).sqrt(precision)  
=\> " 257.9592991151898786"

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [December 23, 2005, 6:08pm UTC](https://www.ruby-forum.com/t/bigdecimal-sqrt/50933/3 "2005-12-23T18:08:19Z")

</div>

On 12/22/05, James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m trying to understand the argument to BigDecimal#sqrt. I figured  
> it was for precision and it does seem to affect it, but I can’t  
> understand the exactly relationship. Can anyone tell me what the  
> argument controls and how?

This is the C source, if you haven’t looked at it yet.

BigDecimal\_sqrt(VALUE self, VALUE nFig)  
{  
ENTER(5);  
Real \*c, \*a;  
S\_INT mx, n;

```
GUARD_OBJ(a,GetVpValue(self,1));
mx = a->Prec *(VpBaseFig() + 1);

n = GetPositiveInt(nFig) + VpDblFig() + 1;
if(mx <= n) mx = n;
GUARD_OBJ(c,VpCreateRbObject(mx, "0"));
VpSqrt(c, a);
return ToValue(c);

```

}

I don’t fully understand what’s going on with it, because my C is  
pretty weak, but hopefully this will provide some insight from  
someone.

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [December 28, 2005, 2:43pm UTC](https://www.ruby-forum.com/t/bigdecimal-sqrt/50933/4 "2005-12-28T14:43:21Z")

</div>

On Dec 27, 2005, at 10:27 PM, mathew wrote:

> James Edward G. II wrote:
> 
> > Let me try asking me question a different way… Is this the  
> > correct way to get a printable sqrt() with 16 digits precision?
> > 
> > > > sprintf “%.16f”, BigDecimal.new(“66543”).sqrt(16)  
> > > > =\> “257.9592991151890260”
> 
> Yes.
> 
> I’ve written some documentation for BigDecimal which will hopefully  
> be checked in to Ruby some time.

Is it available online anywhere before then or could I trouble you to  
send it to me privately?

James Edward G. II

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [December 23, 2005, 5:13am UTC](https://www.ruby-forum.com/t/bigdecimal-sqrt/50933/5 "2005-12-23T05:13:42Z")

</div>

On Dec 22, 2005, at 4:04 PM, James Edward G. II wrote:

> I’m trying to understand the argument to BigDecimal#sqrt. I  
> figured it was for precision and it does seem to affect it, but I  
> can’t understand the exactly relationship. Can anyone tell me what  
> the argument controls and how?

Let me try asking me question a different way… Is this the correct  
way to get a printable sqrt() with 16 digits precision?

> > sprintf “%.16f”, BigDecimal.new(“66543”).sqrt(16)  
> > =\> “257.9592991151890260”

James Edward G. II

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [December 23, 2005, 5:13am UTC](https://www.ruby-forum.com/t/bigdecimal-sqrt/50933/6 "2005-12-23T05:13:44Z")

</div>

On Dec 22, 2005, at 4:04 PM, James Edward G. II wrote:

> I’m trying to understand the argument to BigDecimal#sqrt. I  
> figured it was for precision and it does seem to affect it, but I  
> can’t understand the exactly relationship. Can anyone tell me what  
> the argument controls and how?

Let me try asking my question a different way… Is this the correct  
way to get a printable square root with N digits precision (N = 16 in  
this example)?

> > sprintf “%.16f”, BigDecimal.new(“66543”).sqrt(16)  
> > =\> “257.9592991151890260”

James Edward G. II

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [December 28, 2005, 5:29am UTC](https://www.ruby-forum.com/t/bigdecimal-sqrt/50933/7 "2005-12-28T05:29:30Z")

</div>

James Edward G. II wrote:

> Let me try asking me question a different way… Is this the correct  
> way to get a printable sqrt() with 16 digits precision?
> 
> > > sprintf “%.16f”, BigDecimal.new(“66543”).sqrt(16)  
> > > =\> “257.9592991151890260”

Yes.

I’ve written some documentation for BigDecimal which will hopefully be  
checked in to Ruby some time.

mathew
