TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: Why Ruby doesn’t know how to do maths?

2 点作者 ajimix将近 6 年前
I&#x27;m not an expert in maths but the following operation gives a different result in ruby than in any other language or calculator I&#x27;ve tried:<p>Ruby: (289 &#x2F; 30 * 30) - (149 &#x2F; 30 * 30)<p>150<p>Rest of the world: (289 &#x2F; 30 * 30) - (149 &#x2F; 30 * 30)<p>140<p>An explanation is greatly appreciated

4 条评论

ThrowawayR2将近 6 年前
289&#x2F;30 is exactly 9 and 149&#x2F;30 is exactly 4 because they are integer expressions. You need to specify the constants as floating point values if you want floating point behavior.<p>Ruby does know how to do math and is doing precisely what you told it to.
评论 #20305133 未加载
Dajve_Bloke将近 6 年前
289&#x2F;30 = 9 as it&#x27;s truncating the non-integer part of the operation. So the calculation evaluates to 270 - 120 = 150
mtmail将近 6 年前
It defaults to integer arithmetic. 5&#x2F;3 will return 1. 5.to_f&#x2F;3 will return 1.6666666666666667
oregontechninja将近 6 年前
This is a stack overflow question buddy