educative.io

Educative

Why the code is bad with double but it's correct with int?

I did all the exercise correctly, but I wasn’t able to pass this one until I changed the declaration from “double length” to “int length”

I thought it was the same, but double handles decimals, why wasn’t the exercise correct with double?


Course: https://www.educative.io/collection/10370001/5692479535841280
Lesson: https://www.educative.io/collection/page/10370001/5692479535841280/6304186360659968

Hi Emilio_Parra, our requirement is to calculate the area of a rectangle, which is typically a measurement that involves whole numbers (integers) representing the length and width of the rectangle. That’s why we have specified int data type for the length and width fields.
On the other hand, double is used to handle decimal numbers, using it for length and width fields might not be appropriate in this case because it introduces unnecessary complexity and potential precision issues.

2 Likes