请教
lisp吧
全部回复
仅看楼主
level 7
TaTaMaD 楼主
(setf afn #'(lambda (b3 b5 c1 p) ;P为T时,加1后加小数点;为NIL时,不加1仅增加小数点
(let ((c5 (1- (length b5))))
(if p
(if (< c5 c1)
(read-from-string (concatenate 'string b5 ".0"))
(read-from-string (concatenate 'string (subseq b5 0 c1) "." (subseq b5 c1))))
(if (< (1- (length b3)) c1)
(read-from-string (concatenate 'string b3 ".0"))
(read-from-string (concatenate 'string (subseq b3 0 c1) "." (subseq b3 c1))))))))
当(funcall #'afn "1234" "1234" 2 t)的时候提示afn没有定义,为啥呢?
@yjqww6 帮忙看看哪里不对
2015年03月23日 10点03分 1
level 8
命名空间问题,去掉afn前面的#'
2015年03月23日 11点03分 2
谢谢[酷]。我写(funcall #'用defun定义的函数的名字 参数)习惯了~~~忘了lambda前面本就有function的简写。
2015年03月23日 11点03分
level 8
随便堆了个,估计和你的不太一样
(defun char-to-int (c)
(- (char-code c) 48))
(defun int-to-char (i)
(code-char (+ i 48)))
(defun to-even (i)
(if (evenp i) i (+ i 1)))
(defun convert (f n)
(if (< f 0)
(return-from convert (- (convert (- f) n))))
(let* ((str (format nil "~e" f))
(e-pos (position #\E str))
(digits (concatenate 'string (subseq str 0 1) (subseq str 2 e-pos)))
(new-digits
(with-output-to-string (s)
(let ((len (length digits)))
(dotimes (i n)
(cond
((>= i len) (princ #\0 s))
((= i (- n 1)) (if (< n len)
(let ((next (char-to-int (char digits n)))
(this (char-to-int (char digits i))))
(cond ((>= next 6) (princ (+ 1 this) s))
((<= next 4) (princ this s))
(t (princ (to-even this) s))))
(princ (char digits i) s)))
(t (princ (char digits i) s))))))))
(values (read-from-string (concatenate 'string (subseq new-digits 0 1)
"." (subseq new-digits 1) (subseq str e-pos)))))))
2015年03月28日 15点03分 5
谢谢[开心]
2015年03月29日 09点03分
该死的百度,总是删我的帖子[不高兴],真心讨厌百度这个公司。
2015年03月29日 09点03分
我测试了一下,0.3512取三位有效数字的时候,convert返回0.351,sisheliuru返回0.35,23438时候,convert--23400.0 ,sisheliuru:23400。我写的那个sisheliuru是一个测试版本,你可以把打印的效果关掉、返回值的标号关掉。
2015年03月29日 09点03分
@TaTaMaD 我那里返回的是浮点数,打印的话会有多余部分,不过不影响运算;0.3512是3.512E-1,三位有效数字就是3.51E-1,没有问题。
2015年03月29日 10点03分
level 7
TaTaMaD 楼主
我做了个测试,你看看吧。
测试数据:
(setf c1 '(9.8249 9.82671 9.8350 9.8351 9.8250 9.82501 28.2345 28.2645 28.350 28.250 28.2501 2.1545446 23438 12.1498 12.1498 1268 10.5002 1.0501 0.3512))
测试方法:
(defun s1 (lst num);
(dolist (x lst)
(format t "~A:~20tconvert--~A~50tsisheliuru:~A~%" x (convert x num) (sisheliuru x num))))
(s1 c1 3),表示对c1中的每个数据取三位有效数字。
测试结果:
9.8249: convert--9.82 sisheliuru:9.82
9.82671: convert--9.83 sisheliuru:9.83
9.835: convert--9.84 sisheliuru:9.84
9.8351: convert--9.84 sisheliuru:9.84
9.825: convert--9.82 sisheliuru:9.82
9.82501: convert--9.82 sisheliuru:9.83
28.2345: convert--28.2 sisheliuru:28.2
28.2645: convert--28.3 sisheliuru:28.3
28.35: convert--28.4 sisheliuru:28.4
28.25: convert--28.2 sisheliuru:28.2
28.2501: convert--28.2 sisheliuru:28.3
2.1545446: convert--2.15 sisheliuru:2.15
23438: convert--23400.0 sisheliuru:23400
12.1498: convert--12.1 sisheliuru:12.1
12.1498: convert--12.1 sisheliuru:12.1
1268: convert--1270.0 sisheliuru:1270
10.5002: convert--10.5 sisheliuru:10.5
1.0501: convert--1.05 sisheliuru:1.05
0.3512: convert--0.351 sisheliuru:0.35
2015年03月29日 08点03分 6
1