【问题标题】:In J, how can I find the extended precision integer floor of a square root在 J 中,我怎样才能找到平方根的扩展精度整数下限
【发布时间】:2015-07-26 07:58:52
【问题描述】:

我知道,当我对一个不产生整数的数字求平方根 (%:) 时,我的答案是浮点数。我正在寻找平方根的下限 (<.) 以获得整数结果。 J 是否有内置的方法来实现这一点?我需要借助循环来找到答案吗?

折腾几个扩展精度 (x:) 请求肯定不行。

   rootanddiffa =: 3 : '(y - root ^ 2);(root =. <. %: y)'
   rootanddiffa 24
┌─┬─┐
│8│4│
└─┴─┘
   rootanddiffa 26
┌─┬─┐
│1│5│
└─┴─┘
   rootanddiffa 99999999999999x
┌──┬────────┐
│_1│10000000│
└──┴────────┘
   rootanddiffb =: 3 : '(y - root ^ 2);(root =. x: <. x: %: y)'
   rootanddiffb 24
┌─┬─┐
│8│4│
└─┴─┘
   rootanddiffb 99999999999999x
┌──┬────────┐
│_1│10000000│
└──┴────────┘

【问题讨论】:

    标签: j extended-precision


    【解决方案1】:

    来自"J for C Programmers: 32"

    关键是成语<.>.@v),其中v是你要应用的动词。当您编写 <.>

    所以,你必须使用&lt;.@%:

    rt2 =: 3 :'(y - root ^ 2);(root =. <.@%: y)'
    rt2 99999999999999x
    ┌────────┬───────┐
    │19999998│9999999│
    └────────┴───────┘
    

    另见Dictionary - Extended and Rational Arithmetic

    <.>.@f 在应用于扩展整数参数时产生扩展整数结果。

    【讨论】:

    • J 背后有足够的智慧,我只知道必须有办法做到这一点。
    【解决方案2】:

    这似乎有效:

    sqrt=: <.@%:
    sqrt 99999999999999x
    

    有关详细信息,请参阅 http://www.jsoftware.com/help/jforc/elementary_mathematics_in_j.htm

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-04
      • 2017-09-23
      • 2019-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多