From d9cb8f29617d7d04730bf475647391eb47b835f0 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sun, 2 Oct 2011 23:36:22 +0000 Subject: [PATCH] core: fix an accuracy error in sqrt() for arguments < 1.0. --- src/real.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/real.cpp b/src/real.cpp index 8738a86f..e6178885 100644 --- a/src/real.cpp +++ b/src/real.cpp @@ -526,9 +526,10 @@ real sqrt(real const &x) uint32_t sign = x.m_signexp & 0x80000000u; ret.m_signexp = sign; - int exponent = (x.m_signexp & 0x7fffffffu) - ((1 << 30) - 1); - exponent = - (exponent / 2) + (v.x >> 23) - (u.x >> 23); - ret.m_signexp |= (exponent + ((1 << 30) - 1)) & 0x7fffffffu; + uint32_t exponent = (x.m_signexp & 0x7fffffffu); + exponent = ((1 << 30) + (1 << 29) -1) - (exponent + 1) / 2; + exponent = exponent + (v.x >> 23) - (u.x >> 23); + ret.m_signexp |= exponent & 0x7fffffffu; /* Five steps of Newton-Raphson seems enough for 32-bigit reals. */ real three = 3;