PostScript:修复 float 的舍入错误

标签 postscript

我正在尝试制作一个带有数字标记的轴。一切工作正常,除了为轴生成的最后一个数字是 0.099999 而不是 0.1。我已从下面的代码中删除了线条绘制部分,但保留了生成数字的部分。这些是我从下面的代码中得到的数字:1.0、0.9、0.8、0.7、0.6、0.5、0.4、0.3、0.2、0.09999999(应该是 0.01)。是否有办法对数字进行舍入或以不同的方式计算数字以避免舍入误差?

/str 10 string def
/count 0 def
/totalUnits 1 def
/ticks 0.1 def

totalUnits ticks neg 0
{
/loopnum exch def
loopnum 10 str cvrs
/stringnumber exch def % string representing the number
/count count 0.1 add def
} for

最佳答案

正如评论所述,您可以使用整数来避免 float 的这种怪癖。

/str 10 string def 
/count 0 def 
/scaling .1 def 

10 -1 1{
    scaling mul 
    10 string cvs 
    %dup =
    /stringnumber exch def 
    /count count 1 add def 
} for 

同样的舍入问题会影响所有使用二进制浮点表示形式的语言,因为分数 1/10 在其二进制表示形式中具有重复模式。这与以十进制表示形式为 1/3 提供 0.333333... 的效果相同。对这些数量求和会丢失底部(输入)值已被截断的位。

关于PostScript:修复 float 的舍入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29637598/

相关文章:

pdf - 使用 Ghostscript 生成缩略图可旋转我的设备尺寸定义以实现横向 pdf 页面

c# - 将 postscript 转换为 jpeg

c# - 使用 C# 创建 Postscript 格式的文档

.net - 注入(inject)staple命令时postscript编码不一致

if-statement - PostScript - 比较值是否相等 + "if"结构

perl - 改进我的 Perl 算法以合并 postscript show 命令

python - Django 或 Python,如何将 postscript 转换为 PDF 或图像

pdf - 打印机吐出 'stair-cased' PostScript源代码

python - 将字符串列表转换为正确的类型

ghostscript - 如何通过命令行将 Illustrator 文件转换为 EPS (*nix)