Double Type的isWhole方法在这里做什么?

时间:2018-07-10 15:19:34

标签: scala floating-point

我在Scala中针对Double类型的isWhole方法的工作进行了测试。 这是我对N=30的测试结果,并且以相同的范围值分配了不同的范围,因此我无法正确地解释下面的第三个结果,即Vector()sqrt函数的返回值中显然应该发生了一些事情,我不知道:

scala> for(i<-1 to 30; if(Math.sqrt(N*N-i*i).isWhole)) yield i
res85: scala.collection.immutable.IndexedSeq[Int] = Vector(18, 24, 30)

scala> for(i<-1.0 to 30.0 by 1.0; if(Math.sqrt(N*N-i*i).isWhole)) yield i
<console>:16: warning: method to in trait FractionalProxy is deprecated (since 2.12.6): use BigDecimal range instead
       for(i<-1.0 to 30.0 by 1.0; if(Math.sqrt(N*N-i*i).isWhole)) yield i
                  ^
res86: scala.collection.immutable.IndexedSeq[Double] = Vector(18.0, 24.0, 30.0)

scala> for(i<-1.0 to 30.0 by 0.1; if(Math.sqrt(N*N-i*i).isWhole)) yield i
<console>:16: warning: method to in trait FractionalProxy is deprecated (since 2.12.6): use BigDecimal range instead
       for(i<-1.0 to 30.0 by 0.1; if(Math.sqrt(N*N-i*i).isWhole)) yield i
                  ^
res87: scala.collection.immutable.IndexedSeq[Double] = Vector()

scala> 1.0 to 30.0 by 0.1
<console>:15: warning: method to in trait FractionalProxy is deprecated (since 2.12.6): use BigDecimal range instead
       1.0 to 30.0 by 0.1
           ^
res88: scala.collection.immutable.NumericRange[Double] = NumericRange 1.0 to 30.0 by 0.1

有任何帮助/说明吗?谢谢。

1 个答案:

答案 0 :(得分:2)

Vector为空。这意味着序列1.0 to 30.0 by 0.1不包含任何Doubles,因此Math.sqrt(N * N - i * i)是一个整数。

由于1.0 to 30.0 by 0.1产生的序列或多或少是随机的,并且整数事件的概率大约为零,因此这是预期的行为。

只需打印序列即可查看发生的情况:

for (i <- 1.0 to 30.0 by 0.1) 
  println(s"i = $i, sqrt(30 * 30 - i * i) = ${math.sqrt(30 * 30 - i * i)}")

输出:

i = 1.0, sqrt(30 * 30 - i * i) = 29.9833287011299
i = 1.1, sqrt(30 * 30 - i * i) = 29.979826550532277
i = 1.2000000000000002, sqrt(30 * 30 - i * i) = 29.975990392312312
i = 1.3000000000000003, sqrt(30 * 30 - i * i) = 29.971820098218927
i = 1.4000000000000004, sqrt(30 * 30 - i * i) = 29.96731552875566
i = 1.5000000000000004, sqrt(30 * 30 - i * i) = 29.962476533157268
i = 1.6000000000000005, sqrt(30 * 30 - i * i) = 29.95730294936445
i = 1.7000000000000006, sqrt(30 * 30 - i * i) = 29.951794603996603
i = 1.8000000000000007, sqrt(30 * 30 - i * i) = 29.945951312322673
i = 1.9000000000000008, sqrt(30 * 30 - i * i) = 29.939772878230055
i = 2.000000000000001, sqrt(30 * 30 - i * i) = 29.93325909419153
i = 2.100000000000001, sqrt(30 * 30 - i * i) = 29.926409741230238
i = 2.200000000000001, sqrt(30 * 30 - i * i) = 29.919224588882646
i = 2.300000000000001, sqrt(30 * 30 - i * i) = 29.911703395159563
i = 2.4000000000000012, sqrt(30 * 30 - i * i) = 29.90384590650507
i = 2.5000000000000013, sqrt(30 * 30 - i * i) = 29.895651857753496
i = 2.6000000000000014, sqrt(30 * 30 - i * i) = 29.88712097208428
i = 2.7000000000000015, sqrt(30 * 30 - i * i) = 29.87825296097481
i = 2.8000000000000016, sqrt(30 * 30 - i * i) = 29.86904752415115
i = 2.9000000000000017, sqrt(30 * 30 - i * i) = 29.85950434953668
i = 3.0000000000000018, sqrt(30 * 30 - i * i) = 29.8496231131986
i = 3.100000000000002, sqrt(30 * 30 - i * i) = 29.839403479292276
i = 3.200000000000002, sqrt(30 * 30 - i * i) = 29.828845100003452
i = 3.300000000000002, sqrt(30 * 30 - i * i) = 29.817947615488226
i = 3.400000000000002, sqrt(30 * 30 - i * i) = 29.80671065381083
i = 3.500000000000002, sqrt(30 * 30 - i * i) = 29.795133830879163
i = 3.6000000000000023, sqrt(30 * 30 - i * i) = 29.783216750378056
i = 3.7000000000000024, sqrt(30 * 30 - i * i) = 29.770959003700234
i = 3.8000000000000025, sqrt(30 * 30 - i * i) = 29.75836016987495
i = 3.9000000000000026, sqrt(30 * 30 - i * i) = 29.745419815494284
i = 4.000000000000003, sqrt(30 * 30 - i * i) = 29.732137494637012
i = 4.100000000000002, sqrt(30 * 30 - i * i) = 29.718512748790104
i = 4.200000000000002, sqrt(30 * 30 - i * i) = 29.704545106767753
i = 4.300000000000002, sqrt(30 * 30 - i * i) = 29.690234084627896
i = 4.400000000000001, sqrt(30 * 30 - i * i) = 29.67557918558625
i = 4.500000000000001, sqrt(30 * 30 - i * i) = 29.660579899927782
i = 4.6000000000000005, sqrt(30 * 30 - i * i) = 29.645235704915553
i = 4.7, sqrt(30 * 30 - i * i) = 29.629546064696974
i = 4.8, sqrt(30 * 30 - i * i) = 29.61351043020736
i = 4.8999999999999995, sqrt(30 * 30 - i * i) = 29.597128239070763
i = 4.999999999999999, sqrt(30 * 30 - i * i) = 29.58039891549808
i = 5.099999999999999, sqrt(30 * 30 - i * i) = 29.563321870182314
i = 5.199999999999998, sqrt(30 * 30 - i * i) = 29.54589650019102
i = 5.299999999999998, sqrt(30 * 30 - i * i) = 29.52812218885583
i = 5.399999999999998, sqrt(30 * 30 - i * i) = 29.50999830565905
i = 5.499999999999997, sqrt(30 * 30 - i * i) = 29.491524206117255
i = 5.599999999999997, sqrt(30 * 30 - i * i) = 29.47269923166183
i = 5.699999999999997, sqrt(30 * 30 - i * i) = 29.453522709516427
i = 5.799999999999996, sqrt(30 * 30 - i * i) = 29.433993952571235
i = 5.899999999999996, sqrt(30 * 30 - i * i) = 29.41411225925406
i = 5.999999999999996, sqrt(30 * 30 - i * i) = 29.393876913398138
i = 6.099999999999995, sqrt(30 * 30 - i * i) = 29.373287184106584
i = 6.199999999999995, sqrt(30 * 30 - i * i) = 29.35234232561347
i = 6.2999999999999945, sqrt(30 * 30 - i * i) = 29.331041577141445
i = 6.399999999999994, sqrt(30 * 30 - i * i) = 29.309384162755794
i = 6.499999999999994, sqrt(30 * 30 - i * i) = 29.287369291214944
i = 6.599999999999993, sqrt(30 * 30 - i * i) = 29.26499615581728
i = 6.699999999999993, sqrt(30 * 30 - i * i) = 29.242263934244217
i = 6.799999999999993, sqrt(30 * 30 - i * i) = 29.219171788399482
i = 6.899999999999992, sqrt(30 * 30 - i * i) = 29.195718864244466
i = 6.999999999999992, sqrt(30 * 30 - i * i) = 29.171904291629644
i = 7.099999999999992, sqrt(30 * 30 - i * i) = 29.147727184121923
i = 7.199999999999991, sqrt(30 * 30 - i * i) = 29.123186638827836
i = 7.299999999999991, sqrt(30 * 30 - i * i) = 29.098281736212538
i = 7.399999999999991, sqrt(30 * 30 - i * i) = 29.07301153991447
i = 7.49999999999999, sqrt(30 * 30 - i * i) = 29.04737509655563
i = 7.59999999999999, sqrt(30 * 30 - i * i) = 29.021371435547287
i = 7.6999999999999895, sqrt(30 * 30 - i * i) = 28.99499956889119
i = 7.799999999999989, sqrt(30 * 30 - i * i) = 28.96825849097595
i = 7.899999999999989, sqrt(30 * 30 - i * i) = 28.941147178368727
i = 7.9999999999999885, sqrt(30 * 30 - i * i) = 28.913664589601925
i = 8.099999999999989, sqrt(30 * 30 - i * i) = 28.88580966495487
i = 8.199999999999989, sqrt(30 * 30 - i * i) = 28.857581326230378
i = 8.299999999999988, sqrt(30 * 30 - i * i) = 28.828978476526014
i = 8.399999999999988, sqrt(30 * 30 - i * i) = 28.800000000000004
i = 8.499999999999988, sqrt(30 * 30 - i * i) = 28.77064476163161
i = 8.599999999999987, sqrt(30 * 30 - i * i) = 28.740911606975867
i = 8.699999999999987, sqrt(30 * 30 - i * i) = 28.710799361912585
i = 8.799999999999986, sqrt(30 * 30 - i * i) = 28.68030683238937
i = 8.899999999999986, sqrt(30 * 30 - i * i) = 28.64943280415862
i = 8.999999999999986, sqrt(30 * 30 - i * i) = 28.618176042508374
i = 9.099999999999985, sqrt(30 * 30 - i * i) = 28.586535291986685
i = 9.199999999999985, sqrt(30 * 30 - i * i) = 28.5545092761196
i = 9.299999999999985, sqrt(30 * 30 - i * i) = 28.522096697122397
i = 9.399999999999984, sqrt(30 * 30 - i * i) = 28.489296235604
i = 9.499999999999984, sqrt(30 * 30 - i * i) = 28.456106550264398
i = 9.599999999999984, sqrt(30 * 30 - i * i) = 28.422526277584833
i = 9.699999999999983, sqrt(30 * 30 - i * i) = 28.38855403151066
i = 9.799999999999983, sqrt(30 * 30 - i * i) = 28.354188403126624
i = 9.899999999999983, sqrt(30 * 30 - i * i) = 28.319427960324347
i = 9.999999999999982, sqrt(30 * 30 - i * i) = 28.284271247461906
i = 10.099999999999982, sqrt(30 * 30 - i * i) = 28.248716785015215
i = 10.199999999999982, sqrt(30 * 30 - i * i) = 28.212763069221
i = 10.299999999999981, sqrt(30 * 30 - i * i) = 28.1764085717112
i = 10.39999999999998, sqrt(30 * 30 - i * i) = 28.1396517391385
i = 10.49999999999998, sqrt(30 * 30 - i * i) = 28.1024909927928
i = 10.59999999999998, sqrt(30 * 30 - i * i) = 28.06492472820835
i = 10.69999999999998, sqrt(30 * 30 - i * i) = 28.026951314761305
i = 10.79999999999998, sqrt(30 * 30 - i * i) = 27.98856909525745
i = 10.899999999999979, sqrt(30 * 30 - i * i) = 27.949776385509786
i = 10.999999999999979, sqrt(30 * 30 - i * i) = 27.910571473905733
i = 11.099999999999978, sqrt(30 * 30 - i * i) = 27.87095262096365
i = 11.199999999999978, sqrt(30 * 30 - i * i) = 27.83091805887834
i = 11.299999999999978, sqrt(30 * 30 - i * i) = 27.79046599105529
i = 11.399999999999977, sqrt(30 * 30 - i * i) = 27.749594591633237
i = 11.499999999999977, sqrt(30 * 30 - i * i) = 27.70830200499483
i = 11.599999999999977, sqrt(30 * 30 - i * i) = 27.666586345264943
i = 11.699999999999976, sqrt(30 * 30 - i * i) = 27.624445695796332
i = 11.799999999999976, sqrt(30 * 30 - i * i) = 27.581878108642286
i = 11.899999999999975, sqrt(30 * 30 - i * i) = 27.538881604015813
i = 11.999999999999975, sqrt(30 * 30 - i * i) = 27.49545416973505
i = 12.099999999999975, sqrt(30 * 30 - i * i) = 27.45159376065442
i = 12.199999999999974, sqrt(30 * 30 - i * i) = 27.407298298081127
i = 12.299999999999974, sqrt(30 * 30 - i * i) = 27.362565669176576
i = 12.399999999999974, sqrt(30 * 30 - i * i) = 27.31739372634221
i = 12.499999999999973, sqrt(30 * 30 - i * i) = 27.271780286589298
i = 12.599999999999973, sqrt(30 * 30 - i * i) = 27.225723130892238
i = 12.699999999999973, sqrt(30 * 30 - i * i) = 27.179220003524765
i = 12.799999999999972, sqrt(30 * 30 - i * i) = 27.132268611378606
i = 12.899999999999972, sqrt(30 * 30 - i * i) = 27.08486662326401
i = 12.999999999999972, sqrt(30 * 30 - i * i) = 27.03701166919156
i = 13.099999999999971, sqrt(30 * 30 - i * i) = 26.988701339634716
i = 13.19999999999997, sqrt(30 * 30 - i * i) = 26.939933184772393
i = 13.29999999999997, sqrt(30 * 30 - i * i) = 26.890704713711035
i = 13.39999999999997, sqrt(30 * 30 - i * i) = 26.841013393685433
i = 13.49999999999997, sqrt(30 * 30 - i * i) = 26.790856649237643
i = 13.59999999999997, sqrt(30 * 30 - i * i) = 26.740231861373246
i = 13.699999999999969, sqrt(30 * 30 - i * i) = 26.689136366694235
i = 13.799999999999969, sqrt(30 * 30 - i * i) = 26.637567456507753
i = 13.899999999999968, sqrt(30 * 30 - i * i) = 26.585522375909804
i = 13.999999999999968, sqrt(30 * 30 - i * i) = 26.532998322843216
i = 14.099999999999968, sqrt(30 * 30 - i * i) = 26.47999244712885
i = 14.199999999999967, sqrt(30 * 30 - i * i) = 26.426501849469236
i = 14.299999999999967, sqrt(30 * 30 - i * i) = 26.37252358042364
i = 14.399999999999967, sqrt(30 * 30 - i * i) = 26.31805463935359
i = 14.499999999999966, sqrt(30 * 30 - i * i) = 26.26309197333781
i = 14.599999999999966, sqrt(30 * 30 - i * i) = 26.20763247605554
i = 14.699999999999966, sqrt(30 * 30 - i * i) = 26.15167298663703
i = 14.799999999999965, sqrt(30 * 30 - i * i) = 26.095210288480164
i = 14.899999999999965, sqrt(30 * 30 - i * i) = 26.038241108031876
i = 14.999999999999964, sqrt(30 * 30 - i * i) = 25.980762113533178
i = 15.099999999999964, sqrt(30 * 30 - i * i) = 25.922769913726448
i = 15.199999999999964, sqrt(30 * 30 - i * i) = 25.864261056523556
i = 15.299999999999963, sqrt(30 * 30 - i * i) = 25.805232027633487
i = 15.399999999999963, sqrt(30 * 30 - i * i) = 25.74567924914783
i = 15.499999999999963, sqrt(30 * 30 - i * i) = 25.685599078082667
i = 15.599999999999962, sqrt(30 * 30 - i * i) = 25.624987804875172
i = 15.699999999999962, sqrt(30 * 30 - i * i) = 25.563841651833183
i = 15.799999999999962, sqrt(30 * 30 - i * i) = 25.502156771536036
i = 15.899999999999961, sqrt(30 * 30 - i * i) = 25.43992924518465
i = 15.999999999999961, sqrt(30 * 30 - i * i) = 25.377155080899065
i = 16.099999999999962, sqrt(30 * 30 - i * i) = 25.313830211961232
i = 16.199999999999964, sqrt(30 * 30 - i * i) = 25.249950495001
i = 16.299999999999965, sqrt(30 * 30 - i * i) = 25.18551170812301
i = 16.399999999999967, sqrt(30 * 30 - i * i) = 25.12050954897215
i = 16.499999999999968, sqrt(30 * 30 - i * i) = 25.05493963273512
i = 16.59999999999997, sqrt(30 * 30 - i * i) = 24.98879749007545
i = 16.69999999999997, sqrt(30 * 30 - i * i) = 24.92207856499937
i = 16.799999999999972, sqrt(30 * 30 - i * i) = 24.85477821264959
i = 16.899999999999974, sqrt(30 * 30 - i * i) = 24.78689169702407
i = 16.999999999999975, sqrt(30 * 30 - i * i) = 24.71841418861657
i = 17.099999999999977, sqrt(30 * 30 - i * i) = 24.649340761975782
i = 17.199999999999978, sqrt(30 * 30 - i * i) = 24.57966639317956
i = 17.29999999999998, sqrt(30 * 30 - i * i) = 24.509385957220566
i = 17.39999999999998, sqrt(30 * 30 - i * i) = 24.438494225299575
i = 17.499999999999982, sqrt(30 * 30 - i * i) = 24.366985862022425
i = 17.599999999999984, sqrt(30 * 30 - i * i) = 24.294855422496354
i = 17.699999999999985, sqrt(30 * 30 - i * i) = 24.222097349321352
i = 17.799999999999986, sqrt(30 * 30 - i * i) = 24.14870596947175
i = 17.899999999999988, sqrt(30 * 30 - i * i) = 24.074675491063225
i = 17.99999999999999, sqrt(30 * 30 - i * i) = 24.00000000000001
i = 18.09999999999999, sqrt(30 * 30 - i * i) = 23.924673456496755
i = 18.199999999999992, sqrt(30 * 30 - i * i) = 23.848689691469428
i = 18.299999999999994, sqrt(30 * 30 - i * i) = 23.772042402789044
i = 18.399999999999995, sqrt(30 * 30 - i * i) = 23.694725151391822
i = 18.499999999999996, sqrt(30 * 30 - i * i) = 23.61673135723909
i = 18.599999999999998, sqrt(30 * 30 - i * i) = 23.53805429511964
i = 18.7, sqrt(30 * 30 - i * i) = 23.458687090287043
i = 18.8, sqrt(30 * 30 - i * i) = 23.37862271392393
i = 18.900000000000002, sqrt(30 * 30 - i * i) = 23.29785397842471
i = 19.000000000000004, sqrt(30 * 30 - i * i) = 23.216373532487797
i = 19.100000000000005, sqrt(30 * 30 - i * i) = 23.13417385600791
i = 19.200000000000006, sqrt(30 * 30 - i * i) = 23.05124725475825
i = 19.300000000000008, sqrt(30 * 30 - i * i) = 22.967585854852047
i = 19.40000000000001, sqrt(30 * 30 - i * i) = 22.883181596972037
i = 19.50000000000001, sqrt(30 * 30 - i * i) = 22.798026230355987
i = 19.600000000000012, sqrt(30 * 30 - i * i) = 22.71211130652541
i = 19.700000000000014, sqrt(30 * 30 - i * i) = 22.625428172744034
i = 19.800000000000015, sqrt(30 * 30 - i * i) = 22.537967965191527
i = 19.900000000000016, sqrt(30 * 30 - i * i) = 22.44972160183728
i = 20.000000000000018, sqrt(30 * 30 - i * i) = 22.36067977499788
i = 20.10000000000002, sqrt(30 * 30 - i * i) = 22.27083294356094
i = 20.20000000000002, sqrt(30 * 30 - i * i) = 22.180171324856786
i = 20.300000000000022, sqrt(30 * 30 - i * i) = 22.088684886158322
i = 20.400000000000023, sqrt(30 * 30 - i * i) = 21.99636333578801
i = 20.500000000000025, sqrt(30 * 30 - i * i) = 21.903196113809486
i = 20.600000000000026, sqrt(30 * 30 - i * i) = 21.80917238227987
i = 20.700000000000028, sqrt(30 * 30 - i * i) = 21.71428101503706
i = 20.80000000000003, sqrt(30 * 30 - i * i) = 21.618510586994628
i = 20.90000000000003, sqrt(30 * 30 - i * i) = 21.52184936291486
i = 21.000000000000032, sqrt(30 * 30 - i * i) = 21.424285285628518
i = 21.100000000000033, sqrt(30 * 30 - i * i) = 21.325805963667555
i = 21.200000000000035, sqrt(30 * 30 - i * i) = 21.226398658274523
i = 21.300000000000036, sqrt(30 * 30 - i * i) = 21.126050269749868
i = 21.400000000000038, sqrt(30 * 30 - i * i) = 21.02474732309519
i = 21.50000000000004, sqrt(30 * 30 - i * i) = 20.922475952907636
i = 21.60000000000004, sqrt(30 * 30 - i * i) = 20.819221887476925
i = 21.700000000000042, sqrt(30 * 30 - i * i) = 20.71497043203292
i = 21.800000000000043, sqrt(30 * 30 - i * i) = 20.60970645108751
i = 21.900000000000045, sqrt(30 * 30 - i * i) = 20.503414349810082
i = 22.000000000000046, sqrt(30 * 30 - i * i) = 20.39607805437109
i = 22.100000000000048, sqrt(30 * 30 - i * i) = 20.28768099118275
i = 22.20000000000005, sqrt(30 * 30 - i * i) = 20.17820606496023
i = 22.30000000000005, sqrt(30 * 30 - i * i) = 20.067635635520137
i = 22.400000000000052, sqrt(30 * 30 - i * i) = 19.955951493226216
i = 22.500000000000053, sqrt(30 * 30 - i * i) = 19.84313483298437
i = 22.600000000000055, sqrt(30 * 30 - i * i) = 19.72916622668068
i = 22.700000000000056, sqrt(30 * 30 - i * i) = 19.61402559394673
i = 22.800000000000058, sqrt(30 * 30 - i * i) = 19.497692171126236
i = 22.90000000000006, sqrt(30 * 30 - i * i) = 19.380144478305557
i = 23.00000000000006, sqrt(30 * 30 - i * i) = 19.261360284258153
i = 23.100000000000062, sqrt(30 * 30 - i * i) = 19.14131656913905
i = 23.200000000000063, sqrt(30 * 30 - i * i) = 19.019989484749907
i = 23.300000000000065, sqrt(30 * 30 - i * i) = 18.89735431217812
i = 23.400000000000066, sqrt(30 * 30 - i * i) = 18.773385416594337
i = 23.500000000000068, sqrt(30 * 30 - i * i) = 18.64805619897143
i = 23.60000000000007, sqrt(30 * 30 - i * i) = 18.521339044464273
i = 23.70000000000007, sqrt(30 * 30 - i * i) = 18.393205267163108
i = 23.80000000000007, sqrt(30 * 30 - i * i) = 18.263625050903684
i = 23.900000000000073, sqrt(30 * 30 - i * i) = 18.13256738578397
i = 24.000000000000075, sqrt(30 * 30 - i * i) = 17.9999999999999
i = 24.100000000000076, sqrt(30 * 30 - i * i) = 17.86588928657055
i = 24.200000000000077, sqrt(30 * 30 - i * i) = 17.730200224475645
i = 24.30000000000008, sqrt(30 * 30 - i * i) = 17.592896293674787
i = 24.40000000000008, sqrt(30 * 30 - i * i) = 17.453939383417033
i = 24.50000000000008, sqrt(30 * 30 - i * i) = 17.31328969318067
i = 24.600000000000083, sqrt(30 * 30 - i * i) = 17.170905625504904
i = 24.700000000000085, sqrt(30 * 30 - i * i) = 17.026743669885793
i = 24.800000000000086, sqrt(30 * 30 - i * i) = 16.880758276807228
i = 24.900000000000087, sqrt(30 * 30 - i * i) = 16.73290172086108
i = 25.00000000000009, sqrt(30 * 30 - i * i) = 16.583123951776866
i = 25.10000000000009, sqrt(30 * 30 - i * i) = 16.431372432027565
i = 25.20000000000009, sqrt(30 * 30 - i * i) = 16.2775919595005
i = 25.300000000000093, sqrt(30 * 30 - i * i) = 16.12172447351695
i = 25.400000000000095, sqrt(30 * 30 - i * i) = 15.96370884224575
i = 25.500000000000096, sqrt(30 * 30 - i * i) = 15.803480629278953
i = 25.600000000000097, sqrt(30 * 30 - i * i) = 15.640971836813561
i = 25.7000000000001, sqrt(30 * 30 - i * i) = 15.476110622504443
i = 25.8000000000001, sqrt(30 * 30 - i * i) = 15.308820986607518
i = 25.9000000000001, sqrt(30 * 30 - i * i) = 15.139022425506699
i = 26.000000000000103, sqrt(30 * 30 - i * i) = 14.966629547095588
i = 26.100000000000104, sqrt(30 * 30 - i * i) = 14.791551642745075
i = 26.200000000000106, sqrt(30 * 30 - i * i) = 14.613692209705064
i = 26.300000000000107, sqrt(30 * 30 - i * i) = 14.432948416730186
i = 26.40000000000011, sqrt(30 * 30 - i * i) = 14.2492105044453
i = 26.50000000000011, sqrt(30 * 30 - i * i) = 14.062361110425027
i = 26.60000000000011, sqrt(30 * 30 - i * i) = 13.872274507087655
i = 26.700000000000113, sqrt(30 * 30 - i * i) = 13.678815738213377
i = 26.800000000000114, sqrt(30 * 30 - i * i) = 13.481839637081945
i = 26.900000000000116, sqrt(30 * 30 - i * i) = 13.281189705745255
i = 27.000000000000117, sqrt(30 * 30 - i * i) = 13.076696830621778
i = 27.10000000000012, sqrt(30 * 30 - i * i) = 12.868177804180107
i = 27.20000000000012, sqrt(30 * 30 - i * i) = 12.655433615644842
i = 27.30000000000012, sqrt(30 * 30 - i * i) = 12.43824746497646
i = 27.400000000000123, sqrt(30 * 30 - i * i) = 12.216382443260088
i = 27.500000000000124, sqrt(30 * 30 - i * i) = 11.989578808281514
i = 27.600000000000126, sqrt(30 * 30 - i * i) = 11.75755076535896
i = 27.700000000000127, sqrt(30 * 30 - i * i) = 11.519982638875502
i = 27.80000000000013, sqrt(30 * 30 - i * i) = 11.27652428720804
i = 27.90000000000013, sqrt(30 * 30 - i * i) = 11.026785569693136
i = 28.00000000000013, sqrt(30 * 30 - i * i) = 10.770329614268665
i = 28.100000000000133, sqrt(30 * 30 - i * i) = 10.506664551606875
i = 28.200000000000134, sqrt(30 * 30 - i * i) = 10.235233265538817
i = 28.300000000000136, sqrt(30 * 30 - i * i) = 9.955400544427746
i = 28.400000000000137, sqrt(30 * 30 - i * i) = 9.666436778875255
i = 28.50000000000014, sqrt(30 * 30 - i * i) = 9.36749699759718
i = 28.60000000000014, sqrt(30 * 30 - i * i) = 9.05759349937896
i = 28.70000000000014, sqrt(30 * 30 - i * i) = 8.735559512703915
i = 28.800000000000143, sqrt(30 * 30 - i * i) = 8.39999999999951
i = 28.900000000000144, sqrt(30 * 30 - i * i) = 8.049223565039778
i = 29.000000000000146, sqrt(30 * 30 - i * i) = 7.681145747868061
i = 29.100000000000147, sqrt(30 * 30 - i * i) = 7.293147468685342
i = 29.20000000000015, sqrt(30 * 30 - i * i) = 6.881860213633475
i = 29.30000000000015, sqrt(30 * 30 - i * i) = 6.442825467137165
i = 29.40000000000015, sqrt(30 * 30 - i * i) = 5.969924622638976
i = 29.500000000000153, sqrt(30 * 30 - i * i) = 5.4543560573170335
i = 29.600000000000154, sqrt(30 * 30 - i * i) = 4.8826222462925415
i = 29.700000000000156, sqrt(30 * 30 - i * i) = 4.232020793898673
i = 29.800000000000157, sqrt(30 * 30 - i * i) = 3.4583232931567576
i = 29.90000000000016, sqrt(30 * 30 - i * i) = 2.4474476501021574
i = 30.00000000000016, sqrt(30 * 30 - i * i) = NaN

对于i = 24.000000000000075,平方根为17.9999999999999,但这当然不是整数。

您可能想刷新IEEE-754的工作方式,此后永远不要将==上的.isWholeDouble与数值计算混合使用。