sqrt

Guaranteed precision of sqrt function in C/C++

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 04:33:53
问题 Everyone knows sqrt function from math.h / cmath in C/C++ - it returns square root of its argument. Of course, it has to do it with some error, because not every number can be stored precisely. But am I guaranteed that the result has some precision? For example, 'it's the best approximation of square root that can be represented in the floating point type used or if you calculate square of the result, it will be as close to initial argument as possible using the floating point type given`?

composing floor and sqrt in haskell

走远了吗. 提交于 2019-11-30 21:21:34
I'm just learning haskell (on my own, for fun) and I've come up against a wall. My Question: How can I define a function flrt = (floor . sqrt) When I try it in a file and compile, GCHi complains with the following: AKS.hs:11:9: No instance for (RealFrac Integer) arising from a use of `floor' Possible fix: add an instance declaration for (RealFrac Integer) In the first argument of `(.)', namely `floor' In the expression: (floor . sqrt) In an equation for `flrt': flrt = (floor . sqrt) AKS.hs:11:17: No instance for (Floating Integer) arising from a use of `sqrt' Possible fix: add an instance

C# high precision calculations

[亡魂溺海] 提交于 2019-11-30 21:12:17
Consider this code: double result = Math.Sqrt(4746073226998689451); For result I get 2178548422 instead of 2178548421.999999854etc... How can I get more precise result? For the particular problem, computing the square root, you can use Decimal type and Newton's algorithm: using System; class Program { public static void Main() { long x = 4746073226998689451; decimal sqrt_x = (decimal)Math.Sqrt(x); for (int i = 0; i < 10; ++i) sqrt_x = 0.5m * (sqrt_x + x / sqrt_x); Console.WriteLine("{0:F16}", sqrt_x); } } The result is: 2178548421.9999998547197773 There is a bunch of high precision maths

How sqrt() of GCC works after compiled? Which method of root is used? Newton-Raphson?

て烟熏妆下的殇ゞ 提交于 2019-11-30 09:02:46
问题 Just curiosity about the standard sqrt() from math.h on GCC works. I coded my own sqrt() using Newton-Raphson to do it! 回答1: yeah, I know fsqrt. But how the CPU does it? I can't debug hardware Typical div/sqrt hardware in modern CPUs uses a power of 2 radix to calculate multiple result bits at once. e.g. http://www.imm.dtu.dk/~alna/pubs/ARITH20.pdf presents details of a design for a Radix-16 div/sqrt ALU, and compares it against the design in Penryn. (They claim lower latency and less power.)

composing floor and sqrt in haskell

拟墨画扇 提交于 2019-11-30 05:39:03
问题 I'm just learning haskell (on my own, for fun) and I've come up against a wall. My Question: How can I define a function flrt = (floor . sqrt) When I try it in a file and compile, GCHi complains with the following: AKS.hs:11:9: No instance for (RealFrac Integer) arising from a use of `floor' Possible fix: add an instance declaration for (RealFrac Integer) In the first argument of `(.)', namely `floor' In the expression: (floor . sqrt) In an equation for `flrt': flrt = (floor . sqrt) AKS.hs:11

Why does **0.5 appear to be more efficient than sqrt() [closed]

醉酒当歌 提交于 2019-11-29 12:34:37
I have tried measuring the speed of these two ways for taking square root: > system.time(expr = replicate(10000, 1:10000 ** (1/2))) ## user system elapsed ## 0.027 0.001 0.028 > system.time(expr = replicate(10000, sqrt(1:10000))) ## user system elapsed ## 3.722 0.665 4.494 If the sqrt() function cannot compete with ** 0.5 , why do we need such a function? (system is OS X Yusemite, and R version is 3.1.2) You forgot important parentheses. Here are the timings after correcting that: system.time(expr = replicate(10000, (1:10000) ** (1/2))) #user system elapsed #4.76 0.32 5.12 system.time(expr =

Generating digits of square root of 2

穿精又带淫゛_ 提交于 2019-11-29 02:57:06
问题 I want to generate the digits of the square root of two to 3 million digits. I am aware of Newton-Raphson but I don't have much clue how to implement it in C or C++ due to lack of biginteger support. Can somebody point me in the right direction? Also, if anybody knows how to do it in python (I'm a beginner), I would also appreciate it. 回答1: You could try using the mapping: a/b -> (a+2b)/(a+b) starting with a= 1, b= 1 . This converges to sqrt(2) (in fact gives the continued fraction

c++ practical computational complexity of <cmath> SQRT()

两盒软妹~` 提交于 2019-11-29 02:23:51
What is the difference in CPU cycles (or, in essence, in 'speed') between x /= y; and #include <cmath> x = sqrt(y); EDIT: I know the operations aren't equivalent, I'm just arbitrarily proposing x /= y as a benchmark for x = sqrt(y) osgx The answer to your question depends on your target platform. Assuming you are using most common x86 cpus, I can give you this link http://instlatx64.atw.hu/ This is a collection of measured instruction latency (How long will it take to CPU to get result after it has argument) and how they are pipelined for many x86 and x86_64 processors. If your target is not

Fast sqrt in Java at the expense of accuracy

只愿长相守 提交于 2019-11-28 23:50:50
I am looking for a fast square root implementation in Java for double values in the input range of [0, 2*10^12]. For any value in this range, the precision should be upto 5 decimal places. In other words, the result can differ from the Math.sqrt() method after 5 decimal places. However, this method needs to be much faster than Math.sqrt() . Any ideas? Thanks! Martin Thurau I don't believe (without a benchmark to prove this wrong) that a pure Java implementation could me much faster than Math.sqrt() . Both the Oracle JRE implementation and the OpenJDK implementation are native implementations.

Is it possible to roll a significantly faster version of sqrt

自古美人都是妖i 提交于 2019-11-28 20:21:52
In an app I'm profiling, I found that in some scenarios this function is able to take over 10% of total execution time. I've seen discussion over the years of faster sqrt implementations using sneaky floating-point trickery, but I don't know if such things are outdated on modern CPUs. MSVC++ 2008 compiler is being used, for reference... though I'd assume sqrt is not going to add much overhead though. See also here for similar discussion on modf function. EDIT: for reference, this is one widely-used method, but is it actually much quicker? How many cycles is SQRT anyway these days? Yes, it is