在 J 中,如何找到平方根的扩展精度整数底
In J, how can I find the extended precision integer floor of a square root
我知道当我对一个不是整数的数字求平方根 (%:
) 时,我的答案是浮点数。我正在寻找平方根的下限 (<.
) 以获得整数结果。 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│
└──┴────────┘
这似乎有效:
sqrt=: <.@%:
sqrt 99999999999999x
有关详细信息,请参阅
http://www.jsoftware.com/help/jforc/elementary_mathematics_in_j.htm
The key is the idiom <.@v (or >.@v), where v is the verb you want to apply. 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.
所以,你必须使用 <.@%:
:
rt2 =: 3 :'(y - root ^ 2);(root =. <.@%: y)'
rt2 99999999999999x
┌────────┬───────┐
│19999998│9999999│
└────────┴───────┘
另见 Dictionary - Extended and Rational Arithmetic
<.@f and >.@f produce extended integer results when applied to extended integer arguments.
我知道当我对一个不是整数的数字求平方根 (%:
) 时,我的答案是浮点数。我正在寻找平方根的下限 (<.
) 以获得整数结果。 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│
└──┴────────┘
这似乎有效:
sqrt=: <.@%:
sqrt 99999999999999x
有关详细信息,请参阅 http://www.jsoftware.com/help/jforc/elementary_mathematics_in_j.htm
The key is the idiom <.@v (or >.@v), where v is the verb you want to apply. 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.
所以,你必须使用 <.@%:
:
rt2 =: 3 :'(y - root ^ 2);(root =. <.@%: y)'
rt2 99999999999999x
┌────────┬───────┐
│19999998│9999999│
└────────┴───────┘
另见 Dictionary - Extended and Rational Arithmetic
<.@f and >.@f produce extended integer results when applied to extended integer arguments.