【发布时间】:2020-01-05 01:55:56
【问题描述】:
如何在 F# 中对 uInt64 求平方根?
我试过了:
sqrt(1UL) // the type 'uint64' does not support the operator 'Sqrt'
//and
1UL**0.5 //the type 'uint64' does not support the operator 'Pow'
您可以使用pown 做整数指数,但不能替换sqrt,因为幂只能是整数。
pown 4 2 // 16
【问题讨论】:
-
为什么不直接转换成
double? -
请在发帖前考虑一下您的问题。你很快就会发现你也不能取
int的平方根。它还会直接引出一个问题:int或int64的平方根有什么类型,您希望它的行为如何? -
@CharlesRoddie 我不是函数式程序员,所以也许我没有看到你在说什么,但我绝对希望能够找到 int 的根。例如,
sqrt(4) = 2或sqrt(3) = 1.73...。我希望它表现得像数学函数,我输入一个数字并取出一个数字,而无需显式输入所有内容 -
@NicoSchertler 如果我没记错的话,double 是有符号的 64 位数字,uInt64 是无符号的 64 位数字。因此,我认为 uInt64 可能无法正确转换为双精度。
-
当你说“我希望它表现得像数学函数”时,你所说的“数学函数”的类型是什么:即域和余域是什么。