0
Why python always gives ans in float?
4 Respuestas
+ 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.
+ 1
Why it is designed so??