[英]In J, how can I find the extended precision integer floor of a square root
I understand that when I take the square root ( %:
) of a number that does not result in an integer, my answer is a float. 我理解当我取一个不产生整数的数字的平方根(
%:
:)时,我的答案是浮点数。 I'm looking to find the floor ( <.
) of the square root in order to get an integer result. 我想找到平方根的底面(
<.
)以获得整数结果。 Does J have a built-in way to achieve this? J有内置的方法来实现这一目标吗? Do I need to resort to a loop to find my answer?
我是否需要求助于循环才能找到答案?
Tossing in a few extended precision ( x:
) requests certainly doesn't do it. 抛出几个扩展精度(
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│
└──┴────────┘
From "J for C Programmers: 32" : 从“J for C Programmers:32” :
The key is the idiom <.@v (or >.@v), where v is the verb you want to apply.
关键是成语<。@ v(或>。@ v),其中v是您要应用的动词。 When you code <.@v, the interpreter knows you are interested in only the integer part of the result, and if the operand is exact-precision, the interpreter will evaluate the integer part of the result exactly.
当您编码<。@ v时,解释器知道您只对结果的整数部分感兴趣,并且如果操作数是精确精度,则解释器将精确地计算结果的整数部分。
So, you have to use <.@%:
: 所以,你必须使用
<.@%:
::
rt2 =: 3 :'(y - root ^ 2);(root =. <.@%: y)'
rt2 99999999999999x
┌────────┬───────┐
│19999998│9999999│
└────────┴───────┘
See also Dictionary - Extended and Rational Arithmetic 另请参见字典 - 扩展和Rational算术
<.@f and >.@f produce extended integer results when applied to extended integer arguments.
<。@ f和>。@ f在应用于扩展整数参数时产生扩展的整数结果。
This appears to work: 这似乎有效:
sqrt=: <.@%:
sqrt 99999999999999x
For more info, see http://www.jsoftware.com/help/jforc/elementary_mathematics_in_j.htm 有关详细信息,请参阅http://www.jsoftware.com/help/jforc/elementary_mathematics_in_j.htm
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.