待解.Math.sqrt;Double;Rounding Error

 1 package com.java7;
 2
 3 // Show square roots of 1 to 99 and the rounding error.
 4 public class SqrRoot {
 5         public static void main(String[] args) {
 6             double num, sroot, rerr;
 7
 8             for(num = 1.0; num < 100.0; num++) {
 9                 sroot = Math.sqrt(num);
10                 System.out.println("Square root of " + num + " is " + sroot);
11
12                 // compute rounding error
13                 rerr = num - (sroot * sroot);
14                 System.out.println("Rounding error is " + rerr);
15                 System.out.println();
16             }
17         }
18 }

执行结果:

Square root of 1.0 is 1.0
Rounding error is 0.0

Square root of 2.0 is 1.4142135623730951
Rounding error is -4.440892098500626E-16

Square root of 3.0 is 1.7320508075688772
Rounding error is 4.440892098500626E-16

Square root of 4.0 is 2.0
Rounding error is 0.0

Square root of 5.0 is 2.23606797749979
Rounding error is -8.881784197001252E-16

Square root of 6.0 is 2.449489742783178
Rounding error is 8.881784197001252E-16

Square root of 7.0 is 2.6457513110645907
Rounding error is -8.881784197001252E-16

Square root of 8.0 is 2.8284271247461903
Rounding error is -1.7763568394002505E-15

Square root of 9.0 is 3.0
Rounding error is 0.0

Square root of 10.0 is 3.1622776601683795
Rounding error is -1.7763568394002505E-15

Square root of 11.0 is 3.3166247903554
Rounding error is 0.0

Square root of 12.0 is 3.4641016151377544
Rounding error is 1.7763568394002505E-15

Square root of 13.0 is 3.605551275463989
Rounding error is 1.7763568394002505E-15

Square root of 14.0 is 3.7416573867739413
Rounding error is 0.0

Square root of 15.0 is 3.872983346207417
Rounding error is -1.7763568394002505E-15

Square root of 16.0 is 4.0
Rounding error is 0.0

Square root of 17.0 is 4.123105625617661
Rounding error is 0.0

Square root of 18.0 is 4.242640687119285
Rounding error is 3.552713678800501E-15

Square root of 19.0 is 4.358898943540674
Rounding error is -3.552713678800501E-15

Square root of 20.0 is 4.47213595499958
Rounding error is -3.552713678800501E-15

Square root of 21.0 is 4.58257569495584
Rounding error is 0.0

Square root of 22.0 is 4.69041575982343
Rounding error is 0.0

Square root of 23.0 is 4.795831523312719
Rounding error is 3.552713678800501E-15

Square root of 24.0 is 4.898979485566356
Rounding error is 3.552713678800501E-15

Square root of 25.0 is 5.0
Rounding error is 0.0

Square root of 26.0 is 5.0990195135927845
Rounding error is 3.552713678800501E-15

Square root of 27.0 is 5.196152422706632
Rounding error is 0.0

Square root of 28.0 is 5.291502622129181
Rounding error is -3.552713678800501E-15

Square root of 29.0 is 5.385164807134504
Rounding error is 3.552713678800501E-15

Square root of 30.0 is 5.477225575051661
Rounding error is 0.0

Square root of 31.0 is 5.5677643628300215
Rounding error is 3.552713678800501E-15

Square root of 32.0 is 5.656854249492381
Rounding error is -7.105427357601002E-15

Square root of 33.0 is 5.744562646538029
Rounding error is 0.0

Square root of 34.0 is 5.830951894845301
Rounding error is 0.0

Square root of 35.0 is 5.916079783099616
Rounding error is 0.0

Square root of 36.0 is 6.0
Rounding error is 0.0

Square root of 37.0 is 6.082762530298219
Rounding error is 7.105427357601002E-15

Square root of 38.0 is 6.164414002968976
Rounding error is 7.105427357601002E-15

Square root of 39.0 is 6.244997998398398
Rounding error is 0.0

Square root of 40.0 is 6.324555320336759
Rounding error is -7.105427357601002E-15

Square root of 41.0 is 6.4031242374328485
Rounding error is 0.0

Square root of 42.0 is 6.48074069840786
Rounding error is 0.0

Square root of 43.0 is 6.557438524302
Rounding error is 7.105427357601002E-15

Square root of 44.0 is 6.6332495807108
Rounding error is 0.0

Square root of 45.0 is 6.708203932499369
Rounding error is -7.105427357601002E-15

Square root of 46.0 is 6.782329983125268
Rounding error is 0.0

Square root of 47.0 is 6.855654600401044
Rounding error is 0.0

Square root of 48.0 is 6.928203230275509
Rounding error is 7.105427357601002E-15

Square root of 49.0 is 7.0
Rounding error is 0.0

Square root of 50.0 is 7.0710678118654755
Rounding error is -7.105427357601002E-15

Square root of 51.0 is 7.14142842854285
Rounding error is -7.105427357601002E-15

Square root of 52.0 is 7.211102550927978
Rounding error is 7.105427357601002E-15

Square root of 53.0 is 7.280109889280518
Rounding error is 0.0

Square root of 54.0 is 7.3484692283495345
Rounding error is 0.0

Square root of 55.0 is 7.416198487095663
Rounding error is 0.0

Square root of 56.0 is 7.483314773547883
Rounding error is 0.0

Square root of 57.0 is 7.54983443527075
Rounding error is 0.0

Square root of 58.0 is 7.615773105863909
Rounding error is -7.105427357601002E-15

Square root of 59.0 is 7.681145747868608
Rounding error is 7.105427357601002E-15

Square root of 60.0 is 7.745966692414834
Rounding error is -7.105427357601002E-15

Square root of 61.0 is 7.810249675906654
Rounding error is 7.105427357601002E-15

Square root of 62.0 is 7.874007874011811
Rounding error is 0.0

Square root of 63.0 is 7.937253933193772
Rounding error is -7.105427357601002E-15

Square root of 64.0 is 8.0
Rounding error is 0.0

Square root of 65.0 is 8.06225774829855
Rounding error is 1.4210854715202004E-14

Square root of 66.0 is 8.12403840463596
Rounding error is -1.4210854715202004E-14

Square root of 67.0 is 8.18535277187245
Rounding error is 0.0

Square root of 68.0 is 8.246211251235321
Rounding error is 0.0

Square root of 69.0 is 8.306623862918075
Rounding error is 0.0

Square root of 70.0 is 8.366600265340756
Rounding error is 0.0

Square root of 71.0 is 8.426149773176359
Rounding error is 0.0

Square root of 72.0 is 8.48528137423857
Rounding error is 1.4210854715202004E-14

Square root of 73.0 is 8.54400374531753
Rounding error is 1.4210854715202004E-14

Square root of 74.0 is 8.602325267042627
Rounding error is 0.0

Square root of 75.0 is 8.660254037844387
Rounding error is -1.4210854715202004E-14

Square root of 76.0 is 8.717797887081348
Rounding error is -1.4210854715202004E-14

Square root of 77.0 is 8.774964387392123
Rounding error is -1.4210854715202004E-14

Square root of 78.0 is 8.831760866327848
Rounding error is -1.4210854715202004E-14

Square root of 79.0 is 8.888194417315589
Rounding error is 0.0

Square root of 80.0 is 8.94427190999916
Rounding error is -1.4210854715202004E-14

Square root of 81.0 is 9.0
Rounding error is 0.0

Square root of 82.0 is 9.055385138137417
Rounding error is -1.4210854715202004E-14

Square root of 83.0 is 9.1104335791443
Rounding error is 0.0

Square root of 84.0 is 9.16515138991168
Rounding error is 0.0

Square root of 85.0 is 9.219544457292887
Rounding error is 0.0

Square root of 86.0 is 9.273618495495704
Rounding error is 0.0

Square root of 87.0 is 9.327379053088816
Rounding error is -1.4210854715202004E-14

Square root of 88.0 is 9.38083151964686
Rounding error is 0.0

Square root of 89.0 is 9.433981132056603
Rounding error is 1.4210854715202004E-14

Square root of 90.0 is 9.486832980505138
Rounding error is 0.0

Square root of 91.0 is 9.539392014169456
Rounding error is 0.0

Square root of 92.0 is 9.591663046625438
Rounding error is 1.4210854715202004E-14

Square root of 93.0 is 9.643650760992955
Rounding error is 0.0

Square root of 94.0 is 9.695359714832659
Rounding error is -1.4210854715202004E-14

Square root of 95.0 is 9.746794344808963
Rounding error is 1.4210854715202004E-14

Square root of 96.0 is 9.797958971132712
Rounding error is 1.4210854715202004E-14

Square root of 97.0 is 9.848857801796104
Rounding error is 1.4210854715202004E-14

Square root of 98.0 is 9.899494936611665
Rounding error is 0.0

Square root of 99.0 is 9.9498743710662
Rounding error is 0.0

时间: 2024-08-26 17:57:19

待解.Math.sqrt;Double;Rounding Error的相关文章

为什么要用Math.sqrt(i)方法

java 练习题 判断 101-200 之间有多少个素数,并输出所有素数 public class Prime { public static int count = 0; public static void main(String[] args) { for (int i = 101; i < 200; i++) {            boolean b = true;            for (int j = 2; j <=  Math.sqrt(i); j++) {//---

20170430 math.sqrt函数

sqrt() 方法返回数字x的平方根 语法: import math math.sqrt( x ) 注意:sqrt()是不能直接访问的,需要导入 math 模块,通过静态对象调用该方法. import math # This will import math module print ("math.sqrt(100) : ", math.sqrt(100))print ("math.sqrt(7) : ", math.sqrt(7))print ("mat

JavaScript基础 Math.sqrt() 平方根 只能计算 大于等于0的数

镇场诗: 清心感悟智慧语,不着世间名与利.学水处下纳百川,舍尽贡高我慢意. 学有小成返哺根,愿铸一良心博客.诚心于此写经验,愿见文者得启发.------------------------------------------ code: 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=ut

java中常用到的math方法(Math.PI、Math.random()、Math.abs(double)、Math.floor(double)、Math.ceil(double)、Math.round(double))

public class MathDemo { public static void main(String args[]){ /** * abs求绝对值 */ System.out.println(Math.abs(-10.4));    //10.4 System.out.println(Math.abs(10.1));     //10.1 /** * ceil天花板的意思,就是返回大的值,注意一些特殊值 */ System.out.println(Math.ceil(-10.1));  

[Math]Sqrt(x)

Total Accepted: 75767 Total Submissions: 314003 Difficulty: Medium Implement int sqrt(int x). Compute and return the square root of x. (M) Pow(x, n) class Solution { public: int mySqrt(int x) { if(x<0){ return INT_MIN; } if(x==0){ return 0; } long lo

ajax异步参数详解及alax错误信息error分析

一.$.ajax()的参数列表 ↑ 下面是Jquery中AJAX参数详细列表: 参数名 类型 描述 url String (默认: 当前页地址) 发送请求的地址. type String (默认: "GET") 请求方式 ("POST" 或 "GET"), 默认为 "GET".注意:其它 HTTP 请求方法,如 PUT 和 DELETE 也可以使用,但仅部分浏览器支持. timeout Number 设置请求超时时间(毫秒).

sqrt开平方算法的尝试,是的看了卡马克大叔的代码,我来试试用C#写个0x5f3759df和0x5f375a86跟System.Math.Sqrt到底哪个更强

今天笔试遇到一个代码题,要求写一个开平方算法,回来发现了雷神之锤里的一段神代码: 1 float Q_rsqrt( float number ) 2 { 3 long i; 4 float x2, y; 5 const float threehalfs = 1.5F; 6 x2 = number * 0.5F; 7 y = number; 8 i = * ( long * ) &y; // evil floating point bit level hacking 9 i = 0x5f3759d

[email&#160;protected] [343] Integer Break (Math &amp; Dynamic Programming)

https://leetcode.com/problems/integer-break/ Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2

java中根据一元二次方程ax^2+bx+c=0ax 2 +bx+c=0的求根公式 计算出一元二次方程的两个解:

package com.itranswarp.learnjava; /** * 求解 一元二次方程ax^2+bx+c=0 */ public class Main { public static void main(String[] args) { // x*x + 3*x - 4 = 0 double a = 1.0; double b = 3.0; double c = -4.0; // 求平方根可用 Math.sqrt(): // double x = Math.sqrt(2)); //