0

Why python always gives ans in float?

14th Mar 2020, 9:17 AM
MR.VIJAY SHANKAR SAH
MR.VIJAY SHANKAR SAH - avatar
4 Respostas
+ 5
When in a language like C you write something like 5/2, the result will be 2. Even if you write... double x = 5/2; ... the result will still be 2.0, because 5 and 2 are integers and the decimals 'fall off'. To prevent this, you have to explicitly write: double x = 5/2.0; It's no problem, one can get used to it. However, for someone who doesn't know a lot about computer science and just wants to get some stuff done quickly, it can be confusing. Because in maths, decimals don't just 'fall off'. If the calculation leads to decimals the decimals will be there. Python conveniently simulates this behaviour for you: Whenever decimals *might* occur, the conversation to float happens automatically, so you never have to worry about losing information.
14th Mar 2020, 11:40 AM
HonFu
HonFu - avatar
+ 1
Why it is designed so??
14th Mar 2020, 9:21 AM
MR.VIJAY SHANKAR SAH
MR.VIJAY SHANKAR SAH - avatar