Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a simple method for square root of big.Rat?

I need to find the square root of a big.Rat. Is there a way to do it without losing (already existing) accuracy?

For example, I could convert the numerator and denominator into floats, get the square root, and then convert it back…

func ratSquareRoot(num *big.Rat) *big.Rat {
    f, exact := num.Float64() //Yuck! Floats!
    squareRoot := math.Sqrt(f)
    var accuracy int64 = 10 ^ 15 //Significant digits of precision for float64
    return big.NewRat(int64(squareRoot*float64(accuracy)), accuracy) 
    // ^ This is now totally worthless. And also probably not simplified very well.

}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

…but that would eliminate all of the accuracy of using a rational. Is there a better way of doing this?

>Solution :

The big.Float type has a .Sqrt(x) operation, and handles defining explicitly the precision you aim for. I’d try to use that and convert the result back to a Rat with the same operations in your question, only manipulating big.Int values.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading