小编Stu*_*ent的帖子

在 Common Lisp 中测试一个不可知的浮点错误=

为了对涉及大量浮点运算的系统进行一些测试,我定义了浮点运算误差的偏差范围,因此如果两个浮点数之间的差异在偏差范围内,则它们被认为在数学上是相等的:

;;; 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)

common-lisp

7
推荐指数
1
解决办法
95
查看次数

释放从 realloc 返回的空指针?

当我有一个应该重复用作参数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并且pfree(p)执行任何操作(因为它是 a free(NULL))。同时(根据标准)旧对象不会被释放。p那么我是否应该有一个(例如)的副本int *last_p = p;来跟踪旧对象,以便free(last_p)realloc失败时可以?

c memory realloc

6
推荐指数
2
解决办法
815
查看次数

公共 Lisp 循环

在以下循环中:

(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)

loops common-lisp

4
推荐指数
1
解决办法
170
查看次数

声明检查类型之间的 Common Lisp 区别

有人可以解释以下两种情况之间的区别吗(如果对我来说无法理解的话,特别是评论所说的内容)来自 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)

common-lisp declare

3
推荐指数
1
解决办法
601
查看次数

Common Lisp 顺序处理结构槽

是否有一种解决方法可以按顺序处理结构体槽位绑定,LET*以便之前的槽位分配对后面的槽位可见?

例如在下面我想cd.

(defstruct (my-struct (:constructor cons-struct (a b)))
  (c (* a b))
  (d c))
Run Code Online (Sandbox Code Playgroud)

我怎么能得到这个效果?

struct common-lisp

2
推荐指数
1
解决办法
57
查看次数

C 运算符优先级后缀增量和取消引用

这是 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)。这个概念我错过了什么?

c operator-precedence

2
推荐指数
1
解决办法
70
查看次数

为什么枚举在 C 中作为一种类型存在

我从教科书中了解到,枚举的典型定义如下:

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或 宏呢?我有点困惑枚举是否真的有必要?!

c enums

2
推荐指数
1
解决办法
1931
查看次数

Common Lisp 宏中的闭包

在下面的代码中,我如何让xy变量反映在宏调用时给出的表达式?

(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)

其中带有WH引用的原始表达式丢失了。我尝试反引用 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)

macros common-lisp

1
推荐指数
1
解决办法
74
查看次数

Common Lisp 宏宏扩展时间参数输入

在下面的函数中,我用 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 类型

lisp macros common-lisp

0
推荐指数
2
解决办法
246
查看次数

标签 统计

common-lisp ×6

c ×3

macros ×2

declare ×1

enums ×1

lisp ×1

loops ×1

memory ×1

operator-precedence ×1

realloc ×1

struct ×1