为了对涉及大量浮点运算的系统进行一些测试,我定义了浮点运算误差的偏差范围,因此如果两个浮点数之间的差异在偏差范围内,则它们被认为在数学上是相等的:
;;; Floating Error Agnostic =
;;; F2 is = to F1 within the deviation range +-DEV
(defparameter *flerag-devi* .0005
"Allowed deviation range, within which two floats
should be considered as mathematically equal.")
(defun flerag= (f1 f2 &optional (devi *flerag-devi*))
""
(<= (abs (- f1 f2)) devi))
Run Code Online (Sandbox Code Playgroud)
现在,当我通过将浮点数与添加到其中的随机分数进行比较来测试函数时,响应是(至少对于我对函数的测试)是正的,例如:
(loop repeat 100000000
with f = 1.0
always (flerag= f (+ f (random *flerag-devi*)))) ;T
Run Code Online (Sandbox Code Playgroud)
当我从原始浮点数中减去一个随机分数时也是如此,例如:
(loop repeat 100000000
with f = 19.0
always (flerag= f (- f …Run Code Online (Sandbox Code Playgroud) 当我有一个应该重复用作参数realloc并保存其返回值的指针时,我知道realloc如果无法进行分配,则不会触及旧对象,返回NULL. 我是否还应该担心以下结构中的旧对象:
int *p = (int *)malloc(sizeof(int *));
if (p = (int *)realloc(p, 2 * sizeof(int *)))
etc...
Run Code Online (Sandbox Code Playgroud)
现在,如果realloc成功了,我需要free(p)在完成后进行。当realloc失败时,我已指定它返回NULL并且p不free(p)执行任何操作(因为它是 a free(NULL))。同时(根据标准)旧对象不会被释放。p那么我是否应该有一个(例如)的副本int *last_p = p;来跟踪旧对象,以便free(last_p)在realloc失败时可以?
在以下循环中:
(let ((funs (loop for i upto 3 do (print i) collect #'(lambda () i))))
(loop for fun in funs collect (funcall fun)))
Run Code Online (Sandbox Code Playgroud)
我直觉地认为我会得到一个包含四个闭包的列表,它们在被调用时返回数字 0 1 2 和 3,但这就是我得到的:
>> 0
>> 1
>> 2
>> 3
=> (4 4 4 4)
Run Code Online (Sandbox Code Playgroud)
但是将i本地重新绑定到其他东西:
(let ((funs (loop for i upto 3 do (print i) collect (let ((x i))
#'(lambda () x)))))
(loop for fun in funs collect (funcall fun)))
Run Code Online (Sandbox Code Playgroud)
按预期工作:
>> 0
>> 1
>> 2
>> 3
=> …Run Code Online (Sandbox Code Playgroud) 有人可以解释以下两种情况之间的区别吗(如果对我来说无法理解的话,特别是评论所说的内容)来自 CLHS 的功能:
;; This function assumes its callers have checked the types of the
;; arguments, and authorizes the compiler to build in that assumption.
(defun discriminant (a b c)
(declare (number a b c))
"Compute the discriminant for a quadratic equation."
(- (* b b) (* 4 a c))) => DISCRIMINANT
(discriminant 1 2/3 -2) => 76/9
;; This function assumes its callers have not checked the types of the
;; arguments, and performs explicit type …Run Code Online (Sandbox Code Playgroud) 是否有一种解决方法可以按顺序处理结构体槽位绑定,LET*以便之前的槽位分配对后面的槽位可见?
例如在下面我想c对d.
(defstruct (my-struct (:constructor cons-struct (a b)))
(c (* a b))
(d c))
Run Code Online (Sandbox Code Playgroud)
我怎么能得到这个效果?
这是 C 新手的另一个幼稚问题:在此页面上,https : //en.cppreference.com/w/c/language/operator_precedence 列出后缀增量的优先级高于指针取消引用的优先级。所以我期望在下面的代码中指针首先递增(指向 10)然后取消引用。
#include <stdio.h>
int main()
{
int a[] = {3, 10, 200};
int *p = a;
printf("%d", *p++);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,此代码仍输出第一个数组项 (3)。这个概念我错过了什么?
我从教科书中了解到,枚举的典型定义如下:
enum weather {
sunny,
windy,
cloudy,
rain,
} weather_outside;
Run Code Online (Sandbox Code Playgroud)
然后声明一个 var,如下所示:
enum weather weather_outside = rain;
Run Code Online (Sandbox Code Playgroud)
我的问题是,如果可以通过说eg来使用枚举常量rain,它保持整数3,那么具有类似类型的更复杂的减速等于3的确切用途和意义是什么enum weather weather_outside = rain;(weather_outside因为枚举值只能是编译时常量)?为什么不直接使用 aconst或 宏呢?我有点困惑枚举是否真的有必要?!
在下面的代码中,我如何让x和y变量反映在宏调用时给出的表达式?
(defmacro defrule (init-form &rest replication-patterns)
(let (rule-table)
`(destructuring-bind (p x y) ',init-form
#'(lambda (w h) (list x y)))))
Run Code Online (Sandbox Code Playgroud)
扩展呼叫时,例如:
(defrule (70 (* 1/2 w) (+ h 3)))
Run Code Online (Sandbox Code Playgroud)
它返回:
(DESTRUCTURING-BIND (P X Y) '(70 (* 1/2 W) (+ H 3))
#'(LAMBDA (W H) (LIST X Y)))
Run Code Online (Sandbox Code Playgroud)
其中带有W和H引用的原始表达式丢失了。我尝试反引用 lambda 函数的创建:
(defmacro defrule (init-form &rest replication-patterns)
(let (rule-table)
`(destructuring-bind (p x y) ',init-form
`#'(lambda (w h) (list ,x ,y)))))
Run Code Online (Sandbox Code Playgroud)
但同样的电话:
(defrule (70 (* 1/2 …Run Code Online (Sandbox Code Playgroud) 在下面的函数中,我用 mac-1 替换了宏,它只返回它工作的参数的值,但 mac-2 抛出一个类型错误。有人可以解释会发生什么并导致此错误吗?为什么 x 从 fun-2 传递给 mac-2 是文字符号x而不是整数?
(defmacro mac-1 (x) x)
(defun fun-1 (x) (mac-1 x))
(fun-1 3) ; => 3
(defmacro mac-2 (x) (+ x 3))
(defun fun-2 (x) (mac-2 x))
(fun-2 3)
Run Code Online (Sandbox Code Playgroud)
执行编译有错误的表单。形式:(MAC-2 X) 编译时错误:在 (MAC-2 X) 的宏扩展期间。使用BREAK-ON-SIGNALS进行拦截。
绑定 SB-KERNEL::X 时,值 X 不是 NUMBER 类型