我这里有一个简单的lisp程序,它计算单位间隔随机均匀选择的两个点之间的平均长度的近似值。如果我运行程序,我得到一个有理数16666671666667/50000000000000
,但是当我(天真地)尝试将有理数格式化为20个位置时,一些精度被抛弃0.33333343000000000000
。我认为,在引擎盖之下,SBCL在格式化之前将理性转换为浮点数,但我不确定该怎么说。我只是使用表达式(format t "~20$~%" (scale-all-line-contributions 10000000 1)
。有没有办法将有理数转换为十进制符号,尽可能保持精度?我理解格式系统功能强大且范围广泛,但我很难找到与有理数相关的文档。
以下是完整性代码'因为它不长,所以。
(defun number-of-pairs (n i)
"get the number of pairs with distance x
for non-zero distances we have to consider two cases"
(cond
((= i 0) n)
((> i 0) (* 2 (- n i)))
((> i n) 0)))
(defun line-contribution (n i power)
"get the number of segments of length i in a line of n segments and their weight combined"
(let
((number-of-pairs (number-of-pairs n i))
(weight-of-pair (expt i power)))
(* number-of-pairs weight-of-pair)))
(defun all-line-contributions (n power)
"get the line contributions for reach [0 .. n]"
(loop for i from 1 upto (- n 1) summing (line-contribution n i power)))
(defun normalized-all-line-contributions (n power)
"normalize line contributions by number of pairs"
(let ((pair-count (expt n 2)))
(/ (all-line-contributions n power) pair-count)))
(defun scale-all-line-contributions (n power)
"scale the line contributions by the distance n
this will guarantee convergence"
(/ (normalized-all-line-contributions n power) (expt n power)))
(print (scale-all-line-contributions 10000000 1))
(format t "~20$~%" (scale-all-line-contributions 10000000 1))
编辑:代码中的固定逻辑错误。新的有理数,浮点对是33333333333333/100000000000000 0.33333334000000000000
答案 0 :(得分:4)
- ...
- (step 1) ember test
- (step 2) Exporting Heroku API Key
- (step 3) check_access_to_heroku_app my_project
- (step 4) Preparing repository for Heroku deployment
- (step 5) git remote add heroku_my_project git@heroku.com:my_project.git
- (step 6) git push heroku_tnt-ui $CI_COMMIT_ID:refs/heads/master
---- Log of Step 6 -----
Counting objects: 69, done.
Delta compression using up to 32 threads.
Compressing objects: 100% (17/17), done.
Writing objects: 100% (36/36), 6.48 KiB | 0 bytes/s, done.
Total 36 (delta 23), reused 31 (delta 19)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Fetching set buildpack https://github.com/tonycoco/heroku-buildpack-ember-cli.git... done
remote: -----> Ember CLI app detected
remote: -----> Exporting config vars to environment
remote: -----> Building environment: production
remote:
remote: PRO TIP: Avoid using semver ranges starting with '>' in engines.node
remote: See https://devcenter.heroku.com/articles/nodejs-support
remote:
remote: -----> Requested node range: >= 0.10.0
remote: -----> Resolved node version: 5.11.1
remote: -----> Downloading and installing node
remote: -----> Using default npm version: 3.8.6
remote: -----> Downloading and installing nginx
remote: -----> Adding boot script
remote: -----> Copying configs
remote: -----> Forcing rebuild of all node_modules. Pruning all node_modules.
remote: -----> Forcing re-installation of all bower_components. Pruning all bower_components.
remote: -----> Creating node_modules cache directory
remote: -----> Creating bower_components cache directory
remote: -----> Pruning cached dependencies not specified in package.json
remote: -----> Installing dependencies
remote: npm ERR! fetch failed https://registry.npmjs.org/ember-cli-sri/-/ember-cli-sri-2.1.0.tgz
remote: npm WARN retry will retry, error on last attempt: Error: fetch failed with status code 502
remote: npm ERR! fetch failed https://registry.npmjs.org/ember-simple-auth/-/ember-simple-auth-1.1.0.tgz
remote: npm WARN retry will retry, error on last attempt: Error: fetch failed with status code 502
remote: npm ERR! fetch failed https://registry.npmjs.org/ember-cli-sri/-/ember-cli-sri-2.1.0.tgz
remote: npm WARN retry will retry, error on last attempt: Error: fetch failed with status code 502
remote: npm ERR! fetch failed https://registry.npmjs.org/ember-simple-auth/-/ember-simple-auth-1.1.0.tgz
remote: npm WARN retry will retry, error on last attempt: Error: fetch failed with status code 502
remote: npm ERR! fetch failed https://registry.npmjs.org/ember-cli-sri/-/ember-cli-sri-2.1.0.tgz
remote: npm ERR! fetch failed https://registry.npmjs.org/ember-simple-auth/-/ember-simple-auth-1.1.0.tgz
remote: npm ERR! Linux 3.13.0-91-generic
remote: npm ERR! argv "/tmp/build_aaa68782965ff56ab3bc/vendor/node/bin/node" "/tmp/build_aaa6878296554310746bc/vendor/node/bin/npm" "install" "--quiet" "--no-optional" "--userconfig" "/tmp/build_aaa6875446ff56ab3bc/.npmrc"
remote: npm ERR! node v5.11.1
remote: npm ERR! npm v3.8.6
remote:
remote: npm ERR! fetch failed with status code 502
remote: npm ERR!
remote: npm ERR! If you need help, you may report this error at:
remote: npm ERR! <https://github.com/npm/npm/issues>
remote:
remote: npm ERR! Please include the following file with any support request:
remote: npm ERR! /tmp/build_aaa687829655456ab3bc/npm-debug.log
remote: 0 info it worked if it ends with ok
remote: 1 verbose cli [ '/tmp/build_aaa68782965546ab3bc/vendor/node/bin/node',
remote: 1 verbose cli '/tmp/build_aaa687846ff56ab3bc/vendor/node/bin/npm',
remote: 1 verbose cli 'install',
remote: 1 verbose cli '--quiet',
remote: 1 verbose cli '--no-optional',
remote: 1 verbose cli '--userconfig',
remote: 1 verbose cli '/tmp/build_aaa68782b46ff56ab3bc/.npmrc' ]
remote: 2 info using npm@3.8.6
remote: 3 info using node@v5.11.1
remote: 4 verbose config Skipping project config: /tmp/build_aaa6878296f56ab3bc/.npmrc. (matches userconfig)
remote: 5 silly loadCurrentTree Starting
remote: 6 silly install loadCurrentTree
remote: 7 silly install readLocalPackageData
remote: 8 silly install normalizeTree
...
请注意,对(format t "~20$" (coerce 16666671666667/50000000000000 'long-float))
; prints 0.33333343333334000000
(format t "~a" (float 16666671666667/50000000000000 1.0l0))
; prints 0.33333343333334d0
的强制可以在Common Lisp的不同实现中产生不同的结果(特别是在CLISP中)。
long-float
的第二个参数是原型:您应该提供任何浮点字面值,第一个参数将转换为相同类型的float。