- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试实现 law of cosines函数,这是我的代码:
cosC :: [a] -> a
cosC sides
| length sides < 3 = 0
| otherwise = (x ^ 2 + y ^ 2 - z ^ 2) / (2 * x * y)
where x = head(tail(tail(sides)))
y = head(tail(sides))
z = head(sides)
但是我收到两个错误:
No instance for (Fractional a)
arising from a use of `/'
In the expression: (x ^ 2 + y ^ 2 - z ^ 2) / (2 * x * y)
In an equation for `cosC':
cosC sides
| length sides < 3 = 0
| otherwise = (x ^ 2 + y ^ 2 - z ^ 2) / (2 * x * y)
where
x = head (tail (tail (sides)))
y = head (tail (sides))
z = head (sides)
和
No instance for (Num a)
arising from the literal `2'
In the first argument of `(*)', namely `2'
In the first argument of `(*)', namely `2 * x'
In the second argument of `(/)', namely `(2 * x * y)'
编辑:我已经修复了上面余弦定理中的符号拼写错误。感谢 Daniel Fischer 指出了这一点。
最佳答案
您正在尝试从一般类型 a
中计算数值结果,这是不可能的。 (这就像试图不仅为一般公路车辆 build 一座桥梁,而且为一般事物 build 一座桥梁,例如宇宙飞船、摩天大楼、回形针和中子星)。只需将 Floating
约束添加到:
cosC :: Floating a => [a] -> a
您可以执行此类计算所需的任何算术运算。 (Fractional
实际上对于这个函数来说已经足够了,但是你将无法计算结果的 arccos
)。
与您的问题无关,请注意,在 Haskell 中有一种更好的分解列表的方法:
cosC (x:y:z:_) = (x^2 + y^2 - z^2) / (2*x*y)
cosC _ = 0
相当于你的定义。无论如何,你为什么要把这些论点作为一个列表?这是一个相当 Lisp 风格的事情,我更喜欢用 Haskell 来做
cosC :: Floating a => a -> a -> a -> a
cosC x y z = (x^2 + y^2 - z^2) / (2*x*y)
关于Haskell 余弦定理的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13645874/
我有一个例程,它将接受 union 参数 d、theta、a 和 alpha 作为输入,并将生成相应的 4x4 齐次矩阵作为输出。我已经测试了我的矩阵乘法,它确实工作正常。我将从输入中得到 5 个矩阵
我有以下代码使用预先计算的内存表执行 Sin/Cos 函数。在下面的例子中,表格有 1024*128 个项目,涵盖了从 0 到 2pi 的所有 Sin/Cos 值。我知道我可以使用 Sin/Cos 对
问题是不言自明的。我看过几个 pi 的例子,但没有看到 trigo 函数。也许可以使用泰勒级数 as done here但我不完全确定如何在 python 中实现它。特别是如何存储这么多数字。我应该提
我开始学习 Python 中的数学模块,并试图围绕 Python 中的正弦、余弦和正切的三角函数。 我花了一些时间学习更多关于三角学的知识,并了解了直角三角形的基本公式是如何工作的: 正弦函数: si
我一个月前才开始学习这门 C++ 类(class)。现在我被分配去写一个程序来计算这个。我不知道我做错了什么。 #include #include float gatherl1(); float
这个问题在这里已经有了答案: Python cos(90) and cos(270) not 0 (3 个答案) 关闭 9 年前。 有没有办法获得角度(以弧度为单位)的精确正切/余弦/正弦? mat
这个问题在这里已经有了答案: Java Math.cos(Math.toRadians()) returns weird values (4 个答案) 关闭 10 年前。 我正在编写一个程序,我必须
我做了一个简单的计算器作为我的第一个android程序,现在我想给它添加trigonometry,log等函数。在 C 中,我们必须包含 math library 才能这样做,我似乎无法弄清楚你是如何
我正面临 objective-c 中 cos 函数的奇怪问题。我安装了带有 iOS 4.3 SDK 的 xcode 4.1.1。 我正在计算一个数的余弦值: y= cos(x*M_PI/180) 这将
尝试将以下 php 方法转换为在 .less 样式表中使用: 在 Less 中,如何在不使用特定语言的 cos()/sin() 函数的情况下实现正弦/余弦方法? .rotate(@deg) {
可以使用 iPhone 3GS 或 Pandora 的人请测试我刚刚编写的以下组装程序吗? 它应该在 NEON 矢量 FPU 上非常快速地计算正弦和余弦。我知道它编译得很好,但没有足够的硬件我无法测试
我为泰勒级数编写了以下函数来计算余弦。 double cosine(int x) { x %= 360; // make it less than 360 double rad = x
我是一名优秀的程序员,十分优秀!