乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 96|回复: 1

[编程交流] Division, but not quite.

[复制链接]

20

主题

344

帖子

325

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
100
发表于 2022-7-6 12:22:01 | 显示全部楼层 |阅读模式
So I was trying to code a base converter (you know, like base 10 to binary, that sort of deal) in LISP, and for some reason, what I was getting back wasn't working even though my math was spot on. I struggled with it for quite literally hours before stumbling upon the problem.
 
Now I don't know if this will work the same on every version, but try this out. Paste this in the command line
 
  1. (/ 51.0 10.0)
Returns 5.1, right? Well yeah, that's what you'd think it would do.
 
Now try this:
  1. (* 10 (rem (/ 51.0 10.0) 1))
For anyone not following me, I'm taking the remainder and multiplying it by 10. Returns 1.0, right? Well now, lets test that.
 
  1. (fix (* 10 (rem (/ 51.0 10.0) 1)))
"fix" removes the remainder from a floating point number and returns the integer portion. So waitatic, why is this returning 0? Isn't the integer portion of 1.0 1? Go ahead and try it:
 
  1. (fix 1.0)
So it seems yes, it is. So then why is it returning an incorrect number? There's only one way to peer deeper into a number, and that's by turning it into a string, and forcing it to display the extra digits.
 
  1. (rtos (* 10 (rem (/ 51.0 10.0) 1)) 2 16)
"rtos" turns a real number into a string. As you can see, 0.9999999999999964 is not equal to 1.0. Close, but no cigar.
 
Is this a problem with AutoCADs division operator? I know computers only work a certain way, but this has caused me hours of grief. I could understand if the numbers were very large, how significant digits would definitely play a part, but I think 51 and 10 are relatively small, and should not be causing an error like this.
回复

使用道具 举报

0

主题

269

帖子

279

银币

限制会员

铜币
-4
发表于 2022-7-6 13:38:26 | 显示全部楼层
I see there is some error that crops up in the very small decimals; happens with computers/computing.
 
What are you trying to determine?  If its' a decimal portion of a real, there are better ways than with 'rem', which is more suited to division of integers.
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-5 06:28 , Processed in 0.537798 second(s), 56 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表