TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

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

2 pointsby ajimixalmost 6 years ago
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 comments

ThrowawayR2almost 6 years ago
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_Blokealmost 6 years ago
289&#x2F;30 = 9 as it&#x27;s truncating the non-integer part of the operation. So the calculation evaluates to 270 - 120 = 150
mtmailalmost 6 years ago
It defaults to integer arithmetic. 5&#x2F;3 will return 1. 5.to_f&#x2F;3 will return 1.6666666666666667
oregontechninjaalmost 6 years ago
This is a stack overflow question buddy