Tomek Gieorgijewski
2025-May-05 17:18 UTC
[Rd] sqrt(.Machine$double.xmax)^2 == Inf, but only on Windows in R
Hello! My three Groschen: On MacOS (Intel CPU) and R 4.4.3 I report:> dput(.Machine$double.xmax, control = 'hex')0x1.fffffffffffffp+1023> dput(sqrt(.Machine$double.xmax), control = 'hex')0x1.fffffffffffffp+511> dput(sqrt(.Machine$double.xmax)^2, control = 'hex')0x1.ffffffffffffep+1023 so as Ivan tested on Linux:> ...which differs from R on Linux: > > dput(.Machine$double.xmax, control = 'hex') > # 0x1.fffffffffffffp+1023 > dput(sqrt(.Machine$double.xmax), control = 'hex') > # 0x1.fffffffffffffp+511 > dput(sqrt(.Machine$double.xmax)^2, control = 'hex') > # 0x1.ffffffffffffep+1023 > > -- > Best regards, > Ivan > > --Also I've found when I was researching sqrt algorithms that on x86_64 GCC implements `sqrt` using machine instruction `sqrtsd` (sqrt scalar double): https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/x86_64/fpu/e_sqrt.c;h=33b59f67c10252bf8982303db2159f5a51c1ad9d;hb=f59f7adb4a00b7784cab1becdf257366104587b7 So if it's used, on recent Intel CPUs should return consistent results among different platforms. I wonder what happens on Windows in this matter. -- Also, to test what clang does, I compiled simple program doing square root of double value to assembly. It also shows usage of machine instruction: `sqrtsd %xmm0, %xmm0` I guess OP reports the problem in consistency between R implementations on different OSs than precision itself. regards, Tomek. [[alternative HTML version deleted]]